77bb4d17642b0da2ceaeeef7b3cf2276d2122691.svn-base
4.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package com.espeed.reading.util;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.commons.dbcp2.BasicDataSourceFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
*
*
* @项目名称: 邮件跟踪系统
* @版权所有: 深圳市科飞时速网络技术有限公司(0755-88843776)
* @技术支持: info@21gmail.com
* @单元名称: 获取数据库连接的工具类
* @开始时间: 2018-5-23
* @开发人员: 杨志钊
*/
public class DBUtil {
private static final Log log = LogFactory.getLog(DBUtil.class);
private static DataSource dataSource;
static {
// 加载配置文件
Properties props = new Properties();
InputStream is = null;
try {
is = new FileInputStream(DBUtil.class.getClassLoader()
.getResource("").getPath()
.replaceFirst("classes", "config.properties"));
props.load(is);
dataSource = BasicDataSourceFactory.createDataSource(props);
printDataSourceStatus();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private DBUtil() {
}
/** 获取数据库连接对象 */
public static Connection getConnection() throws SQLException {
Connection conn = null;
if (dataSource != null) {
conn = dataSource.getConnection();
}
return conn;
}
/** 释放资源 */
public static void close(ResultSet rs, Statement st, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (st != null) {
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static String printDataSourceStatus() {
BasicDataSource basicDataSource = (BasicDataSource) dataSource;
StringBuilder builder = new StringBuilder();
builder.append("数据库连接池启动成功,配置参数如下:\n");
builder.append("初始化连接数:" + basicDataSource.getInitialSize() + "\n");
builder.append("最大活动连接数:" + basicDataSource.getMaxTotal() + "\n");
builder.append("最大空闲连接数:" + basicDataSource.getMaxIdle() + "\n");
builder.append("最小空闲连接数:" + basicDataSource.getMinIdle() + "\n");
builder.append("最大等待时间:" + (basicDataSource.getMaxWaitMillis() / 1000)
+ "s\n");
builder.append("超时时间:" + basicDataSource.getRemoveAbandonedTimeout()
+ "s\n");
builder.append("默认自动提交状态:" + basicDataSource.getDefaultAutoCommit()
+ "\n");
builder.append("\n@当前活跃连接数:" + basicDataSource.getNumActive() + "\n");
builder.append("@当前空闲数:" + basicDataSource.getNumIdle() + "\n");
log.warn(builder);
return builder.toString();
}
// public static String DBUrl=ConfigPath.getBaseUrl();
// public static String DBUser=ConfigPath.getBaseUserName();
// public static String DBPassword=ConfigPath.getBasePassword();
// public static final String DBDriver="com.mysql.jdbc.Driver";
// public static Connection conn;
// //加载驱动
// static
// {
// try {
// Class.forName(DBDriver);
//
// } catch (Exception e)
// {
// e.printStackTrace();
// }
// }
// //获得连接
// public static Connection getConnection() throws SQLException{
// return DriverManager.getConnection(DBUrl, DBUser, DBPassword);
// }
//
// /** 关闭数据库连接*/
// public static void close(ResultSet rs,Statement st,Connection conn){
// if(rs!=null){ try {
// rs.close();
// } catch (SQLException e) {
// e.printStackTrace();
// }finally{
// if(st!=null){
// try {
// st.close();
// } catch (SQLException e) {
// e.printStackTrace();
// }finally{
// if(conn!=null){
// try {
// conn.close();
// } catch (SQLException e) {
// e.printStackTrace();
// }
// }
// }
// }
// }
// }
// }
}