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 - Connecting to an ODBC Data Source (Page 3 of 5 )
Your first project today is a Java application that uses a JDBC-ODBC bridge to connect to a Microsoft Access file.
The Access file for this project is world20.mdb, a database of world energy statistics published by the U.S. Energy Information Administration. The Coal table in this database includes three fields you will be using in the project:
To use this database, you must have an ODBC driver on your system that supports Microsoft Access files. Using the ODBC Data Source Administrator (or a similar program if you're on a non-Windows system), you must create a new ODBC data source associated with world20.mdb.
Other setup work might be needed depending on the ODBC drivers present on your system, if any. Consult the documentation included with the ODBC driver.
Caution - Though the process of using a Microsoft Access database on a Windows system via ODBC is fairly straightforward, with other database formats and systems you might need to install an ODBC driver and learn more about its use before you try to create a JDBC-ODBC application.
After you have downloaded world20.mdb to your computer or found another database that's compatible with the ODBC drivers on your system, the final step in getting the file ready for JDBC-ODBC is to create a data source associated with it. Unlike other input-output classes in Java, JDBC doesn't use a filename to identify a data file and use its contents. Instead, a tool such as the ODBC Data Source Administrator is used to name the ODBC source and indicate the file folder where it can be found.
In the ODBC Data Source Administrator, click the User DSN tab to see a list of data sources that are available. To add a new one associated with world20.mdb (or your own database), click the Add button, choose an ODBC driver, and then click the Finish button.
A Setup window opens that you can use to provide a name, short description, and other information about the database. Click the Select button to find and choose the database file.
Figure 20.3 shows the Setup window used to set up world20.mdb as a data source in the ODBC Data Sources Administrator.
Figure 20.3 The driver Setup window.
After a database has been associated with an ODBC data source, working with it in a Java program is relatively easy if you are conversant with SQL.
The first task in a JDBC program is to load the driver (or drivers) that will be used to connect to a data source. A driver is loaded with the Class.forName(String) method. Class, part of the java.lang package, can be used to load classes into the Java interpreter. The forName(String) method loads the class named by the specified string. A ClassNotFoundException may be thrown by this method.
All programs that use an ODBC data source use sun.jdbc.odbc.JdbcOdbcDriver, the JDBC-ODBC bridge driver included with Java 2. Loading this class into a Java interpreter requires the following statement:
After the driver has been loaded, you can establish a connection to the data source by using the DriverManager class in the java.sql package.
The getConnection(String, String, String) method of DriverManager can be used to set up the connection. It returns a reference to a Connection object representing an active data connection.
The three arguments of this method are as follows:
A name identifying the data source and the type of database connectivity used to reach it
The last two items are needed only if the data source is secured with a username and a password. If not, these arguments can be null strings ("").
The name of the data source is preceded by the text jdbc:odbc: when using the JDBC-ODBC bridge, which indicates the type of database connectivity in use.
The following statement could be used to connect to a data source called Payroll with a username of Doc and a password of 1rover1:
After you have a connection, you can reuse it each time you want to retrieve or store information from that connection's data source.
The getConnection() method and all others called on a data source throw SQLException errors if something goes wrong as the data source is being used. SQL has its own error messages, and they are passed along as part of SQLException objects.