Chapter 7: Refining JDBC Queries and Updates

This chapter discusses advanced techniques for fine-tuning database queries and updates from JDBC.

The Internals of Driver and DriverManager

You can obtain detailed information from Driver and DriverManager through a set of methods. They are advanced functions, and a programmer usually won’t use Driver or DriveManager unless he wants to discover or set specific JDBC behaviors. Only those functions that are of the most interest to an application developer are listed in the following, the others being more useful for JDBC driver developers:

DriverManager’s Methods

void setLoginTimeout(int seconds); int getLoginTimeout(); void setLogStream(java.io.PrintStream out); java.io.PrintStream getLogStream(); void println(String logmessage); 

The method in the first line of the preceding sets the maximum time allowed when attempting to log in to a database. All registered JDBC drivers use the time-out value, expressed in seconds. An exception occurs whenever the timer expires.

The method getLoginTimeout() in the second line of the preceding returns the current time-out value.

The JDBC logging facility, as shown in the third line of the preceding, is used in listings in other chapters. It allows the tracing of all JDBC activity during program execution by providing a PrintStream. After the log stream has been set, you can disable the tracing facility by providing a null parameter to the setLogStream() method. Note that this method is now deprecated.

The method getLogStream() in the fourth line of the preceding (java.io. PrintStream getLogStream();) returns the current JDBC logging PrintStream or null if logging is disabled. This method is now deprecated.

The method in the last line of the preceding (void println(String logmessage);) is used on the DriverManager object to print a message on the current JDBC log stream. DriverManager is very talkative, and this facility may be useful for inserting your own messages in the log stream. println() is now deprecated.

Driver’s Methods

boolean acceptsURL(String url); int getMajorVersion(); int getMinorVersion(); boolean jdbcCompliant(); DriverPropertyInfo[] getPropertyInfo(String url, java.util.Properties info);

In some cases, it may be of interest to know if a particular driver is able to connect to the given uniform resource locator (URL). The method in the first line of the preceding returns true if the driver is able to understand the subprotocol specified in the URL.

The method in the second line of the preceding returns the driver’s major version number.

The method in the third line of the preceding returns the driver’s minor version number.

In case the driver fully supports the JDBC API and SQL 92 Entry Level, the method in the fourth line of the preceding returns true. Using the jdbcCompliant() method is a good way to verify that a driver is JDBC compliant. As shown in Listing 7-1, it may be a good exercise to get driver information for a few JDBC drivers that you intend to use.

Listing 7-1: Getting Information about the Driver

start example
// getting driver info import java.sql.*; class SimpleExample {          public static void main(String args[])          {                  try                  {                           Driver myDriver = new jdbc.foobar.MyDriver();                           DriverManager.registerDriver(myDriver);                           DriverManager.setLogStream(                                   java.lang.System.out);                           System.out.println("Connection to" +                                   "jdbc:mydriver://javabank.com/ possible? " +                                   myDriver.acceptsURL                                    ("jdbc:mydriver://javabank.com/"));                           System.out.println("Major Version: " +                                   myDriver.getMajorVersion());                           System.out.println("Minor Version: " +                                   myDriver.getMinorVersion());                           System.out.println("JDBC COMPLIANT driver? "                                   + myDriver.jdbcCompliant());                  }                  catch(java.lang.Exception ex)                  {                           ex.printStackTrace();                  }          } }
end example

The method in the line

DriverPropertyInfo[] getPropertyInfo(String url, java.util.Properties info);

returns an array of DriverPropertyInfo objects describing the driver’s possible properties. It takes the URL as an argument as well as a proposed list of property name/value pairs that will be sent to open the connection. An empty array is returned when no properties are required. Note that the name/value pair is arbitrary and depends on the driver.

The getPropertyInfo() method is used to discover what properties should be provided to make a connection to a database. It can, for example, be used within a generic graphical user interface (GUI) tool that has no prior knowledge about the properties that it should prompt a user for to get the connection. The DriverPropertyInfo objects should be analyzed to discover the possible properties, both those that are required and those that are optional. It may be necessary to iterate through several calls to the getPropertyInfo() method because additional property values may become necessary, as soon as the values are supplied.

The DriverPropertyInfo class is composed of the following members:

DriverPropertyInfo’s Attributes

String DriverPropertyInfo.name; String DriverPropertyInfo.description; boolean DriverPropertyInfo.required; String DriverPropertyInfo.value; String[] DriverPropertyInfo.choices;

String DriverPropertyInfo.name is the name of the property. This keyword enables you to uniquely name a specific property. The properties are arbitrary name/value pairs; therefore, no mandatory properties exist. Refer to the JDBC driver documentation to find out about its specific properties.

String DriverPropertyInfo.description gives a description of the property. It may be null.

boolean DriverPropertyInfo.required is set to true if a value must be supplied to the property during a Driver.connect(). false means that the property isn’t mandatory to establish a connection using this specific JDBC driver.

String DriverPropertyInfo.value is the current value of the property or null if no value is known.

If the property can be chosen from a set of values, the array in the last line of the preceding (String[] DriverPropertyInfo.choices) contains the possible choices as String values.



JDBC 3. 0. JAVA Database Connectivity
JDBC 3: Java Database Connectivity
ISBN: 0764548751
EAN: 2147483647
Year: 2002
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net