怎么模块化JDBC,把对数据库的操作代码封装起来,形成DBUtil类
时间: 2024-01-22 07:01:55 浏览: 35
模块化JDBC的主要思路是将数据库连接、关闭、查询和更新等操作封装在一个类中,使得其他地方只需要调用该类的方法即可完成对数据库的操作。下面是一个简单的DBUtil类的实现示例:
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil {
// 数据库连接信息
private static final String URL = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC";
private static final String USER = "root";
private static final String PASSWORD = "123456";
// 静态代码块,在类被加载时就会执行,用于加载数据库驱动
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
// 获取数据库连接
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL, USER, PASSWORD);
}
// 关闭数据库连接
public static void close(Connection conn, PreparedStatement ps, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
// 查询操作
public static ResultSet query(String sql, Object... params) throws SQLException {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = getConnection();
ps = conn.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
ps.setObject(i + 1, params[i]);
}
rs = ps.executeQuery();
} catch (SQLException e) {
close(conn, ps, rs);
throw e;
}
return rs;
}
// 更新操作
public static int update(String sql, Object... params) throws SQLException {
Connection conn = null;
PreparedStatement ps = null;
int result = 0;
try {
conn = getConnection();
ps = conn.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
ps.setObject(i + 1, params[i]);
}
result = ps.executeUpdate();
} catch (SQLException e) {
close(conn, ps, null);
throw e;
}
return result;
}
}
```
在上面的示例中,我们将数据库连接信息、获取连接、关闭连接、查询和更新等操作都封装在了DBUtil类中。使用时,只需要调用该类的方法即可完成对数据库的操作。例如,查询用户信息的代码如下:
```java
public User queryUserById(int id) throws SQLException {
String sql = "select * from user where id = ?";
ResultSet rs = DBUtil.query(sql, id);
User user = null;
if (rs.next()) {
user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setAge(rs.getInt("age"));
user.setSex(rs.getString("sex"));
}
return user;
}
```
在上面的代码中,我们使用DBUtil类的query方法查询用户信息,然后将查询结果封装成一个User对象返回。通过这种方式,我们可以很方便地对数据库进行操作,并且可以很容易地修改和扩展数据库操作的功能。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)