KickJava   Java API By Example, From Geeks To Geeks.

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


1
2 /**
3  * RmiJdbc client/server JDBC Driver
4  * (C) GIE Dyade (Groupe BULL / INRIA Research Center) 1997
5  *
6  * @version 1.0
7  * @author Pierre-Yves Gibello (pierreyves.gibello@experlog.com)
8  * Additional SSL Support
9  * Douglas Hammond(djhammond@sympatico.ca)
10  */

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

32 public class RJStatementServer
33 extends UnicastRemoteObject JavaDoc
34 implements RJStatementInterface, Unreferenced JavaDoc {
35
36   java.sql.Statement JavaDoc jdbcStatement_;
37
38   public RJStatementServer(java.sql.Statement JavaDoc s)
39   throws java.rmi.RemoteException JavaDoc {
40        super(RJJdbcServer.rmiJdbcListenerPort, RJJdbcServer.rmiClientSocketFactory, RJJdbcServer.rmiServerSocketFactory);
41     jdbcStatement_ = s;
42   }
43
44 public void unreferenced()
45   {
46     Runtime.getRuntime().gc();
47   }
48
49   protected void finalize() throws Throwable JavaDoc {
50     if (jdbcStatement_ != null) jdbcStatement_.close();
51     Runtime.getRuntime().gc();
52   }
53
54
55   /**
56    * Execute a SQL statement that returns a single ResultSet.
57    *
58    * @param sql typically this is a static SQL SELECT statement
59    * @return a ResultSet that contains the data produced by the
60    * query; never null
61    */

62   public RJResultSetInterface executeQuery(String JavaDoc sql)
63   throws java.rmi.RemoteException JavaDoc, SQLException {
64     return new RJResultSetServer(jdbcStatement_.executeQuery(sql));
65   }
66
67   /**
68    * Execute a SQL INSERT, UPDATE or DELETE statement. In addition,
69    * SQL statements that return nothing such as SQL DDL statements
70    * can be executed.
71    *
72    * @param sql a SQL INSERT, UPDATE or DELETE statement or a SQL
73    * statement that returns nothing
74    * @return either the row count for INSERT, UPDATE or DELETE or 0
75    * for SQL statements that return nothing
76    */

77   public int executeUpdate(String JavaDoc sql) throws java.rmi.RemoteException JavaDoc,
78   SQLException {
79     return jdbcStatement_.executeUpdate(sql);
80   }
81
82   /**
83    * In many cases, it is desirable to immediately release a
84    * Statements's database and JDBC resources instead of waiting for
85    * this to happen when it is automatically closed; the close
86    * method provides this immediate release.
87    *
88    * <P><B>Note:</B> A Statement is automatically closed when it is
89    * garbage collected. When a Statement is closed, its current
90    * ResultSet, if one exists, is also closed.
91    */

92   public void close() throws java.rmi.RemoteException JavaDoc, SQLException {
93     if(jdbcStatement_ != null) jdbcStatement_.close();
94   }
95
96   //----------------------------------------------------------------------
97

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

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

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

132   public int getMaxRows() throws java.rmi.RemoteException JavaDoc, SQLException {
133     return jdbcStatement_.getMaxRows();
134   }
135
136   /**
137    * The maxRows limit is set to limit the number of rows that any
138    * ResultSet can contain. If the limit is exceeded, the excess
139    * rows are silently dropped.
140    *
141    * @param max the new max rows limit; zero means unlimited
142    */

143   public void setMaxRows(int max) throws java.rmi.RemoteException JavaDoc, SQLException {
144     jdbcStatement_.setMaxRows(max);
145   }
146
147   /**
148    * If escape scanning is on (the default), the driver will do
149    * escape substitution before sending the SQL to the database.
150    *
151    * @param enable true to enable; false to disable
152    */

153   public void setEscapeProcessing(boolean enable)
154   throws java.rmi.RemoteException JavaDoc, SQLException {
155     jdbcStatement_.setEscapeProcessing(enable);
156   }
157
158   /**
159    * The queryTimeout limit is the number of seconds the driver will
160    * wait for a Statement to execute. If the limit is exceeded, a
161    * java.rmi.RemoteException is thrown.
162    *
163    * @return the current query timeout limit in seconds; zero means unlimited
164    */

165   public int getQueryTimeout() throws java.rmi.RemoteException JavaDoc, SQLException {
166     return jdbcStatement_.getQueryTimeout();
167   }
168
169   /**
170    * The queryTimeout limit is the number of seconds the driver will
171    * wait for a Statement to execute. If the limit is exceeded, a
172    * java.rmi.RemoteException is thrown.
173    *
174    * @param seconds the new query timeout limit in seconds; 0 means unlimited
175    */

176   public void setQueryTimeout(int seconds) throws java.rmi.RemoteException JavaDoc, SQLException {
177     jdbcStatement_.setQueryTimeout(seconds);
178   }
179
180   /**
181    * Cancel can be used by one thread to cancel a statement that
182    * is being executed by another thread.
183    */

184   public void cancel() throws java.rmi.RemoteException JavaDoc, SQLException {
185     jdbcStatement_.cancel();
186   }
187
188   /**
189    * The first warning reported by calls on this Statement is
190    * returned. A Statment's execute methods clear its SQLWarning
191    * chain. Subsequent Statement warnings will be chained to this
192    * SQLWarning.
193    *
194    * <p>The warning chain is automatically cleared each time
195    * a statement is (re)executed.
196    *
197    * <P><B>Note:</B> If you are processing a ResultSet then any
198    * warnings associated with ResultSet reads will be chained on the
199    * ResultSet object.
200    *
201    * @return the first SQLWarning or null
202    */

203   public SQLWarning getWarnings() throws java.rmi.RemoteException JavaDoc, SQLException {
204     return jdbcStatement_.getWarnings();
205   }
206
207   /**
208    * After this call, getWarnings returns null until a new warning is
209    * reported for this Statement.
210    */

211   public void clearWarnings() throws java.rmi.RemoteException JavaDoc, SQLException {
212     jdbcStatement_.clearWarnings();
213   }
214
215   /**
216    * setCursorname defines the SQL cursor name that will be used by
217    * subsequent Statement execute methods. This name can then be
218    * used in SQL positioned update/delete statements to identify the
219    * current row in the ResultSet generated by this statement. If
220    * the database doesn't support positioned update/delete, this
221    * method is a noop.
222    *
223    * <P><B>Note:</B> By definition, positioned update/delete
224    * execution must be done by a different Statement than the one
225    * which generated the ResultSet being used for positioning. Also,
226    * cursor names must be unique within a Connection.
227    *
228    * @param name the new cursor name.
229    */

230   public void setCursorName(String JavaDoc name) throws java.rmi.RemoteException JavaDoc, SQLException {
231     jdbcStatement_.setCursorName(name);
232   }
233
234   //----------------------- Multiple Results --------------------------
235

236   /**
237    * Execute a SQL statement that may return multiple results.
238    * Under some (uncommon) situations a single SQL statement may return
239    * multiple result sets and/or update counts. Normally you can ignore
240    * this, unless you're executing a stored procedure that you know may
241    * return multiple results, or unless you're dynamically executing an
242    * unknown SQL string. The "execute", "getMoreResults", "getResultSet"
243    * and "getUpdateCount" methods let you navigate through multiple results.
244    *
245    * The "execute" method executes a SQL statement and indicates the
246    * form of the first result. You can then use getResultSet or
247    * getUpdateCount to retrieve the result, and getMoreResults to
248    * move to any subsequent result(s).
249    *
250    * @param sql any SQL statement
251    * @return true if the next result is a ResultSet; false if it is
252    * an update count or there are no more results
253    * @see #getResultSet
254    * @see #getUpdateCount
255    * @see #getMoreResults
256    */

257   public boolean execute(String JavaDoc sql) throws java.rmi.RemoteException JavaDoc, SQLException {
258     return jdbcStatement_.execute(sql);
259   }
260
261   /**
262    * getResultSet returns the current result as a ResultSet. It
263    * should only be called once per result.
264    *
265    * @return the current result as a ResultSet; null if the result
266    * is an update count or there are no more results
267    * @see #execute
268    */

269   public RJResultSetInterface getResultSet()
270   throws java.rmi.RemoteException JavaDoc, SQLException {
271
272      RJResultSetServer rsServer = null;
273
274      ResultSet rs = jdbcStatement_.getResultSet();
275
276      // only return result set if it is not null
277
// Thanks to Phil Lopez from Informix for the fix
278
// See also RJStatement.java's getResultSet() method
279
if (rs != null) {
280        rsServer = new RJResultSetServer(rs);
281      }
282
283      return rsServer;
284   }
285
286   /**
287    * getUpdateCount returns the current result as an update count;
288    * if the result is a ResultSet or there are no more results, -1
289    * is returned. It should only be called once per result.
290    *
291    * @return the current result as an update count; -1 if it is a
292    * ResultSet or there are no more results
293    * @see #execute
294    */

295   public int getUpdateCount() throws java.rmi.RemoteException JavaDoc, SQLException {
296     return jdbcStatement_.getUpdateCount();
297   }
298
299   /**
300    * getMoreResults moves to a Statement's next result. It returns true if
301    * this result is a ResultSet. getMoreResults also implicitly
302    * closes any current ResultSet obtained with getResultSet.
303    *
304    * There are no more results when (!getMoreResults() &&
305    * (getUpdateCount() == -1)
306    *
307    * @return true if the next result is a ResultSet; false if it is
308    * an update count or there are no more results
309    * @see #execute
310    */

311   public boolean getMoreResults()
312   throws java.rmi.RemoteException JavaDoc, SQLException {
313     return jdbcStatement_.getMoreResults();
314   }
315
316   // JDBC 2.0 methods
317
public void setFetchSize(int rows) throws RemoteException, SQLException {
318     jdbcStatement_.setFetchSize(rows);
319   }
320
321   public void setFetchDirection(int dir) throws RemoteException, SQLException {
322     jdbcStatement_.setFetchDirection(dir);
323   }
324
325   public int getResultSetType() throws RemoteException, SQLException {
326     return jdbcStatement_.getResultSetType();
327   }
328
329   public int getResultSetConcurrency() throws RemoteException, SQLException {
330     return jdbcStatement_.getResultSetConcurrency();
331   }
332
333   public int getFetchSize() throws RemoteException, SQLException {
334     return jdbcStatement_.getFetchSize();
335   }
336
337   public int getFetchDirection() throws RemoteException, SQLException {
338     return jdbcStatement_.getFetchDirection();
339   }
340
341   public RJConnectionInterface getConnection()
342   throws RemoteException, SQLException {
343     return new RJConnectionServer(jdbcStatement_.getConnection());
344   }
345
346   public int[] executeBatch() throws RemoteException, SQLException {
347     return jdbcStatement_.executeBatch();
348   }
349
350   public void clearBatch() throws RemoteException, SQLException {
351     jdbcStatement_.clearBatch();
352   }
353
354   public void addBatch(String JavaDoc sql) throws RemoteException, SQLException {
355     jdbcStatement_.addBatch(sql);
356   }
357
358   // JDBC 3.0 methods
359
public boolean getMoreResults(int current)
360   throws RemoteException, SQLException {
361     return jdbcStatement_.getMoreResults(current);
362   }
363    
364   public RJResultSetInterface getGeneratedKeys()
365   throws RemoteException, SQLException {
366     return new RJResultSetServer(jdbcStatement_.getGeneratedKeys());
367   }
368   
369   public int executeUpdate(String JavaDoc sql, int autoGeneratedKeys)
370   throws RemoteException, SQLException {
371     return jdbcStatement_.executeUpdate(sql, autoGeneratedKeys);
372   }
373   
374   public int executeUpdate(String JavaDoc sql, int columnIndexes[])
375   throws RemoteException, SQLException {
376     return jdbcStatement_.executeUpdate(sql, columnIndexes);
377   }
378
379   public int executeUpdate(String JavaDoc sql, String JavaDoc columnNames[])
380   throws RemoteException, SQLException {
381     return jdbcStatement_.executeUpdate(sql, columnNames);
382   }
383
384   public boolean execute(String JavaDoc sql, int autoGeneratedKeys)
385   throws RemoteException, SQLException {
386     return jdbcStatement_.execute(sql, autoGeneratedKeys);
387   }
388
389   public boolean execute(String JavaDoc sql, int columnIndexes[])
390   throws RemoteException, SQLException {
391     return jdbcStatement_.execute(sql, columnIndexes);
392   }
393
394   public boolean execute(String JavaDoc sql, String JavaDoc columnNames[])
395   throws RemoteException, SQLException {
396     return jdbcStatement_.execute(sql, columnNames);
397   }
398
399   public int getResultSetHoldability() throws RemoteException, SQLException {
400     return jdbcStatement_.getResultSetHoldability();
401   }
402
403 };
404
405
Popular Tags