Java Reference
In-Depth Information
 
During initialization, when the DriverManager loads drivers listed in the "jdbc.drivers"
system property.
 
Using Class.forName() , when a program can also explicitly load JDBC drivers at any time
When getConnection() is called, the DriverManager attempts to locate a suitable driver from
amongst those loaded at initialization and those loaded explicitly. It does this by polling all registered
drivers, passing the URL of the database to each driver's acceptsURL() method.
To illustrate explicit loading of a JDBC driver, a new JOptionPane with a JComboBox listing several
different JDBC drivers has been added to the DBManager class. This makes it possible for the user to
select the SQL Server version and use either the JdbcOdbcDriver or the Opta2000 pure Java driver.
Note
For brevity, the String arrays driving these JOptionPanes are restricted to only a few
sample items.
In addition to these changes, a couple of new features have been included to the application to add the
following functionality:
 
Window Menu has been added, together with some supporting code that allows the user to
perform such window-management tasks as cascading and tiling the JInternalFrames used to
display result sets and other information.
 
Help Menu has been added to allow the user to access information about the database
management system and the JDBC driver in use.
 
StatusPanel has been added to the bottom of the JFrame to support a message and a timer
display showing the time required to execute a statement.
 
Code has been added to get the system time before and after connecting to the database. The
elapsed time in milliseconds is calculated from the difference between these times and is displayed
on the status bar added to the bottom of the JFrame.
The code for the Window Menu and the Help Menu is similar to the menus shown in earlier chapters.
Listing 10-1 shows the cascade and tile functions supported.
Listing 10-1: The Window Menu
package JavaDatabaseBible.part2;
import java.awt.*;
import javax.swing.*;
public class WindowMenu extends DBMenu{
public WindowMenu(){
setText("Window");
setActionCommand("Window");
setBorderPainted(false);
add(new DBMenuItem("Cascade",'C',itemListener,true));
add(new DBMenuItem("Tile horizontally",'H',itemListener,true));
add(new DBMenuItem("Tile vertically",'V',itemListener,true));
}
}
The window-management functions are implemented in the DBManager class through the cascade(),
tileVertically(), and tileHorizontally() methods. The selected() method is used to
identify the currently selected JInternalFrame in order to position it correctly.
The StatusPanel class is also very simple. It incorporates a couple of JLabels added to the CENTER
and EAST areas of a JPanel with BorderLayout , as shown in Listing 10-2 . The StatusPanel is
Search WWH ::




Custom Search