【Java】JDBC 数据库连接的演变
短信预约 信息系统项目管理师 报名、考试、查分时间动态提醒
环境搭建
使用Maven工程的依赖项,如果普通工程就点注释的地址下载jar包即可
<dependencies>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.19version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.13version>
<scope>testscope>
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);
//[email protected]
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或者项目的class="lazy" data-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("class="lazy" data-srcjdbc.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);
}
【Java】JDBC 数据库连接的演变
原文:https://www.cnblogs.com/mindzone/p/12762480.html
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341