1
|
|
package nmp.dbms.JDBC;
|
2
|
|
|
3
|
|
import java.io.IOException;
|
4
|
|
import java.io.PrintWriter;
|
5
|
|
import java.sql.ResultSet;
|
6
|
|
import java.sql.ResultSetMetaData;
|
7
|
|
import java.sql.SQLException;
|
8
|
|
import java.util.Vector;
|
9
|
|
|
10
|
|
import nmp.dbms.ExtSQLVectorResult;
|
11
|
|
import nmp.dbms.SQLVectorResult;
|
12
|
|
import nmp.util.Log;
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
public class Jdbc_Sql{
|
18
|
|
Vector connections;
|
19
|
|
DBConnection defaultCon;
|
20
|
|
boolean debug, debugLL;
|
21
|
|
PrintWriter logWriter;
|
22
|
|
String driverName, url, user, passwd;
|
23
|
|
int numCons;
|
24
|
|
|
25
|
0
|
private synchronized DBConnection getConnection0()
|
26
|
|
throws SQLException {
|
27
|
0
|
DBConnection con;
|
28
|
0
|
if( connections.size() > 0)
|
29
|
0
|
con = (DBConnection)connections.remove( connections.size() - 1);
|
30
|
|
else
|
31
|
0
|
con = new DBConnection( driverName, url, user, passwd, debugLL,logWriter);
|
32
|
0
|
if( ! con.isValid())
|
33
|
0
|
con = new DBConnection( driverName, url, user, passwd, debugLL,logWriter);
|
34
|
0
|
return con;
|
35
|
|
}
|
36
|
|
|
37
|
0
|
private synchronized void putConnection0( DBConnection con) {
|
38
|
0
|
if( connections.size() > numCons) {
|
39
|
0
|
try {
|
40
|
0
|
con.closeConnection();
|
41
|
|
}catch ( SQLException e) {
|
42
|
|
|
43
|
|
}
|
44
|
0
|
return;
|
45
|
|
}
|
46
|
0
|
try {
|
47
|
0
|
con.reset();
|
48
|
|
}catch ( SQLException e) {
|
49
|
0
|
return;
|
50
|
|
}
|
51
|
0
|
if( con.isValid())
|
52
|
0
|
connections.addElement( con);
|
53
|
0
|
notifyAll();
|
54
|
|
}
|
55
|
|
|
56
|
0
|
public Jdbc_Sql( String driverName, String url, String user, String passwd)
|
57
|
|
throws SQLException{
|
58
|
0
|
this( driverName, url, user, passwd, false, false, null, 1);
|
59
|
|
}
|
60
|
|
|
61
|
0
|
public Jdbc_Sql( String driverName, String url, String user, String passwd,
|
62
|
|
boolean debugResult, boolean debug)
|
63
|
|
throws SQLException {
|
64
|
0
|
this( driverName, url, user, passwd, debugResult, debug, null, 1);
|
65
|
|
}
|
66
|
|
|
67
|
0
|
public Jdbc_Sql( String driverName, String url, String user, String passwd, boolean debugResult,
|
68
|
|
boolean debug, PrintWriter logWriter, int nCons)
|
69
|
|
throws SQLException {
|
70
|
0
|
this.numCons = nCons;
|
71
|
0
|
this.debug = debugResult;
|
72
|
0
|
if( logWriter == null)
|
73
|
0
|
logWriter = Log.getWriter();
|
74
|
0
|
this.logWriter = logWriter;
|
75
|
0
|
this.driverName = driverName;
|
76
|
0
|
this.url = url;
|
77
|
0
|
this.user = user;
|
78
|
0
|
this.passwd = passwd;
|
79
|
0
|
this.debugLL = debug;
|
80
|
0
|
this.connections = new Vector();
|
81
|
0
|
for( int i = 0; i < nCons; i++)
|
82
|
0
|
connections.addElement( new DBConnection( driverName, url, user, passwd, debugLL,logWriter));
|
83
|
|
}
|
84
|
|
|
85
|
0
|
protected void finalize() {
|
86
|
0
|
closeAll();
|
87
|
|
}
|
88
|
|
|
89
|
0
|
public void closeAll() {
|
90
|
0
|
for( int i = 0; i < connections.size(); i++)
|
91
|
0
|
try {
|
92
|
0
|
((DBConnection)connections.elementAt( i)).closeConnection();
|
93
|
|
}catch( SQLException e) {
|
94
|
|
|
95
|
|
}
|
96
|
0
|
connections.removeAllElements();
|
97
|
|
}
|
98
|
|
|
99
|
|
|
100
|
|
|
101
|
|
|
102
|
0
|
public DBConnection getConnection()
|
103
|
|
throws SQLException {
|
104
|
0
|
return getConnection0();
|
105
|
|
}
|
106
|
|
|
107
|
|
|
108
|
|
|
109
|
|
|
110
|
0
|
public void putConnection( DBConnection con)
|
111
|
|
throws SQLException {
|
112
|
0
|
putConnection0( con);
|
113
|
|
}
|
114
|
|
|
115
|
|
|
116
|
|
|
117
|
|
|
118
|
0
|
public TransactionHandle getTransactionHandle()
|
119
|
|
throws SQLException {
|
120
|
0
|
return new TransactionHandle( this, getConnection());
|
121
|
|
}
|
122
|
|
|
123
|
|
|
124
|
|
|
125
|
|
|
126
|
0
|
public ResultSet executeRawQuery( String sqlstring)
|
127
|
|
throws SQLException {
|
128
|
0
|
return executeRawQuery( null, sqlstring);
|
129
|
|
}
|
130
|
|
|
131
|
|
|
132
|
|
|
133
|
|
|
134
|
0
|
public ResultSet executeRawQuery( DBConnection con0, String sqlstring)
|
135
|
|
throws SQLException {
|
136
|
0
|
ResultSet rs = null;
|
137
|
0
|
DBConnection con = con0;
|
138
|
0
|
try {
|
139
|
0
|
if( con == null)
|
140
|
0
|
con = defaultCon;
|
141
|
0
|
if( con == null || ! con.isValid())
|
142
|
0
|
con = defaultCon = getConnection();
|
143
|
0
|
return con.executeQuery( sqlstring);
|
144
|
|
} catch( SQLException ex){
|
145
|
0
|
logWriter.println( sqlstring);
|
146
|
0
|
ex.printStackTrace( logWriter);
|
147
|
0
|
throw ex;
|
148
|
|
}
|
149
|
|
}
|
150
|
|
|
151
|
|
|
152
|
|
|
153
|
|
|
154
|
0
|
public SQLVectorResult executeQuery( String sqlstring)
|
155
|
|
throws SQLException {
|
156
|
0
|
return executeQuery( null, sqlstring, -1);
|
157
|
|
}
|
158
|
|
|
159
|
|
|
160
|
|
|
161
|
|
|
162
|
0
|
public SQLVectorResult executeQuery( DBConnection con, String sqlstring)
|
163
|
|
throws SQLException {
|
164
|
0
|
return executeQuery( con, sqlstring, -1);
|
165
|
|
}
|
166
|
|
|
167
|
|
|
168
|
|
|
169
|
|
|
170
|
|
|
171
|
|
|
172
|
|
|
173
|
0
|
public SQLVectorResult executeQuery( String sqlstring, int maxcount)
|
174
|
|
throws SQLException {
|
175
|
0
|
return executeQuery( null, sqlstring, maxcount);
|
176
|
|
}
|
177
|
|
|
178
|
|
|
179
|
|
|
180
|
|
|
181
|
|
|
182
|
|
|
183
|
|
|
184
|
0
|
public SQLVectorResult executeQuery( DBConnection con0, String sqlstring, int maxcount)
|
185
|
|
throws SQLException {
|
186
|
0
|
SQLVectorResult result = null;
|
187
|
0
|
ResultSet rs = null;
|
188
|
0
|
DBConnection con = con0;
|
189
|
0
|
try {
|
190
|
0
|
if( con == null)
|
191
|
0
|
con = getConnection();
|
192
|
0
|
result = new SQLVectorResult();
|
193
|
0
|
result.rows = new Vector();
|
194
|
0
|
rs = con.executeQuery( sqlstring);
|
195
|
|
|
196
|
0
|
ResultSetMetaData rsmd = rs.getMetaData();
|
197
|
0
|
int numCols = rsmd.getColumnCount();
|
198
|
0
|
result.columnName = new String[ numCols];
|
199
|
0
|
result.columnType = new int[ numCols];
|
200
|
0
|
for( int i=0; i < numCols ; i++){
|
201
|
0
|
result.columnName[i] = rsmd.getColumnName( i + 1);
|
202
|
0
|
result.columnType[i] = rsmd.getColumnType( i + 1);
|
203
|
0
|
if( debug)
|
204
|
0
|
logWriter.print( result.columnName[i] + ":" + result.columnType[i] + "\t ");
|
205
|
|
}
|
206
|
0
|
if( debug)
|
207
|
0
|
logWriter.println("");
|
208
|
0
|
while( maxcount != 0 && rs.next()){
|
209
|
0
|
if( --maxcount == 0)
|
210
|
0
|
result = new ExtSQLVectorResult( result, con, con0, rs);
|
211
|
0
|
Object[] row = new Object [numCols];
|
212
|
0
|
for( int i = 0 ; i < numCols ; i++){
|
213
|
0
|
if ((result.columnType[i] == java.sql.Types.LONGVARBINARY )||(result.columnType[i] == java.sql.Types.VARBINARY ))
|
214
|
|
|
215
|
0
|
row[i] = nmp.io.Utilities.unserialize( (byte[])rs.getObject(i+1));
|
216
|
|
else
|
217
|
0
|
row[i] = rs.getObject(i+1);
|
218
|
0
|
if( debug)
|
219
|
0
|
logWriter.print( row[i] + "\t ");
|
220
|
|
}
|
221
|
0
|
result.rows.addElement( row);
|
222
|
0
|
if( debug)
|
223
|
0
|
logWriter.println("");
|
224
|
|
}
|
225
|
|
|
226
|
0
|
return result;
|
227
|
|
} catch( IOException ex){
|
228
|
0
|
logWriter.println( sqlstring);
|
229
|
0
|
ex.printStackTrace( logWriter);
|
230
|
0
|
throw new SQLException( "Error unserializing object in executeQuery");
|
231
|
|
} catch( SQLException ex){
|
232
|
0
|
logWriter.println( sqlstring);
|
233
|
0
|
ex.printStackTrace( logWriter);
|
234
|
0
|
throw ex;
|
235
|
|
} finally {
|
236
|
0
|
try {
|
237
|
0
|
if( ( result == null || ! (result instanceof ExtSQLVectorResult)) && rs != null)
|
238
|
0
|
rs.close();
|
239
|
0
|
if( ! (result instanceof ExtSQLVectorResult) && con != null && con0 == null)
|
240
|
0
|
putConnection( con);
|
241
|
|
} catch( SQLException ex) {
|
242
|
|
|
243
|
|
}
|
244
|
|
}
|
245
|
|
}
|
246
|
|
|
247
|
|
|
248
|
|
|
249
|
|
|
250
|
0
|
public boolean executeQuery( SQLVectorResult result, int maxcount)
|
251
|
|
throws SQLException {
|
252
|
0
|
try {
|
253
|
0
|
if( result == null)
|
254
|
0
|
return false;
|
255
|
0
|
result.rows = new Vector();
|
256
|
0
|
if( !( result instanceof ExtSQLVectorResult))
|
257
|
0
|
return false;
|
258
|
0
|
ResultSet rs = ((ExtSQLVectorResult)result).rs;
|
259
|
0
|
if( rs == null) {
|
260
|
0
|
try {
|
261
|
0
|
if( ((ExtSQLVectorResult)result).rs != null)
|
262
|
0
|
((ExtSQLVectorResult)result).rs.close();
|
263
|
0
|
((ExtSQLVectorResult)result).rs = null;
|
264
|
0
|
if( ((ExtSQLVectorResult)result).con0 == null) {
|
265
|
0
|
putConnection( ((ExtSQLVectorResult)result).con);
|
266
|
0
|
((ExtSQLVectorResult)result).con = null;
|
267
|
|
}
|
268
|
|
} catch( SQLException ex) {
|
269
|
|
|
270
|
|
}
|
271
|
0
|
return false;
|
272
|
|
}
|
273
|
|
|
274
|
0
|
ResultSetMetaData rsmd = rs.getMetaData();
|
275
|
0
|
int numCols = rsmd.getColumnCount();
|
276
|
0
|
do{
|
277
|
0
|
Object[] row = new Object [numCols];
|
278
|
0
|
for( int i = 0 ; i < numCols ; i++){
|
279
|
0
|
if ((result.columnType[i] == java.sql.Types.LONGVARBINARY )||(result.columnType[i] == java.sql.Types.VARBINARY ))
|
280
|
|
|
281
|
0
|
row[i] = nmp.io.Utilities.unserialize( (byte[])rs.getObject(i+1));
|
282
|
|
else
|
283
|
0
|
row[i] = rs.getObject(i+1);
|
284
|
0
|
if( debug)
|
285
|
0
|
logWriter.print( row[i] + "\t ");
|
286
|
|
}
|
287
|
0
|
result.rows.addElement( row);
|
288
|
0
|
if( debug)
|
289
|
0
|
logWriter.println("");
|
290
|
0
|
}while( --maxcount != 0 && rs.next());
|
291
|
0
|
if( maxcount != 0) {
|
292
|
0
|
if( rs != null)
|
293
|
0
|
rs.close();
|
294
|
0
|
((ExtSQLVectorResult)result).rs = null;
|
295
|
0
|
if( ((ExtSQLVectorResult)result).con0 == null) {
|
296
|
0
|
putConnection( ((ExtSQLVectorResult)result).con);
|
297
|
0
|
((ExtSQLVectorResult)result).con = null;
|
298
|
|
}
|
299
|
|
}
|
300
|
|
} catch( IOException ex){
|
301
|
0
|
try {
|
302
|
0
|
if( ((ExtSQLVectorResult)result).rs != null)
|
303
|
0
|
((ExtSQLVectorResult)result).rs.close();
|
304
|
0
|
((ExtSQLVectorResult)result).rs = null;
|
305
|
0
|
if( ((ExtSQLVectorResult)result).con0 == null) {
|
306
|
0
|
putConnection( ((ExtSQLVectorResult)result).con);
|
307
|
0
|
((ExtSQLVectorResult)result).con = null;
|
308
|
|
}
|
309
|
|
} catch( SQLException ex2) {
|
310
|
|
|
311
|
|
}
|
312
|
0
|
throw new SQLException( "Error unserializing object in executeQuery");
|
313
|
|
} catch( SQLException ex) {
|
314
|
0
|
try {
|
315
|
0
|
if( ((ExtSQLVectorResult)result).rs != null)
|
316
|
0
|
((ExtSQLVectorResult)result).rs.close();
|
317
|
0
|
((ExtSQLVectorResult)result).rs = null;
|
318
|
0
|
if( ((ExtSQLVectorResult)result).con0 == null) {
|
319
|
0
|
putConnection( ((ExtSQLVectorResult)result).con);
|
320
|
0
|
((ExtSQLVectorResult)result).con = null;
|
321
|
|
}
|
322
|
|
} catch( SQLException ex2) {
|
323
|
|
|
324
|
|
}
|
325
|
0
|
throw ex;
|
326
|
|
}
|
327
|
0
|
return true;
|
328
|
|
}
|
329
|
|
|
330
|
|
|
331
|
0
|
public int executeUpdate(String sqlstring)
|
332
|
|
throws SQLException {
|
333
|
0
|
return executeUpdate( null, sqlstring);
|
334
|
|
}
|
335
|
|
|
336
|
0
|
public int executeUpdate(DBConnection con0, String sqlstring)
|
337
|
|
throws SQLException {
|
338
|
0
|
DBConnection con = con0;
|
339
|
0
|
try {
|
340
|
0
|
if( con == null)
|
341
|
0
|
con = getConnection();
|
342
|
0
|
int result = con.executeUpdate(sqlstring);
|
343
|
0
|
if( debug)
|
344
|
0
|
logWriter.println( "Resultado : " + result);
|
345
|
0
|
return result;
|
346
|
|
} catch( SQLException ex){
|
347
|
0
|
logWriter.println( sqlstring);
|
348
|
0
|
ex.printStackTrace( logWriter);
|
349
|
0
|
throw ex;
|
350
|
|
} finally {
|
351
|
0
|
try {
|
352
|
0
|
if( con != null && con0 == null) putConnection( con);
|
353
|
|
} catch( SQLException ex2) {
|
354
|
|
|
355
|
|
}
|
356
|
|
}
|
357
|
|
}
|
358
|
|
|
359
|
0
|
public int executeUpdate(String sqlstring, java.util.Vector values)
|
360
|
|
throws SQLException {
|
361
|
0
|
return executeUpdate( null, sqlstring, values);
|
362
|
|
}
|
363
|
|
|
364
|
0
|
public int executeUpdate(DBConnection con0, String sqlstring, java.util.Vector values)
|
365
|
|
throws SQLException {
|
366
|
0
|
DBConnection con = con0;
|
367
|
0
|
try {
|
368
|
0
|
if( con == null)
|
369
|
0
|
con = getConnection();
|
370
|
0
|
int result = con.executeUpdate(sqlstring, values);
|
371
|
0
|
if( debug)
|
372
|
0
|
logWriter.println( "Resultado : " + result);
|
373
|
0
|
return result;
|
374
|
|
} catch( SQLException ex){
|
375
|
0
|
logWriter.println( sqlstring);
|
376
|
0
|
ex.printStackTrace( logWriter);
|
377
|
0
|
throw ex;
|
378
|
|
} finally {
|
379
|
0
|
try {
|
380
|
0
|
if( con != null && con0 == null) putConnection( con);
|
381
|
|
} catch( SQLException ex2) {
|
382
|
|
|
383
|
|
}
|
384
|
|
}
|
385
|
|
}
|
386
|
|
|
387
|
0
|
public String nativeSQL( String command)
|
388
|
|
throws SQLException{
|
389
|
0
|
DBConnection con = null;
|
390
|
0
|
try {
|
391
|
0
|
con = getConnection();
|
392
|
0
|
return con.nativeSQL( command);
|
393
|
|
} catch( SQLException ex) {
|
394
|
0
|
return "error";
|
395
|
|
}finally {
|
396
|
0
|
if( con != null) putConnection( con);
|
397
|
|
}
|
398
|
|
}
|
399
|
|
|
400
|
|
}
|
401
|
|
|