环境搭建
使用Maven工程的依赖项,如果普通工程就点注释的地址下载jar包即可
mysql
mysql-connector-java
8.0.19
junit
junit
4.13
test
原始JDBC链接
@Testpublic void connectionTest1() throwsSQLException {//获取驱动对象//这是8+版本的驱动,5+版本的驱动是这样的com.mysql.jdbc.Driver
Driver driver = newcom.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 = newProperties();
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() throwsSQLException, 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= newProperties();
properties.setProperty("user","root");
properties.setProperty("password","123456");
Connection connection=driver.connect(url,properties);
System.out.println(connection);
}
演变2 利用驱动管理者实现
@Test //用驱动管理者代替驱动对象
public void connectionTest3() throwsSQLException, 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() throwsSQLException, 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() throwsSQLException, 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() throwsSQLException, ClassNotFoundException, IOException {
InputStream inputStream= ConnectorTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties= newProperties();
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的普通项目可采用下面这两种方式读取配置文件
@Testpublic void connectionTest6() throwsSQLException, 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= newProperties();
properties.load(newFileInputStream(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() throwsSQLException, ClassNotFoundException, IOException {
Properties properties= newProperties();
properties.load(new FileInputStream("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