Technical Support
Discussion Forum
Online Training
Read About Java
Java In-Depth
Product Discounts
Membership Information JDC Resources DukeDollars

Java Cup Logo

JDC Home Page

JDC Applets
(chat, newsreader, calendar)

Log Out

Online Training
shadowSearchFAQFeedback

Course Notes Table of Contents | Exercises
JDBC Short Course Index | Online Training Index

Connecting without JDBCTest
Working Applet | Help
Solution

Prerequisites

Skeleton Code

This 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:

  1. Using the JDBC parts of the JDK requires the java.sql package. Import the java.sql package.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. When you have finished examining the database metadata, close the connection via the Connection.close method.

The task numbers above are linked to the step-by-step help page. Also available is a complete solution, which meets these requirements, and Expected Behavior.


Copyright © 1997 MageLang Institute. All Rights Reserved
May-97 Copyright © 1996, 1997 Sun Microsystems Inc. All Rights Reserved