【数据库编程】2.获取数据库连接5种方式
2021/8/13 2:08:33
本文主要是介绍【数据库编程】2.获取数据库连接5种方式,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
获取数据库连接的5种方式
1. 基础
@Test public void connect01() throws SQLException { Driver driver = new Driver(); String url = "jdbc:mysql://localhost:3306/demo"; Properties properties = new Properties(); properties.setProperty("user", "root"); properties.setProperty("password", "123456"); Connection connect = driver.connect(url, properties); System.out.println(connect); }
2. 使用反射加载Driver类
使用反射加载Driver类是动态加载,会更加灵活,可以减少依赖。
@Test public void connect02() throws Exception { Driver driver = (Driver)Class.forName("com.mysql.jdbc.Driver").newInstance(); String url = "jdbc:mysql://localhost:3306/demo"; Properties properties = new Properties(); properties.setProperty("user", "root"); properties.setProperty("password", "123456"); Connection connect = driver.connect(url, properties); System.out.println(connect); }
3. 使用DriverManager
使用DriverManager替代Driver类进行统一管理,这种方式的代码也会更简洁一些。
@Test public void connect03() throws Exception { // 使用DriverManager替代Driver进行统一管理 Driver driver = (Driver)Class.forName("com.mysql.jdbc.Driver").newInstance(); String url = "jdbc:mysql://localhost:3306/demo"; String user = "root"; String password = "123456"; // 注册驱动 DriverManager.registerDriver(driver); Connection connection = DriverManager.getConnection(url, user, password); System.out.println(connection); }
4. 自动注册驱动
这种方式最常用。使用Class.forName()自动完成注册读懂,因为在Driver类的静态代码块中注册了Driver。源码如下:
static { try { // 注册驱动 DriverManager.registerDriver(new Driver()); } catch (SQLException var1) { throw new RuntimeException("Can't register driver!"); } }
因此获取连接的代码可以更简洁,代码如下:
@Test public void connect04() throws Exception { // 使用Class.forName自动完成注册驱动,简化代码 // Driver类静态代码块中注册了Driver /* 1. mysql驱动5.1.6可以无需Class.forName("com.mysql.jdbc.Driver"); 2. 从JDK1.5以后调用了JDBC4,不再需要显示调用class.forName()注册驱动而是自动调用驱动jar包下 META-INF\services\java.sql\Driver文本中的类名称去注册 3. 建议写上,更加明确 */ Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/demo"; String user = "root"; String password = "123456"; Connection connection = DriverManager.getConnection(url, user, password); System.out.println(connection); }
注:
- MySQL驱动5.1.6可以无需Class.forName("com.mysql.jdbc.Driver");
- 从JDK1.5以后调用了JDBC4,不再需要显示调用Class.forName()注册驱动而是自动调用驱动jar包下META-INF\services\java.sql\Driver文本中的类名称去注册
- 建议写上,更加明确
5. 添加配置文件
对方式4进行改进,添加配置文件,把用户名、密码、驱动包路径、URL都写在配置文件中,增加程序的灵活性。
创建mysql.properties文件,写入如下内容:
user=root password=123456 url=jdbc:mysql://localhost:3306/demo?rewriteBatchedStatements=true dirver=com.mysql.jdbc.Driver
将配置文件读入到Properties对象中,再从Properties对象获取每个值。代码如下:
@Test public void connect05() throws SQLException, IOException, ClassNotFoundException { // 加载配置文件 Properties properties = new Properties(); properties.load(new FileInputStream("src\\mysql.properties")); // 获取值 String user = properties.getProperty("user"); String password = properties.getProperty("password"); String dirver = properties.getProperty("dirver"); String url = properties.getProperty("url"); Class.forName(dirver); Connection connection = DriverManager.getConnection(url, user, password); System.out.println(connection); }
这篇关于【数据库编程】2.获取数据库连接5种方式的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-06小米11i印度快充版ROM合集:极致体验,超越期待
- 2024-10-06【ROM下载】小米11i 5G 印度版系统, 疾速跃迁,定义新速度
- 2024-10-06【ROM下载】小米 11 青春活力版,青春无极限,活力全开
- 2024-10-05小米13T Pro系统合集:性能与摄影的极致融合,值得你升级的系统ROM
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求