![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]()
![]()
(chat, newsreader, calendar)
|
![]() |
|
Course Notes Table of Contents | Exercises JDBC Short Course Index | Online Training Index
|
Connecting without JDBCTest |
Working Applet | Help Solution
|
PrerequisitesSkeleton CodeThis exercise walks you through the steps to create an application that connects to a JDBC data source, using the JDBC-ODBC Bridge. It basically duplicates the steps in the JDBCTest Introduction exercise.
Perform the following tasks:
Before you can connect to the database, register the JDBC driver. As in the previous example, you are going to use the JDBC-ODBC bridge provided with the JDK. The registration process involves loading the class. In this case, the driver name is the sun.jdbc.odbc.JdbcOdbcDriver class. Use Class.forName to load it, and check for exceptions. The class must be locatable in the CLASSPATH. This should not be a problem because this driver is part of the JDK distribution. Once you have the driver registered, you can use it to connect to the database. This is done through the DriverManager.getConnection method. The getConnection method requires three parameters, similar to the 'Select A Database' window before: the URL, username, and password. As a result, it creates an instance of Connection. After connecting to the database, you can fetch the database metadata through the Connection.getMetaData method. This returns an instance of DatabaseMetaData, which has around 100 methods to discover information about the database. To truly duplicate JDBCTest, once you have the database metadata, print out all the information available. As a cautionary note, not all existing JDBC drivers provide database metadata. The Imaginary mSQL driver does not. If you are using an incomplete JDBC driver, check for null before examining the database metadata. When you have finished examining the database metadata, close the connection via the Connection.close method.
|
Copyright © 1997 MageLang Institute. All Rights Reserved May-97 Copyright © 1996, 1997 Sun Microsystems Inc. All Rights Reserved |