JDBC封装类

把JDBC封装在一个类,可以直接完成加载连接关闭

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.example.oa;

import java.sql.*;
import java.util.ResourceBundle;

public class DBUil {
private static ResourceBundle bundle = ResourceBundle.getBundle("resources.jdbc");
private static String driver = bundle.getString("driver");
private static String url = bundle.getString("url");
private static String user = bundle.getString("user");
private static String password = bundle.getString("password");
static {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
public static Connection getConnection (String[] args) throws SQLException {
Connection connection = DriverManager.getConnection(url,user,password);
return connection;
}
public static void close(Connection connection, Statement ps, ResultSet rs){
if (rs != null){
try {
rs.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (ps != null){
try {
ps.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (connection != null){
try {
connection.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}



}
}


JDBC封装类
http://example.com/2024/04/07/JDBC封装类/
作者
Jack Asher
发布于
2024年4月7日
许可协议