KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > driver > Statement


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2005 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): Vadim Kassin, Jean-Bernard van Zuylen.
23  */

24
25 package org.objectweb.cjdbc.driver;
26
27 import java.sql.BatchUpdateException JavaDoc;
28 import java.sql.ResultSet JavaDoc;
29 import java.sql.SQLException JavaDoc;
30 import java.sql.SQLWarning JavaDoc;
31 import java.util.Vector JavaDoc;
32
33 import org.objectweb.cjdbc.common.exceptions.NotImplementedException;
34 import org.objectweb.cjdbc.common.sql.AbstractWriteRequest;
35 import org.objectweb.cjdbc.common.sql.AlterRequest;
36 import org.objectweb.cjdbc.common.sql.CreateRequest;
37 import org.objectweb.cjdbc.common.sql.DeleteRequest;
38 import org.objectweb.cjdbc.common.sql.DropRequest;
39 import org.objectweb.cjdbc.common.sql.InsertRequest;
40 import org.objectweb.cjdbc.common.sql.SelectRequest;
41 import org.objectweb.cjdbc.common.sql.StoredProcedure;
42 import org.objectweb.cjdbc.common.sql.UpdateRequest;
43
44 /**
45  * A <code>Statement</code> object is used for executing a static SQL
46  * statement and obtaining the results produced by it.
47  * <p>
48  * Only one <code>ResultSet</code> per <code>Statement</code> can be open at
49  * any point in time. Therefore, if the reading of one <code>ResultSet</code>
50  * is interleaved with the reading of another, each must have been generated by
51  * different <code>Statements</code>. All <code>Statements</code> execute
52  * methods implicitly close a statement's current <code>ResultSet</code> if an
53  * open one exists.
54  *
55  * @see java.sql.Statement
56  * @see DriverResultSet
57  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
58  * @author <a HREF="mailto:vadim@kase.kz">Vadim Kassin </a>
59  * @author <a HREF="mailto:jbvanzuylen@transwide.com">Jean-Bernard van Zuylen
60  * </a>
61  * @version 1.0
62  */

63 public class Statement implements java.sql.Statement JavaDoc
64 {
65   /** The connection that created us */
66   protected Connection connection = null;
67
68   /** Vector for batch commands */
69   protected Vector JavaDoc batch = null;
70
71   /** The warnings chain */
72   private SQLWarning JavaDoc warnings = null;
73
74   /** The current result for a read request */
75   protected ResultSet JavaDoc result = null;
76
77   /** The update count for a write request */
78   protected int updateCount = -1;
79
80   /** Query timeout in seconds (0 means no timeout) */
81   private int timeout = 0;
82
83   /** Default ResultSet fetch size */
84   private int fetchSize = 0;
85   /** Cursor name used jointly with fetch size */
86   private String JavaDoc cursorName;
87
88   /** Type of the ResultSet defaults to TYPE_FORWARD_ONLY */
89   private int resultSetType = ResultSet.TYPE_FORWARD_ONLY;
90
91   /** ResultSet Concurrency defaults to CONCUR_READ_ONLY */
92   private int resultSetConcurrency = ResultSet.CONCUR_READ_ONLY;
93
94   /** Maximum field size (unused) */
95   private int maxFieldSize = 0;
96
97   /** Maximum number of rows */
98   private int maxRows = 0;
99
100   /**
101    * Direction for fetching rows from ResultSet (note that this hint is
102    * currently ignored
103    */

104   private int fetchDirection = ResultSet.FETCH_FORWARD;
105
106   /**
107    * Should the driver to escape processing before sending to the DB?
108    */

109   protected boolean escapeProcessing = true;
110
111   /** Auto generated keys */
112   protected ResultSet JavaDoc generatedKeys = null;
113   protected int generatedKeysFlag = java.sql.Statement.NO_GENERATED_KEYS;
114
115   /**
116    * Creates a new <code>Statement</code> instance.
117    *
118    * @param c the <code>Connection</code> that created us
119    */

120   public Statement(Connection c)
121   {
122     connection = c;
123   }
124
125   /**
126    * Adds sql to the current list of commands.
127    *
128    * @param sql an SQL statement that returns an update count (INSERT or UPDATE)
129    * @exception SQLException if an error occurs
130    */

131   public synchronized void addBatch(String JavaDoc sql) throws SQLException JavaDoc
132   {
133     if (batch == null)
134       batch = new Vector JavaDoc();
135     batch.addElement(sql.trim());
136   }
137
138   /**
139    * Could be use by one thread to cancel a statement that is being executed by
140    * another thread. We don't support that for instance.
141    *
142    * @exception SQLException if an error occurs
143    */

144   public void cancel() throws SQLException JavaDoc
145   {
146     throw new NotImplementedException("cancel()");
147   }
148
149   /**
150    * Empties the current list of commands.
151    *
152    * @exception SQLException if an error occurs
153    */

154   public void clearBatch() throws SQLException JavaDoc
155   {
156     if (batch != null)
157       batch.removeAllElements();
158   }
159
160   /**
161    * After this call, <code>getWarnings</code> returns <code>null</code>
162    * until a new warning is reported for this <code>Statement</code>.
163    *
164    * @exception SQLException if a database access error occurs (why?)
165    */

166   public void clearWarnings() throws SQLException JavaDoc
167   {
168     warnings = null;
169   }
170
171   /**
172    * Execute a batch of commands
173    *
174    * @return an array containing update count that corresponding to the commands
175    * that executed successfully
176    * @exception BatchUpdateException if an error occurs on one statement (the
177    * number of updated rows for the successfully executed
178    * statements can be found in
179    * BatchUpdateException.getUpdateCounts())
180    */

181   public int[] executeBatch() throws BatchUpdateException JavaDoc
182   {
183     if (batch == null || batch.isEmpty())
184       return new int[0];
185
186     int size = batch.size();
187     int[] batchResult = new int[size];
188     int i = 0;
189
190     try
191     {
192       for (i = 0; i < size; i++)
193         batchResult[i] = this.executeUpdate((String JavaDoc) batch.elementAt(i));
194       return batchResult;
195     }
196     catch (SQLException JavaDoc e)
197     {
198       String JavaDoc message = "Batch failed for request " + i + ": "
199           + batch.elementAt(i) + " (" + e + ")";
200
201       int[] updateCounts = new int[i];
202       System.arraycopy(batchResult, 0, updateCounts, 0, i);
203
204       throw new BatchUpdateException JavaDoc(message, updateCounts);
205     }
206     finally
207     {
208       batch.removeAllElements();
209     }
210   }
211
212   /**
213    * In many cases, it is desirable to immediately release a Statement's
214    * database and JDBC resources instead of waiting for this to happen when it
215    * is automatically closed. The close method provides this immediate release.
216    * <p>
217    * <B>Note: </B> A Statement is automatically closed when it is garbage
218    * collected. When a Statement is closed, its current ResultSet, if one
219    * exists, is also closed.
220    *
221    * @exception SQLException if a database access error occurs (why?)
222    */

223   public void close() throws SQLException JavaDoc
224   {
225     // Force the ResultSet to close
226
if (result != null)
227       result.close();
228
229     // Disasociate it from us (For Garbage Collection)
230
result = null;
231     connection = null;
232   }
233
234   /**
235    * Execute a SQL statement that may return multiple results.
236    *
237    * @param sql any SQL statement
238    * @return true if the result is a ResultSet or false if it is an integer
239    * @exception SQLException if an error occurs
240    */

241   public boolean execute(String JavaDoc sql) throws SQLException JavaDoc
242   {
243     int start = 0;
244     try
245     {
246       // Ignore any leading parenthesis
247
while (sql.charAt(start) == '(')
248         start++;
249     }
250     catch (IndexOutOfBoundsException JavaDoc e)
251     {
252       // Probably a buggy request, let it go through and let thefollowing code
253
// to report an accurate error if any.
254
start = 0;
255     }
256
257     if (sql.regionMatches(true, start, "select", 0, 6)
258         || (sql.regionMatches(true, start, "{call", 0, 5))
259         || (sql.regionMatches(true, start, "values", 0, 6)))
260     {
261       result = executeQuery(sql);
262       return true;
263     }
264     else
265     {
266       updateCount = executeUpdate(sql);
267       return false;
268     }
269   }
270
271   /**
272    * Execute a SQL statement that returns a single ResultSet
273    *
274    * @param sql typically a static SQL <code>SELECT</code> statement
275    * @return a ResulSet that contains the data produced by the query
276    * @exception SQLException if a database access error occurs
277    */

278   public java.sql.ResultSet JavaDoc executeQuery(String JavaDoc sql) throws SQLException JavaDoc
279   {
280     return executeQuery(null, sql.trim());
281   }
282
283   /**
284    * Execute a SQL statement that returns a single ResultSet
285    *
286    * @param sqlSkeleton the SQL request squeleton or null
287    * @param sqlQuery typically a static SQL <code>SELECT</code> statement that
288    * is already trimed
289    * @return a ResulSet that contains the data produced by the query
290    * @exception SQLException if a database access error occurs or if this
291    * statement is closed
292    */

293   protected java.sql.ResultSet JavaDoc executeQuery(String JavaDoc sqlSkeleton, String JavaDoc sqlQuery)
294       throws SQLException JavaDoc
295   {
296     if (isClosed())
297     {
298       throw new SQLException JavaDoc("Unable to execute query on a closed statement");
299     }
300     updateCount = -1; // invalidate the last write result
301
if (result != null)
302     { // Discard the previous result
303
result.close();
304       result = null;
305     }
306
307     if (sqlQuery.regionMatches(true, 0, "{call", 0, 5))
308     {
309       StoredProcedure proc = new StoredProcedure(sqlQuery, escapeProcessing,
310           timeout, Connection.LINE_SEPARATOR, true /* isRead */);
311       if (connection.controllerNeedsSqlSkeleton || !connection.isDriverProcessed())
312         proc.setSqlSkeleton(sqlSkeleton);
313       proc.setMaxRows(maxRows);
314       proc.setFetchSize(fetchSize);
315       proc.setCursorName(cursorName);
316       result = connection.execReadStoredProcedure(proc);
317     }
318     else
319     {
320       SelectRequest request = new SelectRequest(sqlQuery, escapeProcessing,
321           timeout, Connection.LINE_SEPARATOR);
322       if (connection.controllerNeedsSqlSkeleton || !connection.isDriverProcessed())
323         request.setSqlSkeleton(sqlSkeleton);
324       request.setMaxRows(maxRows);
325       request.setFetchSize(fetchSize);
326       request.setCursorName(cursorName);
327       result = connection.execReadRequest(request);
328     }
329
330     if (result instanceof DriverResultSet)
331       ((DriverResultSet) result).setStatement(this);
332     return result;
333   }
334
335   /**
336    * Execute a SQL INSERT, UPDATE or DELETE statement. In addition SQL
337    * statements that return nothing such as SQL DDL statements can be executed
338    *
339    * @param sql a SQL statement
340    * @return either a row count, or 0 for SQL commands
341    * @exception SQLException if a database access error occurs
342    */

343   public int executeUpdate(String JavaDoc sql) throws SQLException JavaDoc
344   {
345     return executeUpdateWithSkeleton(null, sql.trim());
346   }
347
348   /**
349    * Execute a SQL INSERT, UPDATE or DELETE statement. In addition SQL
350    * statements that return nothing such as SQL DDL statements can be executed
351    *
352    * @param sqlSkeleton the SQL request squeleton or null
353    * @param sqlQuery a static SQL statement that is already trimed
354    * @return either a row count, or 0 for SQL commands
355    * @exception SQLException if a database access error occurs or if this
356    * statement is closed
357    */

358   protected int executeUpdateWithSkeleton(String JavaDoc sqlSkeleton, String JavaDoc sqlQuery)
359       throws SQLException JavaDoc
360   {
361     if (isClosed())
362     {
363       throw new SQLException JavaDoc("Unable to execute query on a closed statement");
364     }
365     if (result != null)
366     { // Discard the previous result
367
result.close();
368       result = null;
369     }
370
371     // Check that the command starts with
372
// insert/update/delete/create/drop/{call
373
String JavaDoc lower = sqlQuery.substring(0,
374         6 < sqlQuery.length() ? 6 : sqlQuery.length()).toLowerCase();
375     AbstractWriteRequest request;
376     if (lower.equals("insert"))
377       request = new InsertRequest(sqlQuery, escapeProcessing, timeout,
378           Connection.LINE_SEPARATOR,
379           (Statement.RETURN_GENERATED_KEYS == generatedKeysFlag) /* isRead */);
380     else if (lower.equals("update"))
381       request = new UpdateRequest(sqlQuery, escapeProcessing, timeout,
382           Connection.LINE_SEPARATOR);
383     else if (lower.equals("delete"))
384       request = new DeleteRequest(sqlQuery, escapeProcessing, timeout,
385           Connection.LINE_SEPARATOR);
386     else if (lower.startsWith("create"))
387       request = new CreateRequest(sqlQuery, escapeProcessing, timeout,
388           Connection.LINE_SEPARATOR);
389     else if (lower.startsWith("drop"))
390       request = new DropRequest(sqlQuery, escapeProcessing, timeout,
391           Connection.LINE_SEPARATOR);
392     else if (lower.startsWith("alter"))
393       request = new AlterRequest(sqlQuery, escapeProcessing, timeout,
394           Connection.LINE_SEPARATOR);
395     else if (lower.startsWith("{call"))
396     { // Call stored procedure and return
397
StoredProcedure proc = new StoredProcedure(sqlQuery, escapeProcessing,
398           timeout, Connection.LINE_SEPARATOR,
399           (Statement.RETURN_GENERATED_KEYS == generatedKeysFlag) /* isRead */);
400       if (connection.controllerNeedsSqlSkeleton || !connection.isDriverProcessed())
401         proc.setSqlSkeleton(sqlSkeleton);
402       updateCount = connection.execWriteStoredProcedure(proc);
403       return updateCount;
404     }
405     else if (lower.startsWith("}call"))
406     { // Call stored procedure and return. This hack is used to allow someone to
407
// use execute() to call a write stored procedure.
408
StoredProcedure proc = new StoredProcedure("{" + sqlQuery.substring(1),
409           escapeProcessing, timeout, Connection.LINE_SEPARATOR,
410           (Statement.RETURN_GENERATED_KEYS == generatedKeysFlag) /* isRead */);
411       if (connection.controllerNeedsSqlSkeleton || !connection.isDriverProcessed())
412         proc.setSqlSkeleton(sqlSkeleton);
413       updateCount = connection.execWriteStoredProcedure(proc);
414       return updateCount;
415     }
416     else
417       throw new SQLException JavaDoc(
418           "executeUpdate only accepts statements starting with insert/update/delete/create/drop/{call ("
419               + sqlQuery + ")");
420
421     if (connection.controllerNeedsSqlSkeleton || !connection.isDriverProcessed())
422       request.setSqlSkeleton(sqlSkeleton);
423
424     if (generatedKeysFlag == Statement.RETURN_GENERATED_KEYS)
425     { // Get the auto generated key back
426
generatedKeys = connection.execWriteRequestWithKeys(request);
427
428       /*
429        * Usually it is one autoincrement field and one generated key but if it
430        * is not acceptable - better way to make another function for return
431        * count of updates Or leave execWriteRequestWithKeys to return count and
432        * add function for return ResultSet
433        */

434       return 1;
435     }
436     else
437     { // No generated keys
438
updateCount = connection.execWriteRequest(request);
439       return updateCount;
440     }
441   }
442
443   /**
444    * Retrieve the connection that created this Statement object
445    *
446    * @return a <code>java.sql.Connection</code> object
447    * @exception SQLException never
448    */

449   public java.sql.Connection JavaDoc getConnection() throws SQLException JavaDoc
450   {
451     return connection;
452   }
453
454   /**
455    * @see java.sql.Statement#getFetchDirection()
456    */

457   public int getFetchDirection() throws SQLException JavaDoc
458   {
459     return fetchDirection;
460   }
461
462   /**
463    * @see java.sql.Statement#getFetchSize()
464    */

465   public int getFetchSize() throws SQLException JavaDoc
466   {
467     return fetchSize;
468   }
469
470   /**
471    * The maxFieldSize limit (in bytes) is the maximum amount of data returned
472    * for any column value; it only applies to <code>BINARY</code>,
473    * <code>VARBINARY</code>,<code>LONGVARBINARY</code>,<code>CHAR</code>,
474    * <code>VARCHAR</code> and <code>LONGVARCHAR</code> columns. If the limit
475    * is exceeded, the excess data is silently discarded.
476    * <p>
477    * <b>Note: </b> We don't do anything with this value yet.
478    *
479    * @return the current max column size limit; zero means unlimited
480    * @exception SQLException if a database access error occurs
481    */

482   public int getMaxFieldSize() throws SQLException JavaDoc
483   {
484     return maxFieldSize;
485   }
486
487   /**
488    * The maxRows limit is set to limit the number of rows that any
489    * <code>ResultSet</code> can contain. If the limit is exceeded, the excess
490    * rows are silently dropped.
491    *
492    * @return the current maximum row limit; zero means unlimited
493    * @exception SQLException if a database access error occurs
494    */

495   public int getMaxRows() throws SQLException JavaDoc
496   {
497     return maxRows;
498   }
499
500   /**
501    * Multiple results are not suppoted so this method always return false and
502    * reset the update count to -1. Any open ResultSet is implicitly closed.
503    *
504    * @return false
505    * @exception SQLException if an error occurs
506    */

507   public boolean getMoreResults() throws SQLException JavaDoc
508   {
509     if (result != null)
510       result.close();
511     updateCount = -1;
512     return false;
513   }
514
515   /**
516    * The queryTimeout limit is the number of seconds the driver will wait for a
517    * Statement to execute. If the limit is exceeded, a <code>SQLException</code>
518    * is thrown.
519    *
520    * @return the current query timeout limit in seconds; 0 = unlimited
521    * @exception SQLException if a database access error occurs
522    */

523   public int getQueryTimeout() throws SQLException JavaDoc
524   {
525     return timeout;
526   }
527
528   /**
529    * Returns the current result as a <code>ResultSet</code>.
530    *
531    * @return the current result set; null if there are no more
532    * @exception SQLException never
533    */

534   public java.sql.ResultSet JavaDoc getResultSet() throws SQLException JavaDoc
535   {
536     return result;
537   }
538
539   /**
540    * Retrieve the concurrency mode for the <code>ResultSet</code>.
541    *
542    * @return <code>CONCUR_READ_ONLY</code> or <code>CONCUR_UPDATABLE</code>
543    * @exception SQLException never
544    */

545   public int getResultSetConcurrency() throws SQLException JavaDoc
546   {
547     return resultSetConcurrency;
548   }
549
550   /**
551    * Retrieve the type of the generated <code>ResultSet</code>.
552    *
553    * @return one of <code>TYPE_FORWARD_ONLY</code> or
554    * <code>TYPE_SCROLL_INSENSITIVE</code>
555    * @exception SQLException never
556    */

557   public int getResultSetType() throws SQLException JavaDoc
558   {
559     return resultSetType;
560   }
561
562   /**
563    * Returns the current result as an update count, if the result is a
564    * <code>ResultSet</code> or there are no more results, -1 is returned. It
565    * should only be called once per result.
566    *
567    * @return the current result as an update count.
568    * @exception SQLException if a database access error occurs
569    */

570   public int getUpdateCount() throws SQLException JavaDoc
571   {
572     return updateCount;
573   }
574
575   /**
576    * The first warning reported by calls on this Statement is returned. A
577    * Statement's execute methods clear its SQLWarning chain. Subsequent
578    * <code>Statement</code> warnings will be chained to this SQLWarning.
579    * <p>
580    * The Warning chain is automatically cleared each time a statement is
581    * (re)executed.
582    * <p>
583    * <B>Note: </B> if you are processing a <code>ResultSet</code> then any
584    * warnings associated with <code>ResultSet</code> reads will be chained on
585    * the <code>ResultSet</code> object.
586    *
587    * @return the first SQLWarning on null
588    * @exception SQLException if a database access error occurs
589    */

590   public SQLWarning JavaDoc getWarnings() throws SQLException JavaDoc
591   {
592     return warnings;
593   }
594
595   /**
596    * Defines the SQL cursor name that will be used by subsequent execute
597    * methods. This name can then be used in SQL positioned update/delete
598    * statements to identify the current row in the ResultSet generated by this
599    * statement. If a database doesn't support positioned update/delete, this
600    * method is a no-op.
601    * <p>
602    *
603    * @param name the new cursor name
604    * @exception SQLException not supported
605    */

606   public void setCursorName(String JavaDoc name) throws SQLException JavaDoc
607   {
608     cursorName = name;
609   }
610
611   /**
612    * If escape scanning is on (the default), the driver will do escape
613    * substitution before sending the SQL to the database.
614    *
615    * @param enable true to enable; false to disable
616    * @exception SQLException if a database access error occurs
617    */

618   public void setEscapeProcessing(boolean enable) throws SQLException JavaDoc
619   {
620     escapeProcessing = enable;
621   }
622
623   /**
624    * @see java.sql.Statement#setFetchDirection(int)
625    */

626   public void setFetchDirection(int direction) throws SQLException JavaDoc
627   {
628     if ((direction == ResultSet.FETCH_FORWARD)
629         || (direction == ResultSet.FETCH_REVERSE)
630         || (direction == ResultSet.FETCH_UNKNOWN))
631       this.fetchDirection = direction;
632     else
633       throw new SQLException JavaDoc("Unsupported direction " + direction
634           + " in setFetchDirection");
635   }
636
637   /**
638    * Set the default fetch size for the produced ResultSet.
639    *
640    * @param rows number of rows that should be fetched from the database
641    * @exception SQLException if a database access error occurs or the condition
642    * 0 <= size <= this.getMaxRows is not satisfied
643    */

644   public void setFetchSize(int rows) throws SQLException JavaDoc
645   {
646     if (rows < 0
647     // The spec forgets the case maxRows = 0.
648
|| 0 < maxRows && maxRows < rows)
649     {
650       throw new SQLException JavaDoc("Invalid fetch size value: " + rows);
651     }
652     // It also forgets the case where maxRows is set < fetchSize AFTERwards,
653
// but we don't care about it.
654

655     fetchSize = rows;
656   }
657
658   /**
659    * Sets the <code>maxFieldSize</code>.
660    *
661    * @param max the new max column size limit; 0 means unlimited
662    * @exception SQLException if a database access error occurs or the condition
663    * max >= 0 is not satisfied
664    */

665   public void setMaxFieldSize(int max) throws SQLException JavaDoc
666   {
667     if (max < 0)
668     {
669       throw new SQLException JavaDoc("Invalid max field size value: " + max);
670     }
671     maxFieldSize = max;
672   }
673
674   /**
675    * Sets the maximum number of rows that any <code>ResultSet</code> can
676    * contain.
677    *
678    * @param max the new max rows limit; 0 means unlimited
679    * @exception SQLException if a database access error occurs or the condition
680    * max >= 0 is not satisfied
681    */

682   public void setMaxRows(int max) throws SQLException JavaDoc
683   {
684     if (max < 0)
685     {
686       throw new SQLException JavaDoc("Invalid max rows limit: " + max);
687     }
688     // this may break fetchSize <= maxRows
689
maxRows = max;
690   }
691
692   /**
693    * Sets the number of seconds the driver will wait for a
694    * <code>Statement</code> object to execute.
695    *
696    * @param seconds the new query timeout limit in seconds; 0 means no timeout
697    * @exception SQLException if a database access error occurs or the condition
698    * seconds >= 0 is not satisfied
699    */

700   public void setQueryTimeout(int seconds) throws SQLException JavaDoc
701   {
702     if (seconds < 0)
703     {
704       throw new SQLException JavaDoc("Invalid query timeout value: " + seconds);
705     }
706     timeout = seconds;
707   }
708
709   /**
710    * @param value an <code>int</code> value
711    * @exception SQLException if an error occurs
712    */

713   public void setResultSetConcurrency(int value) throws SQLException JavaDoc
714   {
715     switch (value)
716     {
717       case ResultSet.CONCUR_READ_ONLY :
718       case ResultSet.CONCUR_UPDATABLE :
719         resultSetConcurrency = value;
720         break;
721       default :
722         throw new SQLException JavaDoc("Invalid ResultSet " + "concurrency mode: "
723             + value);
724     }
725   }
726
727   /**
728    * @param value an <code>int</code> value
729    * @exception SQLException if an error occurs
730    */

731   public void setResultSetType(int value) throws SQLException JavaDoc
732   {
733     switch (value)
734     {
735       case ResultSet.TYPE_FORWARD_ONLY :
736       case ResultSet.TYPE_SCROLL_INSENSITIVE :
737         resultSetType = value;
738         break;
739       case ResultSet.TYPE_SCROLL_SENSITIVE :
740         throw new SQLException JavaDoc(
741             "TYPE_SCROLL_SENSITIVE is not a supported ResultSet type");
742       default :
743         throw new SQLException JavaDoc("Invalid ResultSet type");
744     }
745   }
746
747   // --------------------------JDBC 3.0-----------------------------
748

749   /**
750    * Moves to this <code>Statement</code> object's next result, deals with any
751    * current <code>ResultSet</code> object(s) according to the instructions
752    * specified by the given flag, and returns <code>true</code> if the next
753    * result is a <code>ResultSet</code> object.
754    * <p>
755    * There are no more results when the following is <code>true</code>:
756    *
757    * <pre>
758    *
759    *
760    *
761    *
762    *
763    *
764    * (!getMoreResults() &amp;&amp; (getUpdateCount() == -1)
765    *
766    *
767    *
768    *
769    *
770    *
771    * </pre>
772    *
773    * @param current one of the following <code>Statement</code> constants
774    * indicating what should happen to current <code>ResultSet</code>
775    * objects obtained using the method
776    * <code>getResultSet</code: <code>CLOSE_CURRENT_RESULT</code>,
777    * <code>KEEP_CURRENT_RESULT</code>, or <code>CLOSE_ALL_RESULTS</code>
778    * @return <code>true</code> if the next result is a <code>ResultSet</code>
779    * object; <code>false</code> if it is an update count or there are
780    * no more results
781    * @exception SQLException if a database access error occurs
782    * @since JDK 1.4
783    * @see #execute(String)
784    */

785   public boolean getMoreResults(int current) throws SQLException JavaDoc
786   {
787     throw new NotImplementedException("getMoreResults");
788   }
789
790   /**
791    * Retrieves any auto-generated keys created as a result of executing this
792    * <code>Statement</code> object. If this <code>Statement</code> object
793    * did not generate any keys, an empty <code>ResultSet</code> object is
794    * returned.
795    *
796    * @return a <code>ResultSet</code> object containing the auto-generated
797    * key(s) generated by the execution of this <code>Statement</code>
798    * object
799    * @exception SQLException if a database access error occurs
800    * @since JDK 1.4
801    */

802   public java.sql.ResultSet JavaDoc getGeneratedKeys() throws SQLException JavaDoc
803   {
804     return generatedKeys;
805   }
806
807   /**
808    * Executes the given SQL statement and signals the driver with the given flag
809    * about whether the auto-generated keys produced by this
810    * <code>Statement</code> object should be made available for retrieval.
811    *
812    * @param sql must be an SQL <code>INSERT</code>,<code>UPDATE</code> or
813    * <code>DELETE</code> statement or an SQL statement that returns
814    * nothing
815    * @param autoGeneratedKeys a flag indicating whether auto-generated keys
816    * should be made available for retrieval; one of the following
817    * constants: <code>Statement.RETURN_GENERATED_KEYS</code>
818    * <code>Statement.NO_GENERATED_KEYS</code>
819    * @return either the row count for <code>INSERT</code>,
820    * <code>UPDATE</code> or <code>DELETE</code> statements, or
821    * <code>0</code> for SQL statements that return nothing
822    * @exception SQLException if a database access error occurs, the given SQL
823    * statement returns a <code>ResultSet</code> object, or the
824    * given constant is not one of those allowed
825    * @since JDK 1.4
826    */

827   public int executeUpdate(String JavaDoc sql, int autoGeneratedKeys)
828       throws SQLException JavaDoc
829   {
830     generatedKeysFlag = autoGeneratedKeys;
831     return executeUpdate(sql);
832   }
833
834   /**
835    * Executes the given SQL statement and signals the driver that the
836    * auto-generated keys indicated in the given array should be made available
837    * for retrieval. The driver will ignore the array if the SQL statement is not
838    * an <code>INSERT</code> statement.
839    *
840    * @param sql an SQL <code>INSERT</code>,<code>UPDATE</code> or
841    * <code>DELETE</code> statement or an SQL statement that returns
842    * nothing, such as an SQL DDL statement
843    * @param columnIndexes an array of column indexes indicating the columns that
844    * should be returned from the inserted row
845    * @return either the row count for <code>INSERT</code>,
846    * <code>UPDATE</code>, or <code>DELETE</code> statements, or 0
847    * for SQL statements that return nothing
848    * @exception SQLException if a database access error occurs or the SQL
849    * statement returns a <code>ResultSet</code> object
850    * @since JDK 1.4
851    */

852   public int executeUpdate(String JavaDoc sql, int[] columnIndexes) throws SQLException JavaDoc
853   {
854     throw new NotImplementedException("executeUpdate");
855   }
856
857   /**
858    * Executes the given SQL statement and signals the driver that the
859    * auto-generated keys indicated in the given array should be made available
860    * for retrieval. The driver will ignore the array if the SQL statement is not
861    * an <code>INSERT</code> statement.
862    *
863    * @param sql an SQL <code>INSERT</code>,<code>UPDATE</code> or
864    * <code>DELETE</code> statement or an SQL statement that returns
865    * nothing
866    * @param columnNames an array of the names of the columns that should be
867    * returned from the inserted row
868    * @return either the row count for <code>INSERT</code>,
869    * <code>UPDATE</code>, or <code>DELETE</code> statements, or 0
870    * for SQL statements that return nothing
871    * @exception SQLException if a database access error occurs
872    * @since JDK 1.4
873    */

874   public int executeUpdate(String JavaDoc sql, String JavaDoc[] columnNames)
875       throws SQLException JavaDoc
876   {
877     throw new NotImplementedException("executeUpdate");
878   }
879
880   /**
881    * Executes the given SQL statement, which may return multiple results, and
882    * signals the driver that any auto-generated keys should be made available
883    * for retrieval. The driver will ignore this signal if the SQL statement is
884    * not an <code>INSERT</code> statement.
885    * <p>
886    * In some (uncommon) situations, a single SQL statement may return multiple
887    * result sets and/or update counts. Normally you can ignore this unless you
888    * are (1) executing a stored procedure that you know may return multiple
889    * results or (2) you are dynamically executing an unknown SQL string.
890    * <p>
891    * The <code>execute</code> method executes an SQL statement and indicates
892    * the form of the first result. You must then use the methods
893    * <code>getResultSet</code> or <code>getUpdateCount</code> to retrieve
894    * the result, and <code>getMoreResults</code> to move to any subsequent
895    * result(s).
896    *
897    * @param sql any SQL statement
898    * @param autoGeneratedKeys a constant indicating whether auto-generated keys
899    * should be made available for retrieval using the method
900    * <code>getGeneratedKeys</code>; one of the following constants:
901    * <code>Statement.RETURN_GENERATED_KEYS</code> or
902    * <code>Statement.NO_GENERATED_KEYS</code>
903    * @return <code>true</code> if the first result is a <code>ResultSet</code>
904    * object; <code>false</code> if it is an update count or there are
905    * no results
906    * @exception SQLException if a database access error occurs
907    * @see #getResultSet
908    * @see #getUpdateCount
909    * @see #getMoreResults()
910    * @see #getGeneratedKeys
911    * @since JDK 1.4
912    */

913   public boolean execute(String JavaDoc sql, int autoGeneratedKeys) throws SQLException JavaDoc
914   {
915     generatedKeysFlag = autoGeneratedKeys;
916     return execute(sql);
917   }
918
919   /**
920    * Executes the given SQL statement, which may return multiple results, and
921    * signals the driver that the auto-generated keys indicated in the given
922    * array should be made available for retrieval. This array contains the
923    * indexes of the columns in the target table that contain the auto-generated
924    * keys that should be made available. The driver will ignore the array if the
925    * given SQL statement is not an <code>INSERT</code> statement.
926    * <p>
927    * Under some (uncommon) situations, a single SQL statement may return
928    * multiple result sets and/or update counts. Normally you can ignore this
929    * unless you are (1) executing a stored procedure that you know may return
930    * multiple results or (2) you are dynamically executing an unknown SQL
931    * string.
932    * <p>
933    * The <code>execute</code> method executes an SQL statement and indicates
934    * the form of the first result. You must then use the methods
935    * <code>getResultSet</code> or <code>getUpdateCount</code> to retrieve
936    * the result, and <code>getMoreResults</code> to move to any subsequent
937    * result(s).
938    *
939    * @param sql any SQL statement
940    * @param columnIndexes an array of the indexes of the columns in the inserted
941    * row that should be made available for retrieval by a call to the
942    * method <code>getGeneratedKeys</code>
943    * @return <code>true</code> if the first result is a <code>ResultSet</code>
944    * object; <code>false</code> if it is an update count or there are
945    * no results
946    * @exception SQLException if a database access error occurs
947    * @see #getResultSet
948    * @see #getUpdateCount
949    * @see #getMoreResults()
950    * @since JDK 1.4
951    */

952   public boolean execute(String JavaDoc sql, int[] columnIndexes) throws SQLException JavaDoc
953   {
954     throw new NotImplementedException("execute");
955   }
956
957   /**
958    * Executes the given SQL statement, which may return multiple results, and
959    * signals the driver that the auto-generated keys indicated in the given
960    * array should be made available for retrieval. This array contains the names
961    * of the columns in the target table that contain the auto-generated keys
962    * that should be made available. The driver will ignore the array if the
963    * given SQL statement is not an <code>INSERT</code> statement.
964    * <p>
965    * In some (uncommon) situations, a single SQL statement may return multiple
966    * result sets and/or update counts. Normally you can ignore this unless you
967    * are (1) executing a stored procedure that you know may return multiple
968    * results or (2) you are dynamically executing an unknown SQL string.
969    * <p>
970    * The <code>execute</code> method executes an SQL statement and indicates
971    * the form of the first result. You must then use the methods
972    * <code>getResultSet</code> or <code>getUpdateCount</code> to retrieve
973    * the result, and <code>getMoreResults</code> to move to any subsequent
974    * result(s).
975    *
976    * @param sql any SQL statement
977    * @param columnNames an array of the names of the columns in the inserted row
978    * that should be made available for retrieval by a call to the
979    * method <code>getGeneratedKeys</code>
980    * @return <code>true</code> if the next result is a <code>ResultSet</code>
981    * object; <code>false</code> if it is an update count or there are
982    * no more results
983    * @exception SQLException if a database access error occurs
984    * @see #getResultSet
985    * @see #getUpdateCount
986    * @see #getMoreResults()
987    * @see #getGeneratedKeys
988    * @since JDK 1.4
989    */

990   public boolean execute(String JavaDoc sql, String JavaDoc[] columnNames) throws SQLException JavaDoc
991   {
992     throw new NotImplementedException("execute");
993   }
994
995   /**
996    * Retrieves the result set holdability for <code>ResultSet</code> objects
997    * generated by this <code>Statement</code> object.
998    *
999    * @return either <code>ResultSet.HOLD_CURSORS_OVER_COMMIT</code> or
1000   * <code>ResultSet.CLOSE_CURSORS_AT_COMMIT</code>
1001   * @exception SQLException if a database access error occurs
1002   * @since JDK 1.4
1003   */

1004  public int getResultSetHoldability() throws SQLException JavaDoc
1005  {
1006    throw new NotImplementedException("getResultSetHoldability");
1007  }
1008
1009  /**
1010   * Test if this statement is closed.
1011   *
1012   * @return <code>true</code> if this statement is closed
1013   */

1014  private boolean isClosed()
1015  {
1016    return (connection == null);
1017  }
1018}
Popular Tags