Chapter 6 Fine Tuning JDBC Queries and Updates
In This Chapter
This chapter discusses the essential techniques for fine-tuning database queries and updates from JDBC, including:
- Dealing with data type conversion
- The SQL escape syntax
- Database transactions
- Handling SQL cursors
Driver and DriverManager Internals
It is possible to obtain detailed information from the Driver and the DriverManager through a set of methods. They are advanced functions, and a programmer usually will not use Driver or DriveManager unless the programmer wants to discover or set specific JDBC behaviors. Only those functions that are of the most interest to an application developer are listed below, the others being more useful for JDBC driver developers.
DriverManager:
void setLoginTimeout(int seconds);
int getLoginTimeout();
void setLogStream(java.io.PrintStream out);
java.io.PrintStream getLogStream();
void println(String logmessage);
void setLoginTimeout(int seconds);
This method sets the maximum time allowed when attempting to log in to a database. All registered JDBC drivers use the timeout value, expressed in seconds. It may be useful to modify this parameter in the case of the Internet scenario. An exception occurs whenever the timer expires.
int getLoginTimeout();
The method getLoginTimeout() returns the current timeout value.
void setLogStream(java.io.PrintStream out);
The JDBC logging facility was used in previous examples. It allows the tracing of all JDBC activity during program execution by providing a PrintStream. Once the log stream has been set, the tracing facility can be disabled by providing a null parameter to the same method.
java.io.PrintStream getLogStream();
The method getLogStream() returns the current JDBC logging PrintStream, or null if logging is disabled.
void println(String logmessage);
This method is used on the DriverManager object to print a message on the current JDBC log stream. The DriverManager is very talkative, and this facility may be useful for inserting your own messages in the log stream, as, for example, before all critical sections of a program to facilitate debugging.
Driver:
boolean acceptsURL(String url);
int getMajorVersion();
int getMinorVersion();
boolean jdbcCompliant();
DriverPropertyInfo[] getPropertyInfo(String url, java.util.Properties info);
boolean acceptsURL(String url);
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). This method will return true if the driver is able to understand the subprotocol specified in the URL.
int getMajorVersion();
This method returns the drivers major version number.
int getMinorVersion();
This method returns the drivers minor version number.
boolean jdbcCompliant();
In case the driver fully supports the JDBC API and SQL 92 Entry Level, this method returns true. It is a good way to verify that a driver is JDBC compliant.
// 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();
}
}
}
DriverPropertyInfo[] getPropertyInfo(String url, java.util.Properties info);
This method returns an array of DriverPropertyInfo objects describing the drivers 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.
The getPropertyInfo() method is used to discover what properties should be provided to make a connection to a database. It could, for example, be used within a generic graphical user interface (GUI) tool that has no prior knowledge about the properties 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 these members:
DriverPropertyInfo:
String DriverPropertyInfo.name;
String DriverPropertyInfo.description;
boolean DriverPropertyInfo.required;
String DriverPropertyInfo.value;
String[] DriverPropertyInfo.choices;
String DriverPropertyInfo.name;
This is the name of the property.
String DriverPropertyInfo.description;
This gives a description of the property. It may be null.
boolean DriverPropertyInfo.required;
This result is set to true if a value must be supplied to the property during a Driver.connect(). False means that the property is optional.
String DriverPropertyInfo.value;
This field is the current value of the property, or null if no value is known.
String[] DriverPropertyInfo.choices;
If the property may be chosen from a set of values, this array contains the possible choices.
SQL Data Type Conversions
The Java data types are not exactly isomorphic to the SQL data types. However, mapping SQL data types into Java allows users to store and retrieve data without losing information.
|