获取数据库连接
不同的数据库在其内部的插入,删除,查询等方式是不同的,为了更好的使用,java将其统一起来,使用统一接口,不同的数据库在接口中实现这些统一接口的方法即可;这套统一的接口用来连接数据库,这套统一接口,称之为JDBC。
使用这样的规范,对于不同的数据库使用起来就比较方便,适用性也强了很多,不同的数据库使用不同的驱动即可。
连接流程
-
定义Driver(驱动)
-
定义url及info(数据库用户名及密码)
url: jdbc:mysql(协议名)😕/localhost(ip地址):3306(默认的mysql端口号)/test(表名) (取自驱动说明文档)
info: 数据库的用户名以及连接名使用Porperties存储
-
使用Driver对象.connect()方法获取连接
方式一:
@Test
public void getConnnect1() throws SQLException{
//获得驱动对象
Driver driver = new com.mysql.cj.jdbc.Driver();
String url = "jdbc:mysql://localhost:3306/db_test";
Properties info = new Properties();
//key名称不能改变,获取连接时通过key获取用户名和密码
info.put("user","root");
info.put("password","root");
//获取连接
Connection connect = driver.connect(url, info);
System.out.println(connect);
}
问题:代码的通用性较低,直接出现了mysql类,如果数据库不同,或是没有导入对应的mysql驱动,程序连运行都运行不起来
方式二:
@Test
public void getConnect2() throws Exception{
//使用反射的方式获得Driver对象
Class driverClass = Class.forName("com.mysql.cj.jdbc.Driver");
Driver driver = (Driver) driverClass.getConstructor().newInstance();
//之后的操作与方式一相同
String url = "jdbc:mysql://localhost:3306/db_test";
Properties info = new Properties();
//key名称不能改变,获取连接时通过key获取用户名和密码
info.put("user","root");
info.put("password","root");
driver.connect(url,info);
}
问题:总是要new一个info对象存放用户名和密码,可以采用driverManager类,减少变量的定义
方式三:
@Test
public void getConnect3() throws Exception{
//使用反射的方式获得Driver对象
Class driverClass = Class.forName("com.mysql.cj.jdbc.Driver");
Driver driver = (Driver) driverClass.getConstructor().newInstance();
//得到对象之后需要使用DriverManager注册
DriverManager.registerDriver(driver);
String url = "jdbc:mysql://localhost:3306/db_test";
String user = "root";
String password = "root";
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println("connection----->" + connection);
}
connection----->com.mysql.cj.jdbc.ConnectionImpl@ca263c2
mysql中的Driver类源码
public class Driver extends NonRegisteringDriver implements java.sql.Driver {
public Driver() throws SQLException {
}
static {
try {
DriverManager.registerDriver(new Driver());
} catch (SQLException var1) {
throw new RuntimeException("Can't register driver!");
}
}
}
由此可知MySQL类被加载的时候内部已经完成了Driver的注册操作,所以外部就不需要继续注册了
实际上对于MySQL而言,不仅是注册,甚至连反射加载Driver包都不需要,因为MySQL类的包再被导入的时候,Service会自动导入驱动,但是这仅仅是对于MySQL而言,所以一般写JDBC时还是会写上加载Driver
方式四:
@Test
public void getConnect4() throws Exception{
//使用反射的方式获得Driver对象 可以省略 但是仅适用与MySQL所以一般还是会写上
Class.forName("com.mysql.cj.jdbc.Driver");
// Class driverClass = Class.forName("com.mysql.cj.jdbc.Driver");
// Driver driver = (Driver) driverClass.getConstructor().newInstance();
//得到对象之后需要使用DriverManager注册
// DriverManager.registerDriver(driver);
String url = "jdbc:mysql://localhost:3306/db_test";
String user = "root";
String password = "root";
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println("connection----->" + connection);
}
方式五(一般写法):
实际上对于上述的写法都存在一个很大的问题,其中的url,user等配置文件都写在了类的代码中,维护之后要重新将类打包,配置和代码耦合在一起,维护起来不方便
@Test
public void getConnect5() throws Exception{
//读取配置文件信息
//通过类的加载器加载配置资源文件
InputStream is = JDBCTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties = new Properties();
properties.load(is);
//获取配置信息
String url = properties.getProperty("url");
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String driverClassName = properties.getProperty("driverClassName");
//获取连接
Class.forName(driverClassName);
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println("connection----->" + connection);
}
jdbc.properties
url=jdbc:mysql://localhost:3306/db_test
user=root
password=root
driverClassName=com.mysql.cj.jdbc.Driver
输出
connection----->com.mysql.cj.jdbc.ConnectionImpl@ca263c2
把配置文件和代码分开,耦合性降低
注意:使用class.getClassLoader().getResourceAsStream()时文件要放在resource下,且不能带\否则会报错