Java Reference
In-Depth Information
7. To access the database using Java, enter the following code:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection dbConnection =
driverManager.getConnection("jdbc:odbc:mydatasource");
where mydatasource is the name of the data source you configured in the
ODBC Data Sources Administrator.
E XERCISES
The Chapter 13 subdirectory in the Exercises folder on the CD-ROM contains a
Java file called JDBCTest. Included in the same directory is a Microsoft Access data-
base called mydatabase.mdb. In order to use this database and run the examples
without modifications, you will need to use Microsoft Access. You will also need to
configure an ODBC interface to Access as previously described. A simplified Mi-
crosoft Access database comes with Microsoft Excel.
This database has a table called Contacts, which contains some records you can
use for this exercise if you do not have access to another database. If you do have ac-
cess to another database, you can modify the program to retrieve the rows from a
different database and table.
ON THE DVD
1. The program as written uses the JDBC-ODBC Bridge Driver. To use a dif-
ferent driver, you will need to change this line to indicate the correct class
name for the driver.
private static String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
2. The program accesses a datasource called mydatasource. If you want to use
the sample database in the directory, you will need to set up a datasource
in ODBC that points to the mydatabase.mdb file. Follow the instructions in
the previous section. If you use a different database, you may need to
change this URL.
// Needs an ODBC datasource with name mydatasource to be set up.
private static String url = "jdbc:odbc:mydatasource";
Search WWH ::




Custom Search