Menu

[r2]: / dbpool / DBOperator.java  Maximize  Restore  History

Download this file

280 lines (244 with data), 6.8 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/**
* <b>数据库操作封装类,用于本包内部使用</b>
* @author 汉娱网络.技术部.wanggl
* @since 1.1
*/
package com.handjoys.dbpool;
import java.sql.*;
import java.util.*;
import com.handjoys.logger.FileLogger;
/**
* <b>数据库操作封装类,用于本包内部使用</b>
* <p>数据库操作的封装类,封装的操作有:
* <li>连接数据库</li>
* <li>断开连接</li>
* <li>执行无返回sql查询</li>
* <li>执行有返回sql查询</li>
* </p>
* @author 汉娱网络.技术部.wanggl
* @since 1.1
*/
public class DBOperator {
/**
* <b>连接数据库</b>
* <p>连接数据库并把连接的句柄返回,失败的话,返回null</p>
* @param driverStr 连接驱动的字符串
* @param connectStr 连接的字符串
* @param dbUser 用户名
* @param dbPassword 密码
* @return 数据库的连接句柄
* @since 1.1
*/
public Connection connectDataBase(String driverStr,String connectStr, String dbUser,
String dbPassword) {
Connection con = null;
try {
//Class.forName("com.handjoys.mysql.jdbc.Driver");
Class.forName(driverStr);
//连接数据库
con = DriverManager.getConnection(connectStr+"?user="+dbUser+"&password="+dbPassword+"&useUnicode=true&characterEncoding=utf-8");
if(con!=null)
{
//输出连接成功
FileLogger.debug("DB:" + connectStr + ", Successfully connected!");
//查询一下 设置uft8编码传送
/**
try
{
update("set names 'utf8'",con);
}catch(Exception er){
er.printStackTrace();
}
***/
}
} catch (ClassNotFoundException e) {
// 驱动没有找到
FileLogger.error("we can not find the jdbc driver");
} catch (SQLException e) {
//连接失败
FileLogger.error("DB:" + connectStr + ", connected failed! Reason:"+e.getMessage());
con = null;
}
return con;
}
/**
* <b>执行无返回查询</b>
* <p>执行诸如insert, update,delete 之类的查询</p>
* @param queryStr 要查询的语句
* @param con 连接句柄
* @return 查询影响条数
* @throws Exception
* @since 1.1
*/
public int update(String queryStr, Connection con) throws Exception {
int ret = 0;
PreparedStatement pstmt;
if (con != null) {
pstmt = con.prepareStatement(queryStr);
try {
FileLogger.debug("ExeSql: "+queryStr);
ret= pstmt.executeUpdate();
} catch (Exception e) {
ret = 0;
FileLogger.error("Execute Sql:" + queryStr
+ "\t [failed] reason:" + e.getMessage());
throw(e);
}
finally
{
pstmt.close();
}
}
return ret;
}
public int batchTransactionUpdate(List<String> queryStrLisyt,Connection con) throws Exception {
int ret = 0;
Statement stmt;
if (con != null) {
con.setAutoCommit(false);
stmt = con.createStatement();
try {
stmt.executeUpdate("START TRANSACTION;");
for(int i=0;i<queryStrLisyt.size();i++)
{
stmt.addBatch(queryStrLisyt.get(i));
}
int [] updateCounts = stmt.executeBatch();
for(int i=0;i<updateCounts.length;i++)
{
FileLogger.debug("batch update result:"+updateCounts[i]+", Statement.SUCCESS_NO_INFO"+Statement.SUCCESS_NO_INFO);
if(updateCounts[i]==Statement.SUCCESS_NO_INFO || updateCounts[i]>0)
{
ret++;
}
else if(updateCounts[i]==Statement.EXECUTE_FAILED);
{
throw new Exception("query failed, while process batch update");
}
}
con.commit();
} catch (Exception e) {
ret = 0;
FileLogger.debug(e.getMessage());
con.rollback();
}
finally
{
con.setAutoCommit(true);
stmt.close();
}
}
return ret;
}
/**
* <b>执行单条有返回语句</b>
* <p>执行select的单条查询,查询失败返回null</p>
* @param queryStr 查询语句
* @param con 连接句柄
* @return 查询结果集
*/
public List<HashMap> select(String queryStr, Connection con) throws Exception {
List<HashMap> ret=new ArrayList<HashMap>();
ResultSet rset = null;
PreparedStatement stmt;
ResultSetMetaData mData;
int count=0;
if (con != null) {
stmt = con.prepareStatement(queryStr);
try {
FileLogger.debug("ExeSql: "+queryStr);
rset = stmt.executeQuery();
FileLogger.debug("Excecute query: " + queryStr + "\t[ok]");
mData=rset.getMetaData();
count=mData.getColumnCount();
//System.out.println("count:"+count);
while(rset.next())
{
HashMap<String,Object> tmpHashMap=new HashMap<String,Object>();
for(int i=1;i<=count;i++)
{
Object tmpobj=rset.getObject(i);
if(tmpobj==null)
{
//根据不同的类型,设置不同的值
switch(mData.getColumnType(i))
{
//整型
case Types.BIGINT:
tmpobj=-1L;
break;
case Types.INTEGER:
case Types.SMALLINT:
tmpobj=-1;
break;
//逻辑型
case Types.BIT:
tmpobj=0;
break;
//字符型
case Types.CHAR:
case Types.VARCHAR:
case Types.LONGVARCHAR:
case Types.CLOB:
tmpobj="";
break;
//时间型
case Types.TIME:
case Types.DATE:
case Types.TIMESTAMP:
tmpobj=new java.util.Date();
break;
//二进制型
case Types.BINARY:
case Types.LONGVARBINARY:
case Types.VARBINARY:
tmpobj=new Object();
break;
//带小数点型
case Types.DECIMAL:
case Types.DOUBLE:
case Types.FLOAT:
case Types.NUMERIC:
tmpobj=0.0D;
break;
}
tmpHashMap.put(mData.getColumnName(i),tmpobj);
}
else
{
tmpHashMap.put(mData.getColumnName(i),rset.getObject(i));
}
//System.out.println(mData.getColumnName(i)+"-->"+rset.getObject(i));
}
ret.add(tmpHashMap);
}
} catch (Exception e) {
e.printStackTrace();
FileLogger.error("Excecute query: " + queryStr + "\t[failed] Reason:"+e.getMessage());
ret = null;
throw(e);
}
finally
{
stmt.close();
}
}
return ret;
}
/**
* <b>关闭连接</b>
* <p>关闭传入的连接</p>
* @param con 连接句柄
*/
public void closeConnection(Connection con) {
if (con != null)
{
try {
con.close();
} catch (Exception e) {
//我们主动关闭之前已经关闭了这个连接
FileLogger.debug("A connection lost while closing it, please check com.handjoys.dbpool.DBOperator");
}
}
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.