forked from yangtong123/StudySpark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJDBCHelper.java
214 lines (180 loc) · 5.61 KB
/
JDBCHelper.java
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package com.yt.spark.jdbc;
import com.yt.spark.conf.Configuration;
import com.yt.spark.conf.Constants;
import java.sql.*;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* JDBC辅助组件
*
* Created by yangtong on 17/5/5.
*/
public class JDBCHelper {
//加载数据库驱动
static {
try {
String driver = Configuration.getProperty(Constants.JDBC_DRIVER);
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//实现单例化
private static JDBCHelper instance = null;
public static JDBCHelper getInstance() {
if (instance == null) {
synchronized (JDBCHelper.class) {
if (instance == null) {
instance = new JDBCHelper();
}
}
}
return instance;
}
private LinkedList<Connection> datasources = new LinkedList<Connection>();
private JDBCHelper() {
int datasourceSize = Configuration.getInteger(Constants.JDBC_DATASOURCE_SIZE);
boolean local = Configuration.getBoolean(Constants.SPARK_LOCAL);
String url = null;
String user = null;
String password = null;
if (local) {
url = Configuration.getProperty(Constants.JDBC_URL);
user = Configuration.getProperty(Constants.JDBC_USER);
password = Configuration.getProperty(Constants.JDBC_PASSWORD);
} else {
url = Configuration.getProperty(Constants.JDBC_URL_PROD);
user = Configuration.getProperty(Constants.JDBC_USER_PROD);
password = Configuration.getProperty(Constants.JDBC_PASSWORD_PROD);
}
//创建指定数量的数据库连接,并放入数据库连接池中
for (int i = 0; i < datasourceSize; i++) {
try {
Connection conn = DriverManager.getConnection(url, user, password);
datasources.push(conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 提供获取数据库连接的方法,并做多线程访问控制
* @return
*/
public synchronized Connection getConnection() {
//如果连接池为空,那么等待1秒
while (datasources.size() == 0) {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return datasources.poll();
}
/**
* 增删改查
* @param sql
* @param params
* @return 影响的行数
*/
public int executeUpdate(String sql, Object[] params) {
int rtn = 0;
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = getConnection();
pstmt = conn.prepareStatement(sql);
if (params != null && params.length > 0) {
for (int i = 0; i < params.length; i++) {
pstmt.setObject(i+1, params[i]);
}
}
rtn = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
datasources.push(conn);
}
}
return rtn;
}
/**
* 执行查询语句
* @param sql
* @param params
* @param callBack
*/
public void executeQuery(String sql, Object[] params, QueryCallBack callBack) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = getConnection();
pstmt = conn.prepareStatement(sql);
if (params != null && params.length > 0) {
for (int i = 0; i < params.length; i++) {
pstmt.setObject(i+1, params[i]);
}
}
rs = pstmt.executeQuery();
callBack.process(rs);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
datasources.push(conn);
}
}
}
/**
* 批量执行sql语句
* @param sql
* @param paramsList
* @return
*/
public int[] executeBatch(String sql, List<Object[]> paramsList) {
int[] rtn = null;
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = getConnection();
//1.取消自动提交
conn.setAutoCommit(false);
pstmt = conn.prepareStatement(sql);
if (paramsList != null && paramsList.size() > 0) {
//2.使用PreparedStatement.addBatch()方法加入批量sql操作
for (Object[] params : paramsList) {
for (int i = 0; i < params.length; i++) {
pstmt.setObject(i+1, params[i]);
}
pstmt.addBatch();
}
}
//3.使用PreparedStatement.executeBatch()执行批量
rtn = pstmt.executeBatch();
//4.批量提交
conn.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
datasources.push(conn);
}
}
return rtn;
}
/**
* 查询回调接口
*/
public static interface QueryCallBack {
/**
* 处理查询结果
* @param rs
* @throws Exception
*/
void process(ResultSet rs) throws Exception;
}
}