环境搭建
使用Maven工程的依赖项,如果普通工程就点注释的地址下载jar包即可
<dependencies> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.19</version> </dependency> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> </dependencies>
原始JDBC链接
@Test public void connectionTest1() throws SQLException { // 获取驱动对象 // 这是8+版本的驱动,5+版本的驱动是这样的com.mysql.jdbc.Driver Driver driver = new com.mysql.cj.jdbc.Driver(); // 注入连接信息 这也是8+的链接方式,必须声明时区,5+版本 jdbc:mysql://localhost:3306/mysql String url = "jdbc:mysql://localhost:3306/mysql?serverTimezone=Asia/Shanghai"; // 协议 jdbc:mysql: // 地址 localhost: // MySQL端口号 3306 // 数据库 mysql // 参数 serverTimezone=Asia/Shanghai" // 配置对象封装账户信息 Properties properties = new Properties(); properties.setProperty("user","root"); properties.setProperty("password","123456"); // 注入信息,得到链接 Connection connection = driver.connect(url,properties); //com.mysql.cj.jdbc.ConnectionImpl@1d548a08 System.out.println(connection); }
演变1 利用反射调取实现类创建驱动实例
@Test // 提升可移植性,面向接口编程,不要出现第三方的API public void connectionTest2() throws SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException { //使用反射动态,获取Driver实现类对象 Class<?> driverClass = Class.forName("com.mysql.cj.jdbc.Driver"); Driver driver = (Driver) driverClass.newInstance(); String url = "jdbc:mysql://localhost:3306/mysql?serverTimezone=Asia/Shanghai"; Properties properties = new Properties(); properties.setProperty("user","root"); properties.setProperty("password","123456"); Connection connection = driver.connect(url,properties); System.out.println(connection); }
演变2 利用驱动管理者实现
@Test // 用驱动管理者代替驱动对象 public void connectionTest3() throws SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException { Class<?> driverClass = Class.forName("com.mysql.cj.jdbc.Driver"); Driver driver = (Driver) driverClass.newInstance(); // 驱动注册 java.sql.DriverManager.registerDriver(driver); String url = "jdbc:mysql://localhost:3306/mysql?serverTimezone=Asia/Shanghai"; String user = "root"; String password = "123456"; // 用驱动管理者配置链接信息去获取连接对象 Connection connection = DriverManager.getConnection(url, user, password); System.out.println(connection); }
演变3 驱动优化
@Test // 驱动再优化 public void connectionTest4() throws SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException { // 注册驱动已经不需要我们来编写了 Class.forName("com.mysql.cj.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/mysql?serverTimezone=Asia/Shanghai"; String user = "root"; String password = "123456"; // 用驱动管理者配置链接信息去获取连接对象 Connection connection = DriverManager.getConnection(url, user, password); System.out.println(connection); }
演变4 驱动完全不需要写了 jdbc5+版本支持此写法
@Test // 驱动再再优化 在5+版本已经不需要驱动这玩意儿了 public void connectionTest4() throws SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException { String url = "jdbc:mysql://localhost:3306/mysql?serverTimezone=Asia/Shanghai"; String user = "root"; String password = "123456"; // 用驱动管理者配置链接信息去获取连接对象 Connection connection = DriverManager.getConnection(url, user, password); System.out.println(connection); }
演示5 配置信息不再使用硬编码的方式注入
配置可随意更改,实现了数据和代码的解耦
@Test // public void connectionTest5() throws SQLException, ClassNotFoundException, IOException { InputStream inputStream = ConnectorTest.class.getClassLoader().getResourceAsStream("jdbc.properties"); Properties properties = new Properties(); properties.load(inputStream); String driverClass = properties.getProperty("driverClass"); String url = properties.getProperty("url"); String user = properties.getProperty("user"); String password = properties.getProperty("password"); //加载驱动 Class.forName(driverClass); Connection connection = DriverManager.getConnection(url, user, password); System.out.println(connection); }
在Maven工程,配置文件放在sources里面
在生成打包文件时,自动生成对应的配置文件
非Maven的普通项目可采用下面这两种方式读取配置文件
@Test public void connectionTest6() throws SQLException, ClassNotFoundException, IOException { // 返回URL的编码 %20 类加载器读取 文件的位置默认是在当前Module或者项目的src包下 String path = Loader.class.getClassLoader().getResource("jdbc.properties").getFile(); // 需要解码 String decode = URLDecoder.decode(path, "UTF-8"); System.out.println(path); System.out.println(decode); Properties properties = new Properties(); properties.load(new FileInputStream(decode)); String driverClass = properties.getProperty("driverClass"); String url = properties.getProperty("url"); String user = properties.getProperty("user"); String password = properties.getProperty("password"); //加载驱动 Class.forName(driverClass); Connection connection = DriverManager.getConnection(url, user, password); System.out.println(connection); } @Test // public void connectionTest7() throws SQLException, ClassNotFoundException, IOException { Properties properties = new Properties(); properties.load(new FileInputStream("src\\jdbc.properties")); String driverClass = properties.getProperty("driverClass"); String url = properties.getProperty("url"); String user = properties.getProperty("user"); String password = properties.getProperty("password"); //加载驱动 Class.forName(driverClass); Connection connection = DriverManager.getConnection(url, user, password); System.out.println(connection); }