For those who want to delve deeply into Java, this article explores Java Database Connectivity (JDBC), a class library that connects Java programs to relational databases. The first of three parts, it is excerpted from chapter 20 of the book Sams Teach Yourself Java 2 in 21 Days, 4th Edition, written by Rogers Cadenhead and Laura Lemay (Sams; ISBN: 0672326280).
Reading and Writing Data Using JDBC and XML - Database Drivers (Page 2 of 5 )
Java programs that use JDBC classes can follow the familiar programming model of issuing SQL statements and processing the resulting data. The format of the database and the platform it was prepared on don't matter.
This platform- and database-independence is made possible by a driver manager. The classes of the JDBC class library are largely dependent on driver managers, which keep track of the drivers required to access database records. You'll need a different driver for each database format that's used in a program, and sometimes you might need several drivers for versions of the same format.
JDBC also includes a driver that bridges JDBC and another database-connectivity standard, ODBC.
The JDBC-ODBC Bridge
ODBC, Microsoft's common interface for accessing SQL databases, is managed on a Windows system by the ODBC Data Source Administrator.
This is run from the Control Panel on a Windows system; to get there on most versions of Windows, click Start, Settings, Control Panel, ODBC Data Sources. On Windows XP, choose Start, Control Panel, Performances and Maintenance, Administrative Tools, Data Sources (ODBC).
The administrator adds ODBC drivers, configures drivers to work with specific database files, and logs SQL use. Figure 20.1 shows the ODBC Data Source Administrator on a Windows system.
Figure 20.1 The ODBC Data Source Administrator on a Windows XP system.
In Figure 20.1, the Drivers tabbed dialog box lists all the ODBC drivers present on the system. Many of the drivers are specific to a database company's format, such as the Microsoft Access Driver.
The JDBC-ODBC bridge allows JDBC drivers to be used as ODBC drivers by converting JDBC method calls into ODBC function calls.
Using the JDBC-ODBC bridge requires three things:
The JDBC-ODBC bridge driver included with Java 2: sun.jdbc.odbc.JdbcOdbcDriver
An ODBC driver
An ODBC data source that has been associated with the driver using software such as the ODBC Data Source Administrator
ODBC data sources can be set up from within some database programs. For example, when a new database file is created in Lotus Approach, users have the option of associating it with an ODBC driver.
All ODBC data sources must be given short descriptive names. The name is used inside Java programs when a connection is made to the database that the source refers to.
On a Windows system, after an ODBC driver is selected and the database is created, they show up in the ODBC Data Source Administrator. Figure 20.2 shows an example of this for a data source named WorldEnergy.
Figure 20.2 A listing of data sources in the ODBC Data Sources Administrator.
The data source WorldEnergy is associated with a Microsoft Access driver, according to Figure 20.2.
Note - Most Windows database programs include one or more ODBC drivers that correspond to the format. Microsoft Access includes ODBC drivers that can be used to connect to an Access database file.