![]() |
|||
![]() ![]() |
![]() |
![]()
|
![]() |
What we learned so far allows us to begin our first Java stand-alone application. Listing 5-3 shows how to open a connection. Listing 5-3: How to open a connection. // opening a connection import java.sql.*; class SimpleExample { public static void main(String args[]) { try { String url = jdbc:odbc:mysource; Class.forName(sun.jdbc.odbc.JdbcOdbcDriver); Connection myConnection = DriverManager.getConnection(url, javauser, hotjava); // ... } catch (java.lang.Exception ex) {} } } Closing a Connection Because we learned how to open a connection, it seems reasonable to learn how to close it before going further. Connection void close(); boolean isClosed(); void setAutoClose(boolean autoclose); boolean getAutoClose(); These methods all apply to a connection instance. void close(); The close() method simply closes the current connection. Normally, a connection closes automatically when it is garbage-collected or when certain fatal errors occur. It may, however, be desirable to immediately close a connection under some circumstances. boolean isClosed(); The isClosed returns true if the connection is closed, and false if it is open. void setAutoClose(boolean autoclose); A connection is normally in autoclose mode by default. Because other objects may depend on specific connections (for example, statements and result sets objects, which are discussed later), it may be necessary to keep a connection open after a transaction has been committed or rolled back (canceled). The setAutoClose() allows you to disable auto closing. boolean getAutoClose(); This method returns true in case the connection is in autoclose state, and false in the opposite case. Listing 5-4 shows how to close a connection. Listing 5-4: Closing a connection. // closing a connection import java.sql.*; class SimpleExample { public static void main(String args[]) { try { // ... String url = jdbc:odbc:mysource; Class.forName(sun.jdbc.odbc.JdbcOdbcDriver); Connection myConnection = DriverManager.getConnection(url, javauser, hotjava); // ... if (!myConnection.isClosed()) myConnection.close(); // ... } catch (java.lang.Exception ex) {} } } Adjusting PropertiesIt is possible to set and query connection properties that affect the general behavior of commands to be performed within the connection. Connection Behavior The methods available to set and query the connection object are listed below. Connection void setReadOnly(boolean readonly); boolean isReadOnly(); void setCatalog(String catalog); String getCatalog(); void setReadOnly(boolean readonly); It may be necessary to put a connection in read-only mode. By default, it is not set as read-only. Setting it in read-only mode may sometimes be practical and may enable database optimizations where the connection will not be used for database updates. boolean isReadOnly(); The return is true if the connection has been set in read-only mode. Use isReadOnly to test the connection mode. void setCatalog(String catalog); A catalog is a database subspace containing the database objects affected by the operations performed within the connection. Some DBMSs manage multiple databases at the same time. It is possible to restrict the subspace to one or another database using the setCatalog() method. Where the DBMS or the driver associated with the connection does not support catalogs, this method will silently ignore all calls to it. String getCatalog(); The getCatalog() method will give the catalog name that is currently in use or a null value. Listing 5-5 shows how to adjust connection properties. Listing 5-5: Adjusting connection properties. // adjusting connection properties import java.sql.*; class SimpleExample { public static void main(String args[]) { try { // ... String url = jdbc:odbc:mysource; Class.forName(sun.jdbc.odbc.JdbcOdbcDriver); Connection myConnection = DriverManager.getConnection(url, javauser, hotjava); if (myConnection.isReadOnly()) System.out.println(Connection is read only); myConnection.setReadOnly(true); System.out.println(Default catalog: + myConnection.getCatalog()); // use the pubs2 database myConnection.setCatalog(pubs2); // ... if (!myConnection.isClosed()) myConnection.close(); // ... } catch (java.lang.Exception ex) {} } } Putting It All Together The essential steps in every Java project that uses JDBC to obtain and terminate a connection to a DBMS are:
A Do-Nothing Client The example in Listing 5-6 does nothing but open a connection and close it. The JDBC log stream is set to the standard output to trace everything that happens. Listing 5-6: A do-nothing client. // a do-nothing client import java.sql.*; class SimpleExample { public static void main(String args[]) { String url = jdbc:odbc:mysource; try { Class.forName(sun.jdbc.odbc.JdbcOdbcDriver); DriverManager.setLogStream( java.lang.System.out); Connection myConnection = DriverManager.getConnection(url, javauser, hotjava); myConnection.close(); } catch(java.lang.Exception ex) { ex.printStackTrace(); } } }
|
![]() |
|