JDBC_2 获取数据库的连接

获取数据库

重点是“方式五”,其他的知道为什么这么迭代就可以了。

方式一

思路:driver,url,info,connection

实例化一个Driver对象,用这个对象的connect方法返回一个Connection对象,然后这个connect方法又需要两个参数(url:数据库地址,info:连接数据库的参数,账号和密码)

    @Test
    public void testconnection1() throws SQLException {
        // 实例化,需要mysql的一个具体的Driver的实现类    多态
        // 获取Driver的实现类对象
        Driver driver = new com.mysql.cj.jdbc.Driver();

        String url = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC&characterEncoding=utf8&userUnicode=true&useSSL=false";

        Properties info = new Properties();
        info.setProperty("user", "root");
        info.setProperty("password", "123456");
        // 获取数据库连接对象 connect
        Connection connect = driver.connect(url, info);
        System.out.println(connect);
    }

执行结果:

方式二

迭代原因:使得在程序中不出现第三方的API(com.mysql.cj.jdbc.Driver),使得程序具有更好的可移植性。

思路:使用 反射 获取Driver的实现类对象

    @Test
    public void testConnection2() throws Exception{
        Class aClass = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver) aClass.newInstance();

        String url = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC&characterEncoding=utf8&useSSL=false";
        Properties info = new Properties();
        info.setProperty("user", "root");
        info.setProperty("password", "123456");

        Connection conn = driver.connect(url, info);
        System.out.println(conn);
    }

方式三

思路:DriverManager(registerDriver,getConnection)

    @Test
    public void testConnection3() throws Exception{
        Class clazz = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver) clazz.newInstance();

        String url = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC&characterEncoding=utf8&useSSL=false";
        String user = "root";
        String password = "123456";

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

方式四

优化:注册驱动 可省,因为在Driver类中有一个静态代码块随着Driver类的加载而加载,里面注册了驱动

    @Test
    public void testConnection4() throws Exception{
        String url = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC&characterEncoding=utf8&useSSL=false";
        String user = "root";
        String password = "123456";

        Class.forName("com.mysql.cj.jdbc.Driver");

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

方式五(最终版)

思路:将数据库加载需要的参数声明在配置文件中,通过读取配置文件的方式,获取连接

好处:实现解耦(数据和代码的分离)、打包的问题(如果配置参数在代码中,重新打包需要花费时间)

    @Test
    public void testConnection5() throws Exception{
        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 url = pros.getProperty("url");
        String driverClass = pros.getProperty("driverClass");

        Class.forName(driverClass);

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

ConnectionTest.class中的 ".class"是获取类的实例的一种方法(反射),再".getClassLoader()对类进行加载

classLoader是负责把磁盘上的 .class文件加载到JVM内存中,

 

 

 

2021.5.27 上海漕河泾

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值