封装工具类
1 为什么要封装工具类
- 在实际JDBC中的使用中,存在着大量的重复代码:例如连接数据库、关闭数据库这些操作。
- 我们需要把传统的JDBC代码进行重构,抽取出通用的JDBC工具类。以后连接任何数据库、释放资源都可以使用这个工具类。

2 重用性方案
2.1 方案思想
(1)新建一个工具类DBUtils。
(2)在工具类DBUtils类中封装获取连接、释放资源两个方法。
- 提供public static Connection getConnection(){}方法。
- 提供public static void closeAll(Connection connection,Statement statement,ResultSet resultSet){}方法
2.2 方案代码
```
package cn.bdqn.demo03;
import java.sql.*;
/**
* 重用性方案
* 获取连接
* 释放资源
*/
public class DBUtils {
static {// 类加载,执行一次!
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
// 1.获取连接
public static Connection getConnection() {
Connection connection = null;
try {
connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/java", "root", "123456");
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
// 2.释放资源
public static void closeAll(Connection connection, Statement statement,
ResultSet resultSet) {
try {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
3 跨平台方案
3.1 方案思想
(1)在项目src文件夹下创建db.properties文件,在文件中编写数据库驱动、数据库url、用户名、密码相关数据。
```
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/java
username=root
password=123456
```
(2)在工具类DBUtils类中读取db.properties配置文件数据。
```
定义private static final Properties PROPERTIES = new Properties();//读取配置文件的Map
定义static{
//通过复用本类自带流,读取配置文件中的数据
InputStream is = DBUtils.class.getResourceAsStream("配置文件路径");
// 通过prop对象将流中的配置信息分隔成键值对
PROPERTIES.load(is);
//通过driverName的键获取对应的值(com.mysql.jdbc.Driver)
String driverName=PROPERTIES.getProperty("driver");
//注册驱动
Class.forName(driverName);
}
```
(3)在工具类DBUtils类中封装获取连接、释放资源两个方法。
- 提供public static Connection getConnection(){}方法。
- 提供public static void closeAll(Connection connection,Statement statement,ResultSet resultSet){}方法
3.2 方案代码
```
package cn.bdqn.demo04;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class DBUtils {
// //读取配置文件的Map
private static final Properties PROPERTIES = new Properties();
static {
// 通过复用本类自带流,读取配置文件中的数据
InputStream is = DBUtils.class.getResourceAsStream("/db.properties");
try {
// 通过prop对象将流中的配置信息分隔成键值对,将配置文件内容加载到properties集合
PROPERTIES.load(is);
// 注册驱动,通过driverName的键获取对应的值(com.mysql.jdbc.Driver)
Class.forName(PROPERTIES.getProperty("driver"));
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
// 获取连接对象
public static Connection getConnection() {
Connection connection = null;
try {
connection = DriverManager.getConnection(
PROPERTIES.getProperty("url"),
PROPERTIES.getProperty("username"),
PROPERTIES.getProperty("password"));
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
// 释放所有资源
public static void closeAll(Connection connection, Statement statement,
ResultSet resultSet) {
try {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```