JDBC 数据库的几种连接方式

前言:JDBC中一般有五种连接方式,这五种方式都给大家分享下,大家根据真实的业务场景去选择连接方式。

JDBC操作步骤:

  1. 获取driver对象
  2. 注册驱动(方式一、方式二不需要注册,因为直接通过driver对象获取的连接)
  3. 获取连接
  4. 执行SQL操作
  5. 关闭连接,释放资源
方式一:
public static void main(String[] args) throws SQLException {
        // 1. 注册Driver
        Driver driver = new com.mysql.jdbc.Driver();
        // 2. 获取连接对象
        /*
        jdbc: 协议
        mysql: 子协议
        localhost: ip地址
        3306: 端口号
        hsp_jdbc: 数据库
         */
        String url = "jdbc:mysql://localhost:3306/hsp_jdbc";
        Properties prop = new Properties();
        prop.setProperty("user", "root");
        prop.setProperty("password", "123456");

        Connection connect = driver.connect(url, prop);
		// 3. 打印连接对象
		System.out.println(connect);
        
        // 4. 关闭连接
        statement.close();
        connect.close();
    }
方式二:利用反射
public static void main(String[] args) throws Exceptionn {

        // 利用反射
        // 1. 注册Driver
        Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) aClass.newInstance();

        // 2. 获取连接对象
        String url = "jdbc:mysql://localhost:3306/hsp_jdbc";
        Properties prop = new Properties();
        prop.setProperty("user", "root");
        prop.setProperty("password", "123456");

        Connection connect = driver.connect(url, prop);
		// 3. 打印对象
        System.out.println(connect);
        // 4. 关闭连接
        connect.close();
    }
方式三:利用DriverManager替换Driver
public static void main(String[] args) throws Exception {

        // 利用DriverManager替换driver
        // 1. 注册Driver
        Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) aClass.newInstance();

        // 2. 获取连接对象
        String url = "jdbc:mysql://localhost:3306/hsp_jdbc";
        Properties prop = new Properties();
        prop.setProperty("user", "root");
        prop.setProperty("password", "123456");

        DriverManager.registerDriver(driver);
        Connection connection = DriverManager.getConnection(url, prop);
		// 3. 打印连接对象
        System.out.println(connection);

        // 4. 关闭连接
        connection.close();
    }
方式四:自动注册
public static void main(String[] args) throws Exception {

        // Class.forName自动完成注册
        // 1. 注册Driver
        Class.forName("com.mysql.jdbc.Driver");

        // 2. 获取连接对象
        String url = "jdbc:mysql://localhost:3306/hsp_jdbc";
        String user = "root";
        String password = "123456";

        Connection connection = DriverManager.getConnection(url, user, password);
		// 3. 打印对象
        System.out.println(connection);

        // 4. 关闭连接
        connection.close();
    }
方式五:抽取配置信息
public static void main(String[] args) throws SQLException, IOException, ClassNotFoundException {

        // 将配置信息写到配置文件
        // 获取配置文件中的信息
        Properties prop = new Properties();
        prop.load(new FileInputStream("src\\jdbc_mysql.properties"));

        String url = prop.getProperty("url");
        String user = prop.getProperty("user");
        String password = prop.getProperty("password");
        String driver = prop.getProperty("driver");
		// 可以省略
        Class.forName(driver);

        Connection connection = DriverManager.getConnection(url, user, password);
		 // 3. 打印对象
        System.out.println(connection);

        // 4. 关闭连接
        connection.close();
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值