-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJDBCUtilsByDruid.java
More file actions
51 lines (45 loc) · 1.52 KB
/
JDBCUtilsByDruid.java
File metadata and controls
51 lines (45 loc) · 1.52 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
package com.charlie.dao_.utils;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
/**
* 基于Druid数据库连接池的工具类
*/
public class JDBCUtilsByDruid {
private static final String druidProperties = "src\\druid.properties";
private static final DataSource ds;
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);
}
}
}