JDBC获取数据库连接的5种方式实例
作者:COWARD_LOVE1 时间:2024-01-19 05:14:05
方式一:直接通过数据库厂商提供的相关驱动
步骤
导入相关驱动(Build Path)
创建Driver类的实例化对象
获取要连接数据库的URL
创建Properties类的实例化对象,将账号和密码封装到该对象中
通过Driver实例化对象调用connect(String url,Properties info)方法获取connection对象
package connection;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.Properties;
import org.junit.jupiter.api.Test;
public class ConnectionTest {
@Test
public void test() throws SQLException {
//创建Driver实例
Driver driver=new com.mysql.cj.jdbc.Driver();
//获取要读取数据库的URL
String url="jdbc:mysql://localhost:3306/test04_emp";
//将账号密码封装到Properties对象中
Properties info=new Properties();
info.setProperty("user", "root");
info.setProperty("password", "abc123");
//通过Driver的对象获取连接
Connection connect=driver.connect(url, info);
//调用连接的toString()
System.out.println(connect);
}
}
方法二:通过反射的方式来构造Driver对象
通过Class的forName(Stirng className)获取驱动的运行时类。
通过运行时类的newInstance()来获取运行时类的对象。
获取URL和将账号密码封装到Properties对象中。
使用运行时类的对象调用connect()方法获取连接。
@Test
public void test2() throws Exception {
//通过反射的方式获取运行时类
Class clazz=Class.forName("com.mysql.cj.jdbc.Driver");
//通过反射的方式创建运行时类对象
Driver driver=(Driver)clazz.newInstance();
//获取URL
String url="jdbc:mysql://localhost:3306/test04_emp";
//封装账号密码
Properties info=new Properties();
info.setProperty("user", "root");
info.setProperty("password", "abc123");
//获取连接
Connection connect=driver.connect(url, info);
System.out.println(connect);
}
方式三:使用DriverManager来替换Driver获取连接
获取Driver的实现类对象。
使用DriverManager的registerDriver(Driver driver)方法来注册驱动。
提供URL、账号和密码。
使用DriverManager的getConnection(String url,String user,String password)来获取连接。
@Test
public void test3() throws Exception {
//通过反射的方式获取Driver对象
Driver driver=(Driver)Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
//调用DriverManager的registerDriver(Driver driver)方法注册驱动
DriverManager.registerDriver(driver);
//提供账号、密码和URL
String user="root";
String password="abc123";
String url="jdbc:mysql://localhost:3306/test04_emp";
//调用DriverManager的getConnection()方法来获取连接
Connection connect=DriverManager.getConnection(url,user,password);
System.out.println(connect);
/*getConnection()还有一种版本:getConnection(String url,Properties info)
* 可以参考方法一和方法二。
*/
}
方法四:省略创建Driver对象和注册驱动
通过反射将Driver类加载到内存中。
提供账号、密码和URL。
通过DriverManager调用getConnection()获取连接。
注意:这里之所以可以省略创建Driver对象和注册驱动是因为,创建Driver对象的目的是给DriverManager调用registerDriver()注册驱动时提供参数,而在Driver类加载到内存中时,有一个static方法会自动调用registerDriver()方法,从而自动注册驱动。
@Test
public void test4() throws Exception {
//通过反射将Driver加载到内存中
Class.forName("com.mysql.cj.jdbc.Driver");
//提供相关信息
String user="root";
String password="abc123";
String url="jdbc:mysql://localhost:3306/test04_emp";
//通过getConnection()获取连接
Connection connect=DriverManager.getConnection(url,user,password);
System.out.println(connect);
}
方式五:通过配置文件的方式
创建配置文件
通过类的加载器生成指向配置文件的流
创建Properties对象,通过调用load()方法将配置文件加载到内存中
通过Properti对象的getProperty(String key)获取相关信息
通过反射的方式加载驱动
调用getConnection()获取连接
@Test
public void test5() throws Exception {
//通过类加载器创建一个指向配置文件的流
InputStream input=ConnectionTest.class.getClassLoader().getResourceAsStream("test.properties");
//创建Properties对象
Properties infoProperties=new Properties();
//以流为参数调用load()加载配置文件
infoProperties.load(input);
//获取相关信息
String driver=infoProperties.getProperty("driver");
String url=infoProperties.getProperty("url");
String user=infoProperties.getProperty("user");
String password=infoProperties.getProperty("password");
//加载驱动
Class.forName(driver);
//获取连接
Connection connection= DriverManager.getConnection(url, user, password);
System.out.println(connection);
}
我们通常都使用方式五
每种方式迭代的原因
第一种方式是直接通过使用指定数据库厂商的驱动来获取数据库连接的,但是我们希望程序具有更好的可移植性,所以采用反射的方式来获取驱动,这就产生了第二种方法;在开发中,我们通常都不使用Driver来获取连接,而是通过使用DriverManager来获得数据库连接,这就从第二种方式到了第三种方式;又因为Driver的实现类中有静态方法调用了regiesterDriver()方法,所以在通过反射的方式将类加载到内存中时,会自动注册驱动,我们可以将注册驱动的过程省略,这就产生了第四种方法;在前面的方法中,我们都是直接把账号密码和URL以及驱动的名称直接写在程序中的额,这不太符合我们日常的规范,也在一定程度上增加了程序的 风险,为此我们将相关信息保存在配置文件中,在程序中通过读取配置文件的方式来获取相关的信息,这就产生了最后一种也是我们最常用的方式。
方式五的优点
将需要的数据放到配置文件中,实现了数据和代码分离,减少了耦合性。
如果需要修改配置文件信息,只需要替换文件就可以,避免了重新打包文件。
来源:https://blog.csdn.net/m0_71987537/article/details/125284985
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python之基础函数案例详解
![](https://img.aspxhome.com/file/2023/8/87808_0s.png)
asp.net中不能在DropDownList中选择多个项 原因分析及解决方法
09年QQ登录Banner之转变
![](https://img.aspxhome.com/file/UploadPic/20092/16/09qqbanner_5-69.jpg)
解决mysql5中文乱码问题的方法
人工智能Text Generation文本生成原理示例详解
浅谈PHP中的Trait使用方法
![](https://img.aspxhome.com/file/2023/8/55738_0s.png)
利用Python实现一个简易的截图工具
python 统计代码行数简单实例
python基于opencv实现人脸识别
![](https://img.aspxhome.com/file/2023/8/68468_0s.jpg)
Django中间件拦截未登录url实例详解
JavaScript中的Promise使用详解
![](https://img.aspxhome.com/file/2023/9/136329_0s.jpg)
python中pip安装库时出现Read timed out解决办法
![](https://img.aspxhome.com/file/2023/6/85036_0s.png)
盘点网络编程必须要知道的基础知识
![](https://img.aspxhome.com/file/2023/4/131794_0s.png)
asp.net C#实现解压缩文件的方法
Pytorch的安装过程之pip、conda、Docker容器安装
Nodejs 数组的队列以及forEach的应用详解
javascript一个无懈可击的实例化XMLHttpRequest的方法
在python plt图表中文字大小调节的方法
python实现在cmd窗口显示彩色文字
![](https://img.aspxhome.com/file/2023/6/131276_0s.jpg)