Java Reference
In-Depth Information
Chapter 10: Building a Client/Server Application
In This Chapter
The aim of this chapter is to round out the discussion of the Java Database Connectivity (JDBC) core
application programming interface (API). Also, this chapter extends the code examples using simple
components in a client/server architecture. In addition, the chapter combines those examples to create
a complete, general-purpose database-management console application. This application forms the
basis of a generic toolkit for working with any data source from a flat file to a full object relational
database.
In the process of creating this application, the capabilities of the MetaData objects in the core API are
explored. The chapter also explains how to connect to different databases and use different drivers
within a single application.
To add to the functionality of this database management application, the chapter discusses measuring
and displaying the time taken to execute a query. The examples illustrate the significant difference in
performance between a good commercial pure Java driver and the jdbc-odbc bridge provided by Sun as
a basic implementation of JDBC.
Using Different Databases and Drivers
To demonstrate the flexibility of JDBC, you can create copies of the Contacts database under a variety
of RDBMS systems and listed them in a JComboBox. The JComboBox is displayed in a
JOptionPane used to select the database, as illustrated in Figure 10-1 .
Figure 10-1: Selecting different databases using a JComboBox
With this new dialog box, the user can select any of a number of versions of the Contacts test database.
Once the selection has been made, a second dialog box is displayed to enable the selection of a JDBC
driver.
Although the use of other drivers is mentioned briefly at the beginning of Chapter 5 , the examples in
earlier chapters all use the JDBC-ODBC bridge, leaving the choice of database management system
open. The reason for this is to get straight into the nuts and bolts of creating and working with a
database.
As discussed in Chapter 4 , the JDBC-ODBC bridge has one significant advantage when working with a
variety of databases: it can be used with virtually any RDBMS, whereas most other drivers are
database-system specific. On the other hand, it has the disadvantage of being less efficient than, for
example, a pure Java driver optimized for a specific RDBMS (just how much less efficient is
demonstrated by the query-timing code discussed later in this chapter).
The DriverManager can load a JDBC driver in two ways:
Search WWH ::




Custom Search