-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJDBCUtilsByDruid.java
More file actions
65 lines (58 loc) · 2.2 KB
/
JDBCUtilsByDruid.java
File metadata and controls
65 lines (58 loc) · 2.2 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.charlie.jdbc.utils;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.*;
import java.util.Properties;
/**
* 基于Druid数据库连接池的工具类
*/
public class JDBCUtilsByDruid {
private static final String druidProperties = "src\\druid.properties";
private static final DataSource ds;
// 测试 该工具类
public static void main(String[] args) throws Exception {
String sql = "select * from admin";
Connection connection = JDBCUtilsByDruid.getConnection();
// class com.alibaba.druid.pool.DruidPooledConnection
System.out.println(connection.getClass());
PreparedStatement preparedStatement = connection.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery(sql);
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String pwd = resultSet.getString("pwd");
System.out.println(id + "\t" + name + "\t" + pwd);
}
JDBCUtilsByDruid.close(resultSet, preparedStatement, connection);
}
static {
Properties properties = new Properties();
try {
properties.load(new FileInputStream(druidProperties));
ds = DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
// 关闭连接,注意:在数据库连接池技术中,close不是真正地断掉连接
// 而是把使用的 Connection对象 放回到连接池
public static void close(ResultSet set, PreparedStatement preparedStatement, Connection connection) {
try {
if (set != null) {
set.close();
}
if (preparedStatement != null) {
preparedStatement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}