JDBC获取数据库连接五种方式

391 阅读2分钟

image.png 这里阐述JDBC程序访问数据库的第一步————创建Connection对象

方式一:使用第三方Api

public void testConnection() throws SQLException {
    Driver driver = new com.mysql.cj.jdbc.Driver();
    String ur1 = "jdbc:mysql://localhost:3306/test?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC &rewriteBatchedStatements=true";
    Properties info= new Properties() ;
    info.setProperty("user","root");
    info.setProperty("password","******");
    Connection conn = driver.connect(ur1, info);
    System.out.println(conn);
}

方式二: 在如下的程序中不出现第三方的api,是得程序具有更好得移植性

    public void testConnection2() throws Exception {
        //1.通过反射获取对象
        Class clazz = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver) clazz.newInstance();
        //2.提供要连接的数据库
        String ur1 = "jdbc:mysql://localhost:3306/test?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC &rewriteBatchedStatements=true";
        //3.提供连接需要的用户名和密码
        Properties info = new Properties();
        info.setProperty("user","root");
        info.setProperty("password","*******");
//        4.获取连接
        Connection conn = driver.connect(ur1,info);
        System.out.println(conn);
    }

方式三:通过反射获取对象

public void testConnection3() throws Exception {
    //1.通过反射获取对象
    Class clazz = Class.forName("com.mysql.cj.jdbc.Driver");
    Driver driver = (Driver) clazz.newInstance();
    //2.提供另外三个连接得基本信息
    String ur1 = "jdbc:mysql://localhost:3306/test?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC &rewriteBatchedStatements=true";
    String user = "root";
    String password = "021216";
    //3.注册驱动
    DriverManager.registerDriver(driver);
    //4.获取连接
    Connection conn = DriverManager.getConnection(ur1, user, password);
    System.out.println(conn);

}

方式四:除去可以省略的步骤

    public void testconnection4() throws Exception {
        //1.提供另外三个连接得基本信息
        String ur1 = "jdbc:mysql://localhost:3306/test?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC &rewriteBatchedStatements=true";
        String user = "root";
        String password = "021216";
        //2.获取Driver实现类的对象
        Class.forName("com.mysql.cj.jdbc.Driver");
        //加载驱动
//        Driver driver = (Driver) clazz.newInstance();
//        //注册驱动
//        DriverManager.registerDriver(driver);
        //3.获取连接
        Connection conn = DriverManager.getConnection(ur1, user, password);
        System.out.println(conn);
    }

方式五:最终版 将数据库连接需要读取的四个信息放在配置文件中,通过读取配置文件的方式,获取连接。通过IO流来读取配置文件中的基本信息

public void testConnection5() throws Exception {
    //1.读取配置文件中的基本信息
    InputStream is = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
    Properties pros = new Properties();
    pros.load(is);
    String user = pros.getProperty("user");
    String password = pros.getProperty("password");
    String ur1 = pros.getProperty("ur1");
    String driverClass = pros.getProperty("driverClass");
    //2.加载驱动
    Class.forName(driverClass);
    //3.获取连接
    Connection conn = DriverManager.getConnection(ur1, user, password);
    System.out.println(conn);

}
```
```