![]() |
|||
![]() ![]() |
![]() |
![]()
|
![]() |
JDBC DriversA specific database is usually reachable through one or more drivers. The Driver Manager and Driver objects provide methods to load a driver and handle driver properties. JDBC must have some knowledge about the available drivers. This knowledge comes from a jdbc.drivers system property. It can be set via the Java interpreter command line or via a property file. Via the command line (which may be included in a shell script or batch file for greater convenience when invoking stand-alone programs): % java -Djdbc.drivers=vendor1.driver1 example Via a file, for example, when using the applet viewer or Suns HotJava browser: # On Unix, this file is ~/.hotjava/properties jdbc.drivers=vendor1.driver1 While the database URL specifies a specific database and protocol to be used, it is sometimes preferable to let the JDBC choose between two or more drivers. In this case, it is possible to specify a driver list in the property called jdbc.drivers. The list of driver class names should be colon separated, for example: vendor1.dbdrv:vendor2.sql.foodriver:vendor3.db.connectdrv JDBC will try to use each of the drivers listed in jdbc.drivers until it finds one that can successfully connect to the given URL. Drivers that are untrusted code will be skipped. The driver will register itself with the driver manager to allow connections to be made. In case the JDBC.driver system property is unavailable, there is a way to force a particular driver to be loaded. For example, the following line will load a JDBC-ODBC bridge driver: Class.forName(sun.jdbc.odbc.JdbcOdbcDriver); Another method is to use the following statements, but these statements will register the bridge driver class with the driver manager: Driver myDriver = new sun.jdbc.odbc.JdbcOdbcDriver(); java.sql.DriverManager.registerDriver(myDriver); Internals Methods are available to set or query driver and driver manager properties. They are used internally, and a programmer will usually not deal with them unless he or she wants to do some fine tuning. They are of interest for advanced programmers who need to discover and set specific properties. For the moment, we will only see those dealing with JDBC message logging. Indeed, the driver manager and all drivers may issue logging and tracing information. A few methods are provided here to redirect these messages and to print specific messages in the log stream. DriverManager void setLogStream(java.io.PrintStream out); java.io.PrintStream getLogStream(); void println(String logmessage); Here is the explanation: void setLogStream(java.io.PrintStream out); The setLogStream() method sets the logging print stream that is used by the driver manager and by the drivers. It can be set to null to disable this facility. java.io.PrintStream getLogStream(); The getLogStream() returns the print stream that is used for logging and tracing. It returns null when logging and tracing is disabled. void println(String logmessage); This method, shown in Listing 5-2, is used to send a message to the logging stream. Listing 5-2: The log stream. // setting the log stream import java.sql.*; class SimpleExample { public static void main(String args[]) { try { String url = jdbc:odbc:mysource; Class.forName(sun.jdbc.odbc.JdbcOdbcDriver); DriverManager.setLogStream(java.lang.System.out); DriverManager.println(Driver registered with the DriverManager!); // ... } catch (java.lang.Exception ex) {} } } More advanced methods of driver manager are discussed in Chapter 6. Creating a Connection Before creating a connection, it is necessary to declare it. It is quite simple; just say that you need a connection object and name it: Connection myConnection; In this example, the connection object is named: myConnection. To connect to the data source, the connection object must be made. The method that provides this functionality is: java.sql.DriverManager.getConnection(); The following statement creates a connection object that will send statements to the database. The URL naming convention is used with the getConnection() method and is the way to specify the data source that is targeted. String url = jdbc:odbc:mysource; Connection myConnection = DriverManager.getConnection(url, javauser, hotjava); This example shows how to pass the URL string to the driver manager plus specific arguments to the driver itself. In this case:
The driver manager will try to find a registered JDBC driver that is allowed to reach the data source that is specified in the URL. There are other methods that allow the user to get a connection to the database. They have the same name, but different parameters to differentiate these methods. DriverManager Connection getConnection(String url); Connection getConnection(String url, String user, String password); Connection getConnection(String url, java.util.Properties info); Connection getConnection(String url); The getConnection(String url) method does not use specific parameters to provide the user name and password. If necessary, and if allowed by the specific driver, these values may be passed within the URL string as shown in this example: String url = jdbc:odbc:mysource;UID=javauser;PWD=hotjava; Connection myConnection = DriverManager.getConnection(url); Connection getConnection(String url, String user, String password); The getConnection(String url, String user, String password) method sends the second and third parameters to the driver and it usually interprets them as the user name and password to connect to the data source. Connection getConnection(String url, java.util.Properties info); In the case of the getConnection(String url, java.util.Properties info) method, the second parameter is a list of arbitrary string pairs such as user and its value, and password and its value. These two connection arguments should be included in the list. The list of such properties should be included in the drivers documentation. However, it is preferable to pass as much information as possible within the database URL.
|
![]() |
|