Day1.2.DriverManager学习笔记

DriverManager 是驱动的管理类:
1)可以通过重载的getConnection()方法获取数据库连接,较为方便
2)可以同时管理多个驱动程序:若注册了多个数据库连接,则调用getConnection()方法时传入的参数不同,即返回不同的数据库连接。

DriverManaer学习代码:

public class TestConnection {
    public void testDriver() throws Exception {
        //1.创建一个Driver 实现类的对象
        Driver driver = new com.mysql.cj.jdbc.Driver();
        //2.准备连接数据库的基本信息:url,user,password
        String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
        Properties info = new Properties();
        info.put("user", "root");
        info.put("password", "zjxjq88394722");

        //3.调用Driver 接口的 connect(url,info)获取数据库连接
        Connection connection = driver.connect(url, info);
        System.out.println(connection);
    }

    //通用方法
    public Connection getConnection() throws Exception {
        String driverClass = null;
        String jdbcUrl = null;
        String user = null;
        String password = null;

        //读取类路径下的jdbc.properties文件
        InputStream in =
                getClass().getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        properties.load(in);
        driverClass = properties.getProperty("driver");
        jdbcUrl = properties.getProperty("jdbcUrl");
        user = properties.getProperty("user");
        password = properties.getProperty("password");


        Driver driver =
                (Driver) Class.forName(driverClass).newInstance();
        Properties info = new Properties();
        info.put("user", user);
        info.put("password", password);
        Connection connection = driver.connect(jdbcUrl, info);

        return connection;
    }

    //测试
    @Test
    public void testConnection() throws Exception {
        System.out.println(getConnection());
    }


    @Test
    public void testDriverManager() throws Exception {
        //1.准备连接数据库的四个字符串
        //驱动的全类名
        String driverClass = "com.mysql.cj.jdbc.Driver";
        //JDBC URL
        String jdbcUrl = "jdbc:mysql://localhost:3306/test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
        //user
        String user = "root";
        //password
        String password = "zjxjq88394722";

        //2.读取类路径下的jdbc.properties文件
        InputStream in =
                getClass().getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        properties.load(in);
        driverClass = properties.getProperty("driver");
        jdbcUrl = properties.getProperty("jdbcUrl");
        user = properties.getProperty("user");
        password = properties.getProperty("password");

        //3.加载数据库驱动程序(DriverManager可以注册多个驱动程序)
        Class.forName(driverClass);

        //4.通过DriverManager 的getConnection()方法获取数据库连接
        Connection connection =
                DriverManager.getConnection(jdbcUrl, user, password);
        System.out.println(connection);
    }

    public Connection getConnection2() throws Exception {
        //1.准备连接数据库的四个字符串
        //1.1创建Properties 对象
        Properties properties = new Properties();

        //1.2获取jdbc.properties 对应的输入流
        InputStream in =
                this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");

        //1.3加载1.2对应的输入流
        properties.load(in);

        //1.4具体决定user,password等4个字符串
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String jdbcUrl = properties.getProperty("jdbcUrl");
        String driver = properties.getProperty("driver");

        //2.加载数据库驱动程序(DriverManager可以注册多个驱动程序)
        Class.forName(driver);

        //3.通过DriverManager 的getConnection()方法获取数据库连接
        return DriverManager.getConnection(jdbcUrl,user,password);
    }

    @Test
    public void testGetConnection2() throws Exception {
        System.out.println(getConnection2());
    }
}

猜你喜欢

转载自blog.csdn.net/zjhzxjq/article/details/88642899