创建连接的五种方式(JDBC)

这篇博客介绍了Java中使用JDBC连接MySQL的四种常见方式,包括直接实例化Driver、反射加载Driver、使用DriverManager注册Driver以及不注册Driver直接获取连接。每种方式都包含了获取连接、执行SQL语句和关闭资源的基本步骤。此外,还提到了SQL注入的概念,并给出了一条示例注入语句,强调了安全性在数据库操作中的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.jdbc;

import com.mysql.jdbc.Driver;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * 第一种连接方式
 */
public class jdbc01 {
    public static void main(String[] args) throws SQLException {
        //1.注册驱动
        Driver driver = new Driver();

        //2.得到连接
        //① jdbc:mysql:// 规定好的表示协议,通过jdbc的方式连接mysql
        //② localhost: 主机,可以是ip地址
        //③ 3306 表示mysql监听的端口
        //④ execute 连接到mysql dbms的哪个数据库
        //⑤ mysql的连接本质就是前面学过的socket连接
        String url = "jdbc:mysql://localhost:3306/execute";
        //将用户名和密码放入到Properties对象中
        Properties properties = new Properties();
        //说明:user和password是规定好,后面的值根据实际情况写
        properties.setProperty("user","root");  //用户
        properties.setProperty("password","178966");//密码
        //用mysql的驱动输入的用户和密码去连接
        Connection connect = driver.connect(url, properties);

        //3.执行sql
//        String sql = "insert into actor values(null,'刘德华','男','1970-11-11','110')";
        String sql = "update actor set name = '杨佳涵' where id = 1";
        //statement 用户执行静态SQL语句并返回其生成的结果的对象
        Statement statement = connect.createStatement();
        int rows = statement.executeUpdate(sql);
        System.out.println(rows > 0 ? "成功" : "失败");

        //4.关闭连接资源
        statement.close();
        connect.close();

    }
}

package com.jdbc;

import com.mysql.jdbc.Driver;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * 第二种连接方式: 使用反射加载Driver类,动态记载,更加的灵活,减少依赖性
 */
public class jdbc02 {
    public static void main(String[] args) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        //1.注册驱动
        //使用反射加载Driver类,动态加载,更加的灵活,减少依赖性
        Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) aClass.newInstance();

        //得到连接
        String url = "jdbc:mysql://localhost:3306/execute";
        //将用户名和密码放入Properties对象中
        Properties properties = new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password","178966");
        Connection connection  = driver.connect(url,properties);

        //3.操作SQL语句
        String sql = "delete from actor where id = 2";
        //Statement用于执行静态SQL语句并返回其生成的结果的对象
        Statement statement = connection.createStatement();
        int rows = statement.executeUpdate(sql);    //如果是 dml语句,返回的就是受影响的行数
        System.out.println(rows > 0 ? "成功" : "失败");

        //关闭资源连接
        statement.close();
        connection.close();
    }
}

package com.jdbc;

import com.mysql.jdbc.Driver;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 第三种连接数据库方式
 */
public class jdbc03 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        //使用反射加载Driver
        Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) aClass.newInstance();

        //创建url和user和password
        String url = "jdbc:mysql://localhost:3306/execute";
        String user = "root";
        String password = "178966";
        DriverManager.registerDriver(driver);   //注册Driver驱动

        Connection connection = DriverManager.getConnection(url,user,password);
        System.out.println(connection);

    }
}

package com.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 创建连接的第四种方式
 */
public class jdbc04 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //使用反射加载了Driver类
        Class.forName("com.mysql.jdbc.Driver");

        //创建url和user和password
        String url = "jdbc:mysql://localhost:3306/execute";
        String user = "root";
        String password = "178966";
        Connection connection = DriverManager.getConnection(url,user,password);
        System.out.println(connection);
    }
}

package com.jdbc;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 创建连接的第五种方式:最常用
 */
public class jdbc05 {
    public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
        //通过Properties对象获取配置文件的信息
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\mysql.properties"));
        //获取相关的值
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");

        Class.forName(driver);  //注册驱动

        Connection connection = DriverManager.getConnection(url,user,password); //获取注册驱动连接
        System.out.println(connection);
    }
}

SQL注入:

name = ‘1’ or’ and pwd = 'or ‘1’ = ‘1’

输入用户名:1’ or

输入密码为:or ‘1’ = '1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值