【Java】JDBC 数据库连接的演变
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了【Java】JDBC 数据库连接的演变,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5542字,纯文字阅读大概需要8分钟。
内容图文
![【Java】JDBC 数据库连接的演变](/upload/InfoBanner/zyjiaocheng/1247/e6b9631f99df4004ba869ac2e1d11a97.jpg)
环境搭建
使用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 // 提升可移植性,面向接口编程,不要出现第三方的APIpublicvoid 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 // 用驱动管理者代替驱动对象publicvoid 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 // 驱动再优化publicvoid 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+版本已经不需要驱动这玩意儿了publicvoid 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 // publicvoid 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 // publicvoid 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); }
原文:https://www.cnblogs.com/mindzone/p/12762480.html
内容总结
以上是互联网集市为您收集整理的【Java】JDBC 数据库连接的演变全部内容,希望文章能够帮你解决【Java】JDBC 数据库连接的演变所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。