提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
前言
本文主要记录JDBC(mysql-connector-java-5.1.37-bin.jar)连接到数据库时,从使用Driver驱动到使用DriverManager,API使用变化(我也不知道自己在说什么)。不涉及底层原理,但有助于理解通过JDBC连接Mysql时,工具包的底层代码逻辑。
一、最基础版本
静态创建driver(就是new一个女朋友啦),在调用driver驱动获取connection连接之前,需要获得两个参数url和properties
package jdbcdemo.base;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import com.mysql.jdbc.Driver;
public class chapter1 {
public static void main(String[] args) throws SQLException, IOException, ClassNotFoundException {
//创建驱动
Driver driver = new Driver();
String url = "jdbc:mysql://localhost:3306/666_db02";
Properties info = new Properties();
info.setProperty("user", "root");
info.setProperty("password", "666");
//获得连接
Connection connection = driver.connect(url, info);
System.out.print("connected");
//记得随手关门
connection.close();
}
}
二、Driver和properties改进
1.Driver反射创建
反射创建driver,为后续创建properties配置文件时,提供Driver路径做准备。由硬编码优化为软编码。
//反射创建
Class c = Class.forName("com.mysql.jdbc.Driver");
Driver driver = (Driver)c.newInstance();
String url = "jdbc:mysql://localhost:3306/ldj_db02";
Properties info = new Properties();
info.setProperty("user", "root");
info.setProperty("password", "ldj");
Connection connection = driver.connect(url, info);
System.out.print("connected");
connection.close();
2.创建properties配置文件
除了url,数据库登录所需的user和password外,driverPath就是之前反射所需用到的Driver包包路径。
#Thu Nov 18 16:49:38 CST 2021
url=jdbc\:mysql\://localhost\:3306/666_db02
driverPath=com.mysql.jdbc.Driver
user=root
password=666
3.通过properties配置文件参数
以后只需要更换数据库,或者登录信息时,只要修改properties配置文件,不用修改源码。
//获取配置文件(包含Driver类路径、url配置、用户名、密码)
Properties info = new Properties();
info.load(new FileInputStream("src//driver.properties"));
Class c = Class.forName(info.getProperty("driverPath"));
Driver driver = (Driver)c.newInstance();
Connection connection = driver.connect(info.getProperty("url"), info);
System.out.print("connected");
connection.close();
三、DriverManager隆重登场
1.driver注册到DriverManager中
DriverManager取代 Driver,对Driver统一管理,便于应对需要连接多个数据库的场景。
//获取配置文件(包含Driver类路径、url配置、用户名、密码)
Properties info = new Properties();
info.load(new FileInputStream("src//driver.properties"));
//将反射创建的driver注册到DriverManger
Class a = Class.forName(info.getProperty("driverPath"));
DriverManager.registerDriver((Driver)a.newInstance());
//由DriverManager来获得conection连接
Connection connection = DriverManager.getConnection(info.getProperty("url"),info);
System.out.print("connected");
connection.close();
2. 简化driver注册(终极版)
Driver类中的·静态代码块:调用DriverManager注册了driver对象
static {
try {
DriverManager.registerDriver(new Driver());
} catch (SQLException var1) {
throw new RuntimeException("Can't register driver!");
}
}
mysql-connector-java-5.1.37-bin.jar中:META-INF/java.sql.Driver配置文件包含了Driver路径,使用JDBC时会直接创建driver。
Properties info = new Properties();
info.load(new FileInputStream("src//driver.properties"));
//反射加载driver,可省略(不推荐)
Class.forName(info.getProperty("driverPath"));
Connection connection = DriverManager.getConnection(info.getProperty("url"),info);
System.out.print("connected");
connection.close();