JDBC中5种连接数据库的方式

package com.heima.jdbc;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class ConnectionTest {
    public static void main(String[] args) throws Exception {
        testConnection1();
        testConnection2();
        testConnection3();
        testConnection4();
        textConnection5();


    }
    public static void testConnection1() throws SQLException {
        // 多态实现,创建一个接口对象,该对象指向接口的实现类
        Driver driver = new com.mysql.cj.jdbc.Driver();
        String url = "jdbc:mysql://localhost:3306/eesy?serverTimezone=Hongkong";
        Properties info = new Properties();
        info.setProperty("user","root");
        info.setProperty("password","root");
        Connection con = driver.connect(url,info);
        System.out.println(con);
    }
    // 方式二:对方式一的迭代,在如下程序中不出现第三方的api,使程序具有更好的可移植性
    public static void testConnection2() throws Exception{
        //1.获取Driver的实现类对象,使用反射
        Class clazz = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver) clazz.newInstance();

        //2.提供要连接的数据库
        String url = "jdbc:mysql://localhost:3306/eesy?serverTimezone=Hongkong";

        //3.提供需要链接的数据库用户名和密码
        Properties info = new Properties();
        info.setProperty("user","root");
        info.setProperty("password","root");

        //4.获取链接
        Connection con = driver.connect(url,info);
        System.out.println(con);

    }

    // 方式三,使用DriverManager(驱动管理者)替换Driver

    public static void testConnection3() throws Exception {
        //1.获取Driver的实现类对象

        //注册驱动
        Class clazz = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver) clazz.newInstance();
        DriverManager.registerDriver(driver);

        //获取链接
        String url = "jdbc:mysql://localhost:3306/eesy?serverTimezone=Hongkong";
        Connection con = DriverManager.getConnection(url,"root","root");
        System.out.println(con);
    }

    //方式四,在方式三的基础上加以优化
    public static void testConnection4() throws Exception {
        //1.获取Driver的实现类对象

        //加载Driver类,其中的静态代码块随着类的加载被执行,从而注册了驱动;

        // 若为mysql数据库,则注册驱动这一步也可以省略,应为导入mysql时,在META-INF下的services下的
        // java.sql.Driver存在驱动路径,导入jar包时自动加载了驱动,但不建议,因为其他数据库
        Class.forName("com.mysql.cj.jdbc.Driver");
//        Driver driver = (Driver) clazz.newInstance();
//        DriverManager.registerDriver(driver);

        //获取链接
        String url = "jdbc:mysql://localhost:3306/eesy?serverTimezone=Hongkong";
        Connection con = DriverManager.getConnection(url,"root","root");
        System.out.println(con);
    }

    // 方式五(finally),将数据库连接需要的4个基本信息声明在配置文件中,获取链接
    public static void  textConnection5() throws Exception {
        //1.读入配置文件中的jdbc.properties中的信息
//        String name = ConnectionTest.class.getClassLoader().getResource("jdbc.properties").getPath();
//        System.out.println(name);
//        InputStream inputStream =new FileInputStream(name);
        // jdbc.properties配置文件要放在resources中
        InputStream inputStream = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
        //System.out.println(inputStream);
        Properties properties = new Properties();
        properties.load(inputStream);

      String user = properties.getProperty("user");
      String password = properties.getProperty("password");
      String url = properties.getProperty("url");
      String driverClass = properties.getProperty("driverClass");

      //2.加载驱动
      Class.forName(driverClass);

      //3.获取连接
        Connection connection = DriverManager.getConnection(url,user,password);

        System.out.println(connection);


    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值