Java Reference
In-Depth Information
The developers of the MySQL database offer Connector/J, a free
open source JDBC driver developed by Mark Matthews. Some of
these drivers are available to download for evaluation.
To download this driver or find out more about it, visit the web
NOTE
Java DB can be found in a
db
subfolder of the JDK installation. To develop applications
that connect to the database, you must make its driver class library accessible. One way
to accomplish this is to edit your
Classpath
environment variable.
The driver library is found in
derby.jar
in the
db/lib
subfolder. If you installed the
JDK in
C:\Program Files\jdk1.6.0
, this library is in
C:\Program Files\jdk1.6.0\
db\lib\derby.jar
. Add the entire file reference, including the folder and filename, to
your
Classpath
.
The steps for setting up a data source for JDBC are similar to those employed with the
JDBC-ODBC bridge:
Create the database.
n
Associate the database with a JDBC driver.
n
18
Establish a data source, which may include selecting a database format, database
server, username, and password.
n
Listing 18.3 is a Java application that can perform two tasks:
Create a Java DB database named
Presidents
with a database table called
con-
tacts
that contains four records.
n
Read the records from this database table.
n
This database is an Access file with contact information for U.S. presidents.
LISTING 18.3
The Full Text of
Presidents.java
1: import java.io.*;
2: import java.sql.*;
3:
4: public class Presidents {
5: String home, system;
6:
7: public Presidents() {
8: // set the database's directory
9: home = System.getProperty(“user.home”, “.”);