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