ConnectionPool.java
6.14 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
package com.sitech.database;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Date;
import java.util.ResourceBundle;
import com.sitech.util.DES3;
import com.sitech.util.Formater;
public class ConnectionPool {
private static int iDrivertype = 0;
private static String sUserName;
private static String sPassword;
private static String sUrl;
private static String sClassforName;
private static boolean waitingcloseconnection = false;
private static ConnectionPool instance = new ConnectionPool();
static private java.sql.Connection conn = null;
static private java.util.Date connDate = null;
static private int connCount = 0;
static private int currconnCount = 0;
private boolean bCommitstate;
private ConnectionPool() {
}
public static ConnectionPool getInstance() {
return instance;
}
static {
try {
ResourceBundle LogfileInfo = ResourceBundle.getBundle("LoginInfo");
sClassforName = LogfileInfo.getString("ClassforName");
sUrl = LogfileInfo.getString("url");
sUserName = LogfileInfo.getString("sqluser");
sPassword = LogfileInfo.getString("sqlpassword");
sPassword = DES3.decrypt(sPassword);
} catch (Exception e) {
}
}
public java.sql.Connection Connection() throws SQLException {
return conn;
}
public synchronized void increase(int i) {
connCount++;
currconnCount = currconnCount + i;
}
synchronized public java.sql.Connection createConnection()
throws SQLException {
if (waitingcloseconnection) {
try {
while (waitingcloseconnection) {
Thread.sleep(1);
}
} catch (Exception e) {
}
}
increase(1);
if (conn != null && !conn.isClosed())
return conn;
try {
Class.forName(sClassforName).newInstance();
conn = DriverManager.getConnection(sUrl, sUserName, sPassword);
connDate = new Date();
connCount = 0;
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
synchronized public void closeConn() throws SQLException {
Date preRefresh = Formater.computeDate(new Date(), 'f', -10);// l��10������Ч
if (preRefresh.after(connDate) || connCount > 500) {
if (conn != null) {
if (currconnCount > 1) {
waitingcloseconnection = true;
try {
int i = 0;
while (currconnCount > 1 && i < 10) {
Thread.sleep(80);
}
} catch (Exception e) {
e.printStackTrace();
}
}
conn.close();
conn = null;
increase(-1);
waitingcloseconnection = false;
}
}
}
protected void finalize() throws Throwable {
if (conn != null) {
conn.close();
conn = null;
}
}
public java.sql.Statement createStatement() throws SQLException {
if (conn != null)
return conn.createStatement();
else
return null;
}
public java.sql.Statement createScrollStatement() throws SQLException {
if (conn != null)
if (iDrivertype == 0)
return conn.createStatement();
else
return conn.createStatement(
java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,
java.sql.ResultSet.CONCUR_READ_ONLY);
else
return null;
}
public java.sql.PreparedStatement createPreparedStatement(String sql)
throws SQLException {
if (conn != null)
return conn.prepareStatement(sql);
else
return null;
}
/* execute Prepared Statement */
public void executePreparedStmt(java.sql.PreparedStatement pstmt)
throws SQLException {
if (conn != null) {
pstmt.executeUpdate();
} else
return;
}
/* execute Prepared Statement */
public java.sql.CallableStatement createPreparedCall(String sql)
throws SQLException {
if (conn != null) {
return conn.prepareCall(sql);
} else
return null;
}
public void beginTransaction() throws SQLException {
if (conn != null) {
bCommitstate = conn.getAutoCommit();
conn.setAutoCommit(false);
}
}
public void endTransaction() throws SQLException {
if (conn != null) {
if (bCommitstate != conn.getAutoCommit())
conn.setAutoCommit(bCommitstate);
}
}
public void commit() throws SQLException {
if (conn != null)
conn.commit();
}
public void rollback() throws SQLException {
if (conn != null)
conn.rollback();
}
public static void main(String[] argv) throws Exception {
String SQL = "select * from tblWasteNo";
// String SQL = "insert into company
// (company_id,name,phone,fax,email,address,postcode,describe) values
// ('0001','sitechcompany','62501566','62501566','bossd_www@si-tech.com.cn','zhongguancun','100086','hello')";
// String SQL = "insert into EMPLOYEE (
// Employee_Id,Name,UserName,Password,Warn_Before_Days,PW_Invalid_Date )
// values ('tt02209','test','tttt','1678',5,'1999-02-12')";
// String SQL = "insert into tt values (1)";
// DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
// java.sql.Connection con =
// DriverManager.getConnection("jdbc:oracle:thin:@172.16.9.3:1521:ora252","dalu","dalu");
ConnectionPool conPool = ConnectionPool.getInstance();
conPool.createConnection();
System.out.println(conPool);
// java.sql.Statement stmt = con.createStatement();
java.sql.Statement stmt = conPool.createStatement();
System.out.println(stmt);
boolean results = true;
stmt.execute(SQL);
System.out.println(results);
java.sql.ResultSet rs;
// int iQuantity;
if (results) {
rs = stmt.getResultSet();
System.out.println(rs);
while (rs.next()) {
/*
* System.out.println(rs.getInt(1));
* System.out.println(rs.getInt(2));
* System.out.println(sitech.util.Format.fromSybaseString(rs.getString(3)).trim());
* System.out.println(rs.getDate(4));
* System.out.println(rs.getInt(5));
*
* System.out.println(sitech.util.Format.fromSybaseString(rs.getString(6)).trim());
* System.out.println(sitech.util.Format.fromSybaseString(rs.getString(7)).trim());
* System.out.println(rs.getInt(8));
* System.out.println(rs.getInt(9));
* System.out.println(rs.getInt(10));
*
* System.out.println(rs.getInt(11));
* System.out.println(sitech.util.Format.fromSybaseString(rs.getString(12)).trim());
*
*/
System.out.println(rs.getInt(1));
// iQuantity = rs.getRow();
}
rs.close();
}
stmt.close();
// con.close();
conPool.closeConn();
}
}