KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Properties JavaDoc;
16 //import javax.transaction.xa.*;
17

18 /**
19  * <P>A Connection represents a session with a specific
20  * database. Within the context of a Connection, SQL statements are
21  * executed and results are returned.
22  *
23  * <P>A Connection's database is able to provide information
24  * describing its tables, its supported SQL grammar, its stored
25  * procedures, the capabilities of this connection, etc. This
26  * information is obtained with the getMetaData method.
27  *
28  * <P><B>Note:</B> By default the Connection automatically commits
29  * changes after executing each statement. If auto commit has been
30  * disabled, an explicit commit must be done or database changes will
31  * not be saved.
32  *
33  * @see DriverManager#getConnection
34  * @see Statement
35  * @see ResultSet
36  * @see DatabaseMetaData
37  */

38 public class RJConnection
39 implements java.sql.Connection JavaDoc, java.io.Serializable JavaDoc {
40
41   RJConnectionInterface rmiConnection_;
42
43   protected RJConnection(RJConnectionInterface rmiconn) {
44     rmiConnection_ = rmiconn;
45   }
46
47   public RJConnection(RJDriverInterface drv, String JavaDoc url, Properties JavaDoc info)
48   throws Exception JavaDoc {
49
50     // url and info are JDBC data
51
rmiConnection_ = drv.connect(url, info);
52
53     if(rmiConnection_ == null) {
54       throw new SQLException("Underlying driver couldn\'t establish the connection: connect() returned null, check the configuration");
55     }
56   }
57
58
59   /**
60    * SQL statements without parameters are normally
61    * executed using Statement objects. If the same SQL statement
62    * is executed many times, it is more efficient to use a
63    * PreparedStatement
64    *
65    * @return a new Statement object
66    */

67    public java.sql.Statement JavaDoc createStatement() throws SQLException {
68      try {
69        return new RJStatement(rmiConnection_.createStatement(), this);
70      } catch(RemoteException JavaDoc e) {
71        throw new java.sql.SQLException JavaDoc(e.getMessage());
72      }
73    }
74
75    /**
76     * A SQL statement with or without IN parameters can be
77     * pre-compiled and stored in a PreparedStatement object. This
78     * object can then be used to efficiently execute this statement
79     * multiple times.
80     *
81     * <P><B>Note:</B> This method is optimized for handling
82     * parametric SQL statements that benefit from precompilation. If
83     * the driver supports precompilation, prepareStatement will send
84     * the statement to the database for precompilation. Some drivers
85     * may not support precompilation. In this case, the statement may
86     * not be sent to the database until the PreparedStatement is
87     * executed. This has no direct affect on users; however, it does
88     * affect which method throws certain SQLExceptions.
89     *
90     * @param sql a SQL statement that may contain one or more '?' IN
91     * parameter placeholders
92     *
93     * @return a new PreparedStatement object containing the
94     * pre-compiled statement
95     */

96    public java.sql.PreparedStatement JavaDoc prepareStatement(String JavaDoc sql)
97    throws SQLException {
98      try {
99        return new RJPreparedStatement(rmiConnection_.prepareStatement(sql),
100         this);
101      } catch(RemoteException JavaDoc e) {
102        throw new java.sql.SQLException JavaDoc(e.getMessage());
103      }
104    }
105
106     /**
107      * A SQL stored procedure call statement is handled by creating a
108      * CallableStatement for it. The CallableStatement provides
109      * methods for setting up its IN and OUT parameters, and
110      * methods for executing it.
111      *
112      * <P><B>Note:</B> This method is optimized for handling stored
113      * procedure call statements. Some drivers may send the call
114      * statement to the database when the prepareCall is done; others
115      * may wait until the CallableStatement is executed. This has no
116      * direct affect on users; however, it does affect which method
117      * throws certain SQLExceptions.
118      *
119      * @param sql a SQL statement that may contain one or more '?'
120      * parameter placeholders. Typically this statement is a JDBC
121      * function call escape string.
122      *
123      * @return a new CallableStatement object containing the
124      * pre-compiled SQL statement
125      */

126     public java.sql.CallableStatement JavaDoc prepareCall(String JavaDoc sql)
127     throws SQLException {
128       try {
129         return new RJCallableStatement(rmiConnection_.prepareCall(sql), this);
130       } catch(RemoteException JavaDoc e) {
131         throw new java.sql.SQLException JavaDoc(e.getMessage());
132       }
133     }
134                         
135     /**
136      * A driver may convert the JDBC sql grammar into its system's
137      * native SQL grammar prior to sending it; nativeSQL returns the
138      * native form of the statement that the driver would have sent.
139      *
140      * @param sql a SQL statement that may contain one or more '?'
141      * parameter placeholders
142      *
143      * @return the native form of this statement
144      */

145     public String JavaDoc nativeSQL(String JavaDoc sql) throws SQLException {
146       try {
147         return rmiConnection_.nativeSQL(sql);
148       } catch(RemoteException JavaDoc e) {
149         throw new java.sql.SQLException JavaDoc(e.getMessage());
150       }
151     }
152
153     /**
154      * If a connection is in auto-commit mode, then all its SQL
155      * statements will be executed and committed as individual
156      * transactions. Otherwise, its SQL statements are grouped into
157      * transactions that are terminated by either commit() or
158      * rollback(). By default, new connections are in auto-commit
159      * mode.
160      *
161      * The commit occurs when the statement completes or the next
162      * execute occurs, whichever comes first. In the case of
163      * statements returning a ResultSet, the statement completes when
164      * the last row of the ResultSet has been retrieved or the
165      * ResultSet has been closed. In advanced cases, a single
166      * statement may return multiple results as well as output
167      * parameter values. Here the commit occurs when all results and
168      * output param values have been retrieved.
169      *
170      * @param autoCommit true enables auto-commit; false disables
171      * auto-commit.
172      */

173     public void setAutoCommit(boolean autoCommit) throws SQLException {
174       try {
175         rmiConnection_.setAutoCommit(autoCommit);
176       } catch(RemoteException JavaDoc e) {
177         throw new java.sql.SQLException JavaDoc(e.getMessage());
178       }
179     }
180
181     /**
182      * Get the current auto-commit state.
183      * @return Current state of auto-commit mode.
184      * @see #setAutoCommit
185      */

186     public boolean getAutoCommit() throws SQLException {
187       try {
188         return rmiConnection_.getAutoCommit();
189       } catch(RemoteException JavaDoc e) {
190         throw new java.sql.SQLException JavaDoc(e.getMessage());
191       }
192     }
193
194     /**
195      * Commit makes all changes made since the previous
196      * commit/rollback permanent and releases any database locks
197      * currently held by the Connection. This method should only be
198      * used when auto commit has been disabled.
199      *
200      * @see #setAutoCommit
201      */

202     public void commit() throws SQLException {
203       try {
204         rmiConnection_.commit();
205       } catch(RemoteException JavaDoc e) {
206         throw new java.sql.SQLException JavaDoc(e.getMessage());
207       }
208     }
209
210     /**
211      * Rollback drops all changes made since the previous
212      * commit/rollback and releases any database locks currently held
213      * by the Connection. This method should only be used when auto
214      * commit has been disabled.
215      *
216      * @see #setAutoCommit
217      */

218     public void rollback() throws SQLException {
219       try {
220         rmiConnection_.rollback();
221       } catch(RemoteException JavaDoc e) {
222         throw new java.sql.SQLException JavaDoc(e.getMessage());
223       }
224     }
225
226     /**
227      * In some cases, it is desirable to immediately release a
228      * Connection's database and JDBC resources instead of waiting for
229      * them to be automatically released; the close method provides this
230      * immediate release.
231      *
232      * <P><B>Note:</B> A Connection is automatically closed when it is
233      * garbage collected. Certain fatal errors also result in a closed
234      * Connection.
235      */

236     public void close() throws SQLException {
237       try {
238         rmiConnection_.close();
239       } catch(RemoteException JavaDoc e) {
240         throw new java.sql.SQLException JavaDoc(e.getMessage());
241       }
242     }
243
244     /**
245      * Tests to see if a Connection is closed.
246      *
247      * @return true if the connection is closed; false if it's still open
248      */

249     public boolean isClosed() throws SQLException {
250       try {
251         return rmiConnection_.isClosed();
252       } catch(RemoteException JavaDoc e) {
253         throw new java.sql.SQLException JavaDoc(e.getMessage());
254       }
255     }
256
257     //======================================================================
258
// Advanced features:
259

260     /**
261      * A Connection's database is able to provide information
262      * describing its tables, its supported SQL grammar, its stored
263      * procedures, the capabilities of this connection, etc. This
264      * information is made available through a DatabaseMetaData
265      * object.
266      *
267      * @return a DatabaseMetaData object for this Connection
268      */

269     public java.sql.DatabaseMetaData JavaDoc getMetaData() throws SQLException {
270       try {
271         return new RJDatabaseMetaData(rmiConnection_.getMetaData(), this);
272       } catch(RemoteException JavaDoc e) {
273         throw new java.sql.SQLException JavaDoc(e.getMessage());
274       }
275     }
276
277     /**
278      * You can put a connection in read-only mode as a hint to enable
279      * database optimizations.
280      *
281      * <P><B>Note:</B> setReadOnly cannot be called while in the
282      * middle of a transaction.
283      *
284      * @param readOnly true enables read-only mode; false disables
285      * read-only mode.
286      */

287     public void setReadOnly(boolean readOnly) throws SQLException {
288       try {
289         rmiConnection_.setReadOnly(readOnly);
290       } catch(RemoteException JavaDoc e) {
291         throw new java.sql.SQLException JavaDoc(e.getMessage());
292       }
293     }
294
295     /**
296      * Tests to see if the connection is in read-only mode.
297      *
298      * @return true if connection is read-only
299      */

300     public boolean isReadOnly() throws SQLException {
301       try {
302         return rmiConnection_.isReadOnly();
303       } catch(RemoteException JavaDoc e) {
304         throw new java.sql.SQLException JavaDoc(e.getMessage());
305       }
306     }
307
308     /**
309      * A sub-space of this Connection's database may be selected by setting a
310      * catalog name. If the driver does not support catalogs it will
311      * silently ignore this request.
312      */

313     public void setCatalog(String JavaDoc catalog) throws SQLException {
314       try {
315         rmiConnection_.setCatalog(catalog);
316       } catch(RemoteException JavaDoc e) {
317         throw new java.sql.SQLException JavaDoc(e.getMessage());
318       }
319     }
320
321     /**
322      * Return the Connection's current catalog name.
323      *
324      * @return the current catalog name or null
325      */

326     public String JavaDoc getCatalog() throws SQLException {
327       try {
328         return rmiConnection_.getCatalog();
329       } catch(RemoteException JavaDoc e) {
330         throw new java.sql.SQLException JavaDoc(e.getMessage());
331       }
332     }
333
334     /**
335      * Transactions are not supported.
336      */

337     int TRANSACTION_NONE = 0;
338
339     /**
340      * Dirty reads, non-repeatable reads and phantom reads can occur.
341      */

342     int TRANSACTION_READ_UNCOMMITTED = 1;
343
344     /**
345      * Dirty reads are prevented; non-repeatable reads and phantom
346      * reads can occur.
347      */

348     int TRANSACTION_READ_COMMITTED = 2;
349
350     /**
351      * Dirty reads and non-repeatable reads are prevented; phantom
352      * reads can occur.
353      */

354     int TRANSACTION_REPEATABLE_READ = 4;
355
356     /**
357      * Dirty reads, non-repeatable reads and phantom reads are prevented.
358      */

359     int TRANSACTION_SERIALIZABLE = 8;
360
361     /**
362      * You can call this method to try to change the transaction
363      * isolation level using one of the TRANSACTION_* values.
364      *
365      * <P><B>Note:</B> setTransactionIsolation cannot be called while
366      * in the middle of a transaction.
367      *
368      * @param level one of the TRANSACTION_* isolation values with the
369      * exception of TRANSACTION_NONE; some databases may not support
370      * other values
371      *
372      * @see DatabaseMetaData#supportsTransactionIsolationLevel
373      */

374     public void setTransactionIsolation(int level) throws SQLException {
375       try {
376         rmiConnection_.setTransactionIsolation(level);
377       } catch(RemoteException JavaDoc e) {
378         throw new java.sql.SQLException JavaDoc(e.getMessage());
379       }
380     }
381
382     /**
383      * Get this Connection's current transaction isolation mode.
384      *
385      * @return the current TRANSACTION_* mode value
386      */

387     public int getTransactionIsolation() throws SQLException {
388       try {
389         return rmiConnection_.getTransactionIsolation();
390       } catch(RemoteException JavaDoc e) {
391         throw new java.sql.SQLException JavaDoc(e.getMessage());
392       }
393     }
394
395     /**
396      * The first warning reported by calls on this Connection is
397      * returned.
398      *
399      * <P><B>Note:</B> Subsequent warnings will be chained to this
400      * SQLWarning.
401      *
402      * @return the first SQLWarning or null
403      */

404     public SQLWarning getWarnings() throws SQLException {
405       try {
406         return rmiConnection_.getWarnings();
407       } catch(RemoteException JavaDoc e) {
408         throw new java.sql.SQLException JavaDoc(e.getMessage());
409       }
410     }
411
412     /**
413      * After this call, getWarnings returns null until a new warning is
414      * reported for this Connection.
415      */

416     public void clearWarnings() throws SQLException {
417       try {
418         rmiConnection_.clearWarnings();
419       } catch(RemoteException JavaDoc e) {
420         throw new java.sql.SQLException JavaDoc(e.getMessage());
421       }
422     }
423
424
425 // JDBC 2.0 methods
426
// Added by Mike Jennings (mjenning@islandnet.com)
427
// sometime in the summer of 1999
428
// Implementation added by Peter Hearty, Aug 2000, peter.hearty@lutris.com.
429

430   public void setTypeMap(java.util.Map JavaDoc map) throws SQLException {
431     try {
432       rmiConnection_.setTypeMap(map);
433     } catch(RemoteException JavaDoc e) {
434       throw new java.sql.SQLException JavaDoc(e.getMessage());
435     }
436   }
437
438   public PreparedStatement prepareStatement(String JavaDoc sql, int resultSetType,
439   int resultSetConcurrency) throws SQLException {
440     try {
441       return new RJPreparedStatement(
442        rmiConnection_.prepareStatement(sql, resultSetType,
443        resultSetConcurrency), this);
444     } catch(RemoteException JavaDoc e) {
445       throw new java.sql.SQLException JavaDoc(e.getMessage());
446     }
447   }
448
449   public CallableStatement prepareCall(String JavaDoc sql, int resultSetType,
450   int resultSetConcurrency) throws SQLException {
451     try {
452       return new RJCallableStatement(
453         rmiConnection_.prepareCall(sql,resultSetType,resultSetConcurrency),
454         this);
455     } catch(RemoteException JavaDoc e) {
456       throw new java.sql.SQLException JavaDoc(e.getMessage());
457     }
458   }
459
460
461   public java.util.Map JavaDoc getTypeMap() throws SQLException {
462     try {
463       return rmiConnection_.getTypeMap();
464     } catch(RemoteException JavaDoc e) {
465       throw new java.sql.SQLException JavaDoc(e.getMessage());
466     }
467   }
468
469   public Statement createStatement(int resultSetType, int resultSetConcurrency)
470   throws SQLException {
471     try {
472       return new RJStatement(
473        rmiConnection_.createStatement(resultSetType,resultSetConcurrency),
474        this);
475     } catch(RemoteException JavaDoc e) {
476       throw new java.sql.SQLException JavaDoc(e.getMessage());
477     }
478   }
479
480 /** TBD XA
481 public void startGlobalTransaction (Xid xid) throws XAException
482   {
483       try {
484         rmiConnection_.startGlobalTransaction (xid);
485       } catch(RemoteException e) {
486         throw new XAException(e.getMessage());
487       }
488   }
489 **/

490
491   //--------------------------JDBC 3.0-----------------------------
492

493   public void setHoldability(int holdability) throws SQLException {
494     try {
495       rmiConnection_.setHoldability(holdability);
496     } catch(RemoteException JavaDoc e) {
497       throw new java.sql.SQLException JavaDoc(e.getMessage());
498     }
499   }
500
501   public int getHoldability() throws SQLException {
502     try {
503       return rmiConnection_.getHoldability();
504     } catch(RemoteException JavaDoc e) {
505       throw new java.sql.SQLException JavaDoc(e.getMessage());
506     }
507   }
508
509
510   public Savepoint setSavepoint() throws SQLException {
511     try {
512       return new RJSavepoint(rmiConnection_.setSavepoint());
513     } catch(RemoteException JavaDoc e) {
514       throw new java.sql.SQLException JavaDoc(e.getMessage());
515     }
516   }
517
518   public Savepoint setSavepoint(String JavaDoc name) throws SQLException {
519     try {
520       return new RJSavepoint(rmiConnection_.setSavepoint(name));
521     } catch(RemoteException JavaDoc e) {
522       throw new java.sql.SQLException JavaDoc(e.getMessage());
523     }
524   }
525
526   public void rollback(Savepoint savepoint) throws SQLException {
527     try {
528       rmiConnection_.rollback(savepoint);
529     } catch(RemoteException JavaDoc e) {
530       throw new java.sql.SQLException JavaDoc(e.getMessage());
531     }
532   }
533
534   public void releaseSavepoint(Savepoint savepoint) throws SQLException {
535     try {
536       rmiConnection_.releaseSavepoint(savepoint);
537     } catch(RemoteException JavaDoc e) {
538       throw new java.sql.SQLException JavaDoc(e.getMessage());
539     }
540   }
541
542   public Statement createStatement(int resultSetType,
543   int resultSetConcurrency, int resultSetHoldability) throws SQLException {
544     try {
545       return new RJStatement(
546        rmiConnection_.createStatement(resultSetType, resultSetConcurrency,
547        resultSetHoldability), this);
548     } catch(RemoteException JavaDoc e) {
549       throw new java.sql.SQLException JavaDoc(e.getMessage());
550     }
551   }
552
553   public PreparedStatement prepareStatement(String JavaDoc sql, int resultSetType,
554   int resultSetConcurrency, int resultSetHoldability) throws SQLException {
555     try {
556       return new RJPreparedStatement(
557        rmiConnection_.prepareStatement(sql, resultSetType,
558        resultSetConcurrency, resultSetHoldability), this);
559     } catch(RemoteException JavaDoc e) {
560       throw new java.sql.SQLException JavaDoc(e.getMessage());
561     }
562   }
563
564
565   public CallableStatement prepareCall(String JavaDoc sql, int resultSetType,
566   int resultSetConcurrency, int resultSetHoldability) throws SQLException {
567     try {
568       return new RJCallableStatement(
569        rmiConnection_.prepareCall(sql, resultSetType, resultSetConcurrency,
570        resultSetHoldability), this);
571     } catch(RemoteException JavaDoc e) {
572       throw new java.sql.SQLException JavaDoc(e.getMessage());
573     }
574   }
575
576   public PreparedStatement prepareStatement(String JavaDoc sql, int autoGeneratedKeys)
577   throws SQLException {
578     try {
579       return new RJPreparedStatement(
580        rmiConnection_.prepareStatement(sql, autoGeneratedKeys), this);
581     } catch(RemoteException JavaDoc e) {
582       throw new java.sql.SQLException JavaDoc(e.getMessage());
583     }
584   }
585
586   public PreparedStatement prepareStatement(String JavaDoc sql, int columnIndexes[])
587   throws SQLException {
588     try {
589       return new RJPreparedStatement(
590        rmiConnection_.prepareStatement(sql, columnIndexes), this);
591     } catch(RemoteException JavaDoc e) {
592       throw new java.sql.SQLException JavaDoc(e.getMessage());
593     }
594   }
595
596   public PreparedStatement prepareStatement(String JavaDoc sql, String JavaDoc columnNames[])
597   throws SQLException {
598     try {
599       return new RJPreparedStatement(
600        rmiConnection_.prepareStatement(sql, columnNames), this);
601     } catch(RemoteException JavaDoc e) {
602       throw new java.sql.SQLException JavaDoc(e.getMessage());
603     }
604   }
605   
606 };
607
608
Popular Tags