KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > rmijdbc > RJStatement


1
2 /**
3  * RmiJdbc client/server JDBC Driver
4  * (C) GIE Dyade (Groupe BULL / INRIA Research Center) 1997
5  * (C) ExperLog 1999-2000
6  *
7  * @version 1.0
8  * @author Pierre-Yves Gibello (pierreyves.gibello@experlog.com)
9  */

10
11 package org.objectweb.rmijdbc;
12
13 import java.sql.*;
14 import java.rmi.RemoteException JavaDoc;
15
16 /**
17  * <P>A Statement object is used for executing a static SQL statement
18  * and obtaining the results produced by it.
19  *
20  * <P>Only one ResultSet per Statement can be open at any point in
21  * time. Therefore, if the reading of one ResultSet is interleaved
22  * with the reading of another, each must have been generated by
23  * different Statements. All statement execute methods implicitly
24  * close a statment's current ResultSet if an open one exists.
25  *
26  * @see Connection#createStatement
27  * @see ResultSet
28  */

29 public class RJStatement implements java.sql.Statement JavaDoc, java.io.Serializable JavaDoc
30 {
31
32     RJStatementInterface rmiStatement_;
33     Connection connection_;
34
35     public RJStatement(RJStatementInterface s, Connection c) {
36       rmiStatement_ = s;
37       connection_ = c;
38     }
39
40     /**
41      * Execute a SQL statement that returns a single ResultSet.
42      *
43      * @param sql typically this is a static SQL SELECT statement
44      * @return a ResultSet that contains the data produced by the
45      * query; never null
46      */

47     public java.sql.ResultSet JavaDoc executeQuery(String JavaDoc sql) throws SQLException {
48       try {
49         return new RJResultSet(rmiStatement_.executeQuery(sql), this);
50       } catch(RemoteException JavaDoc e) {
51         throw new java.sql.SQLException JavaDoc(e.getMessage());
52       }
53     }
54
55     /**
56      * Execute a SQL INSERT, UPDATE or DELETE statement. In addition,
57      * SQL statements that return nothing such as SQL DDL statements
58      * can be executed.
59      *
60      * @param sql a SQL INSERT, UPDATE or DELETE statement or a SQL
61      * statement that returns nothing
62      * @return either the row count for INSERT, UPDATE or DELETE or 0
63      * for SQL statements that return nothing
64      */

65     public int executeUpdate(String JavaDoc sql) throws SQLException {
66       try {
67         return rmiStatement_.executeUpdate(sql);
68       } catch(RemoteException JavaDoc e) {
69         throw new java.sql.SQLException JavaDoc(e.getMessage());
70       }
71     }
72
73     /**
74      * In many cases, it is desirable to immediately release a
75      * Statements's database and JDBC resources instead of waiting for
76      * this to happen when it is automatically closed; the close
77      * method provides this immediate release.
78      *
79      * <P><B>Note:</B> A Statement is automatically closed when it is
80      * garbage collected. When a Statement is closed, its current
81      * ResultSet, if one exists, is also closed.
82      */

83     public void close() throws SQLException {
84       try {
85         rmiStatement_.close();
86       } catch(RemoteException JavaDoc e) {
87         throw new java.sql.SQLException JavaDoc(e.getMessage());
88       }
89     }
90
91     //----------------------------------------------------------------------
92

93     /**
94      * The maxFieldSize limit (in bytes) is the maximum amount of data
95      * returned for any column value; it only applies to BINARY,
96      * VARBINARY, LONGVARBINARY, CHAR, VARCHAR, and LONGVARCHAR
97      * columns. If the limit is exceeded, the excess data is silently
98      * discarded.
99      *
100      * @return the current max column size limit; zero means unlimited
101      */

102     public int getMaxFieldSize() throws SQLException {
103       try {
104         return rmiStatement_.getMaxFieldSize();
105       } catch(RemoteException JavaDoc e) {
106         throw new java.sql.SQLException JavaDoc(e.getMessage());
107       }
108     }
109     
110     /**
111      * The maxFieldSize limit (in bytes) is set to limit the size of
112      * data that can be returned for any column value; it only applies
113      * to BINARY, VARBINARY, LONGVARBINARY, CHAR, VARCHAR, and
114      * LONGVARCHAR fields. If the limit is exceeded, the excess data
115      * is silently discarded. For maximum portability use values
116      * greater than 256.
117      *
118      * @param max the new max column size limit; zero means unlimited
119      */

120     public void setMaxFieldSize(int max) throws SQLException {
121       try {
122         rmiStatement_.setMaxFieldSize(max);
123       } catch(RemoteException JavaDoc e) {
124         throw new java.sql.SQLException JavaDoc(e.getMessage());
125       }
126     }
127
128     /**
129      * The maxRows limit is the maximum number of rows that a
130      * ResultSet can contain. If the limit is exceeded, the excess
131      * rows are silently dropped.
132      *
133      * @return the current max row limit; zero means unlimited
134      */

135     public int getMaxRows() throws SQLException {
136       try {
137         return rmiStatement_.getMaxRows();
138       } catch(RemoteException JavaDoc e) {
139         throw new java.sql.SQLException JavaDoc(e.getMessage());
140       }
141     }
142
143     /**
144      * The maxRows limit is set to limit the number of rows that any
145      * ResultSet can contain. If the limit is exceeded, the excess
146      * rows are silently dropped.
147      *
148      * @param max the new max rows limit; zero means unlimited
149      */

150     public void setMaxRows(int max) throws SQLException {
151       try {
152         rmiStatement_.setMaxRows(max);
153       } catch(RemoteException JavaDoc e) {
154         throw new java.sql.SQLException JavaDoc(e.getMessage());
155       }
156     }
157
158     /**
159      * If escape scanning is on (the default), the driver will do
160      * escape substitution before sending the SQL to the database.
161      *
162      * @param enable true to enable; false to disable
163      */

164     public void setEscapeProcessing(boolean enable) throws SQLException {
165       try {
166         rmiStatement_.setEscapeProcessing(enable);
167       } catch(RemoteException JavaDoc e) {
168         throw new java.sql.SQLException JavaDoc(e.getMessage());
169       }
170     }
171
172     /**
173      * The queryTimeout limit is the number of seconds the driver will
174      * wait for a Statement to execute. If the limit is exceeded, a
175      * SQLException is thrown.
176      *
177      * @return the current query timeout limit in seconds; zero means unlimited
178      */

179     public int getQueryTimeout() throws SQLException {
180       try {
181         return rmiStatement_.getQueryTimeout();
182       } catch(RemoteException JavaDoc e) {
183         throw new java.sql.SQLException JavaDoc(e.getMessage());
184       }
185     }
186
187     /**
188      * The queryTimeout limit is the number of seconds the driver will
189      * wait for a Statement to execute. If the limit is exceeded, a
190      * SQLException is thrown.
191      *
192      * @param seconds the new query timeout limit in seconds; zero means unlimited
193      */

194     public void setQueryTimeout(int seconds) throws SQLException {
195       try {
196         rmiStatement_.setQueryTimeout(seconds);
197       } catch(RemoteException JavaDoc e) {
198         throw new java.sql.SQLException JavaDoc(e.getMessage());
199       }
200     }
201
202     /**
203      * Cancel can be used by one thread to cancel a statement that
204      * is being executed by another thread.
205      */

206     public void cancel() throws SQLException {
207       try {
208         rmiStatement_.cancel();
209       } catch(RemoteException JavaDoc e) {
210         throw new java.sql.SQLException JavaDoc(e.getMessage());
211       }
212     }
213
214     /**
215      * The first warning reported by calls on this Statement is
216      * returned. A Statment's execute methods clear its SQLWarning
217      * chain. Subsequent Statement warnings will be chained to this
218      * SQLWarning.
219      *
220      * <p>The warning chain is automatically cleared each time
221      * a statement is (re)executed.
222      *
223      * <P><B>Note:</B> If you are processing a ResultSet then any
224      * warnings associated with ResultSet reads will be chained on the
225      * ResultSet object.
226      *
227      * @return the first SQLWarning or null
228      */

229     public SQLWarning getWarnings() throws SQLException {
230       try {
231         return rmiStatement_.getWarnings();
232       } catch(RemoteException JavaDoc e) {
233         throw new java.sql.SQLException JavaDoc(e.getMessage());
234       }
235     }
236
237     /**
238      * After this call, getWarnings returns null until a new warning is
239      * reported for this Statement.
240      */

241     public void clearWarnings() throws SQLException {
242       try {
243         rmiStatement_.clearWarnings();
244       } catch(RemoteException JavaDoc e) {
245         throw new java.sql.SQLException JavaDoc(e.getMessage());
246       }
247     }
248
249     /**
250      * setCursorname defines the SQL cursor name that will be used by
251      * subsequent Statement execute methods. This name can then be
252      * used in SQL positioned update/delete statements to identify the
253      * current row in the ResultSet generated by this statement. If
254      * the database doesn't support positioned update/delete, this
255      * method is a noop.
256      *
257      * <P><B>Note:</B> By definition, positioned update/delete
258      * execution must be done by a different Statement than the one
259      * which generated the ResultSet being used for positioning. Also,
260      * cursor names must be unique within a Connection.
261      *
262      * @param name the new cursor name.
263      */

264     public void setCursorName(String JavaDoc name) throws SQLException {
265       try {
266         rmiStatement_.setCursorName(name);
267       } catch(RemoteException JavaDoc e) {
268         throw new java.sql.SQLException JavaDoc(e.getMessage());
269       }
270     }
271     
272     //----------------------- Multiple Results --------------------------
273

274     /**
275      * Execute a SQL statement that may return multiple results.
276      * Under some (uncommon) situations a single SQL statement may return
277      * multiple result sets and/or update counts. Normally you can ignore
278      * this, unless you're executing a stored procedure that you know may
279      * return multiple results, or unless you're dynamically executing an
280      * unknown SQL string. The "execute", "getMoreResults", "getResultSet"
281      * and "getUpdateCount" methods let you navigate through multiple results.
282      *
283      * The "execute" method executes a SQL statement and indicates the
284      * form of the first result. You can then use getResultSet or
285      * getUpdateCount to retrieve the result, and getMoreResults to
286      * move to any subsequent result(s).
287      *
288      * @param sql any SQL statement
289      * @return true if the next result is a ResultSet; false if it is
290      * an update count or there are no more results
291      * @see #getResultSet
292      * @see #getUpdateCount
293      * @see #getMoreResults
294      */

295     public boolean execute(String JavaDoc sql) throws SQLException {
296       try {
297         return rmiStatement_.execute(sql);
298       } catch(RemoteException JavaDoc e) {
299         throw new java.sql.SQLException JavaDoc(e.getMessage());
300       }
301     }
302     
303     /**
304      * getResultSet returns the current result as a ResultSet. It
305      * should only be called once per result.
306      *
307      * @return the current result as a ResultSet; null if the result
308      * is an update count or there are no more results
309      * @see #execute
310      */

311     public java.sql.ResultSet JavaDoc getResultSet() throws SQLException {
312        ResultSet rs = null;
313        try {
314          RJResultSetInterface rjrsi = rmiStatement_.getResultSet();
315
316          // only create a local ResultSet if one actually existed on the server
317
// Thanks to Phil Lopez from Informix for the fix...
318
// See also RJStatementServer.java's getResultSet()
319
if (rjrsi != null) {
320            rs = new RJResultSet(rjrsi, this);
321          }
322
323        } catch(RemoteException JavaDoc e) {
324          throw new java.sql.SQLException JavaDoc(e.getMessage());
325        }
326
327        return rs;
328     }
329
330     /**
331      * getUpdateCount returns the current result as an update count;
332      * if the result is a ResultSet or there are no more results, -1
333      * is returned. It should only be called once per result.
334      *
335      * @return the current result as an update count; -1 if it is a
336      * ResultSet or there are no more results
337      * @see #execute
338      */

339     public int getUpdateCount() throws SQLException {
340       try {
341         return rmiStatement_.getUpdateCount();
342       } catch(RemoteException JavaDoc e) {
343         throw new java.sql.SQLException JavaDoc(e.getMessage());
344       }
345     }
346
347     /**
348      * getMoreResults moves to a Statement's next result. It returns true if
349      * this result is a ResultSet. getMoreResults also implicitly
350      * closes any current ResultSet obtained with getResultSet.
351      *
352      * There are no more results when (!getMoreResults() &&
353      * (getUpdateCount() == -1)
354      *
355      * @return true if the next result is a ResultSet; false if it is
356      * an update count or there are no more results
357      * @see #execute
358      */

359     public boolean getMoreResults() throws SQLException {
360       try {
361         return rmiStatement_.getMoreResults();
362       } catch(RemoteException JavaDoc e) {
363         throw new java.sql.SQLException JavaDoc(e.getMessage());
364       }
365     }
366
367 // JDBC 2.0 methods
368
public void setFetchSize(int rows) throws SQLException {
369     try {
370       rmiStatement_.setFetchSize(rows);
371     } catch(RemoteException JavaDoc e) {
372       throw new java.sql.SQLException JavaDoc(e.getMessage());
373     }
374   }
375
376   public void setFetchDirection(int dir) throws SQLException {
377     try {
378       rmiStatement_.setFetchDirection(dir);
379     } catch(RemoteException JavaDoc e) {
380       throw new java.sql.SQLException JavaDoc(e.getMessage());
381     }
382   }
383
384   public int getResultSetType() throws SQLException {
385     try {
386       return rmiStatement_.getResultSetType();
387     } catch(RemoteException JavaDoc e) {
388       throw new java.sql.SQLException JavaDoc(e.getMessage());
389     }
390   }
391
392   public int getResultSetConcurrency() throws SQLException {
393     try {
394       return rmiStatement_.getResultSetConcurrency();
395     } catch(RemoteException JavaDoc e) {
396       throw new java.sql.SQLException JavaDoc(e.getMessage());
397     }
398   }
399
400   public int getFetchSize() throws SQLException {
401     try {
402       return rmiStatement_.getFetchSize();
403     } catch(RemoteException JavaDoc e) {
404       throw new java.sql.SQLException JavaDoc(e.getMessage());
405     }
406   }
407
408   public int getFetchDirection() throws SQLException {
409     try {
410       return rmiStatement_.getFetchDirection();
411     } catch(RemoteException JavaDoc e) {
412       throw new java.sql.SQLException JavaDoc(e.getMessage());
413     }
414   }
415
416   public Connection getConnection() throws SQLException {
417     return connection_;
418   }
419
420   public int[] executeBatch() throws SQLException {
421     try {
422       return rmiStatement_.executeBatch();
423     } catch(RemoteException JavaDoc e) {
424       throw new java.sql.SQLException JavaDoc(e.getMessage());
425     }
426   }
427
428   public void clearBatch() throws SQLException {
429     try {
430       rmiStatement_.clearBatch();
431     } catch(RemoteException JavaDoc e) {
432       throw new java.sql.SQLException JavaDoc(e.getMessage());
433     }
434   }
435
436   public void addBatch(String JavaDoc sql) throws SQLException {
437     try {
438       rmiStatement_.addBatch(sql);
439     } catch(RemoteException JavaDoc e) {
440       throw new java.sql.SQLException JavaDoc(e.getMessage());
441     }
442   }
443
444 // JDBC 3.0 methods
445
public boolean getMoreResults(int current) throws SQLException {
446     try {
447       return rmiStatement_.getMoreResults(current);
448     } catch(RemoteException JavaDoc e) {
449       throw new java.sql.SQLException JavaDoc(e.getMessage());
450     }
451   }
452     
453   public ResultSet getGeneratedKeys() throws SQLException {
454     try {
455       return new RJResultSet(rmiStatement_.getGeneratedKeys(), this);
456     } catch(RemoteException JavaDoc e) {
457       throw new java.sql.SQLException JavaDoc(e.getMessage());
458     }
459   }
460     
461   public int executeUpdate(String JavaDoc sql, int autoGeneratedKeys)
462   throws SQLException {
463     try {
464       return rmiStatement_.executeUpdate(sql,autoGeneratedKeys);
465     } catch(RemoteException JavaDoc e) {
466       throw new java.sql.SQLException JavaDoc(e.getMessage());
467     }
468   }
469     
470   public int executeUpdate(String JavaDoc sql, int columnIndexes[])
471   throws SQLException {
472     try {
473       return rmiStatement_.executeUpdate(sql, columnIndexes);
474     } catch(RemoteException JavaDoc e) {
475       throw new java.sql.SQLException JavaDoc(e.getMessage());
476     }
477   }
478
479   public int executeUpdate(String JavaDoc sql, String JavaDoc columnNames[])
480   throws SQLException {
481     try {
482       return rmiStatement_.executeUpdate(sql, columnNames);
483     } catch(RemoteException JavaDoc e) {
484       throw new java.sql.SQLException JavaDoc(e.getMessage());
485     }
486   }
487
488   public boolean execute(String JavaDoc sql, int autoGeneratedKeys)
489   throws SQLException {
490     try {
491       return rmiStatement_.execute(sql, autoGeneratedKeys);
492     } catch(RemoteException JavaDoc e) {
493       throw new java.sql.SQLException JavaDoc(e.getMessage());
494     }
495   }
496
497   public boolean execute(String JavaDoc sql, int columnIndexes[]) throws SQLException {
498     try {
499       return rmiStatement_.execute(sql, columnIndexes);
500     } catch(RemoteException JavaDoc e) {
501       throw new java.sql.SQLException JavaDoc(e.getMessage());
502     }
503   }
504     
505   public boolean execute(String JavaDoc sql, String JavaDoc columnNames[]) throws SQLException {
506     try {
507       return rmiStatement_.execute(sql, columnNames);
508     } catch(RemoteException JavaDoc e) {
509       throw new java.sql.SQLException JavaDoc(e.getMessage());
510     }
511   }
512
513   public int getResultSetHoldability() throws SQLException {
514     try {
515       return rmiStatement_.getResultSetHoldability();
516     } catch(RemoteException JavaDoc e) {
517       throw new java.sql.SQLException JavaDoc(e.getMessage());
518     }
519   }
520 };
521
522
Popular Tags