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

Help: Connecting without JDBCTest
Working Applet | Help
Solution

Help is available for each task, or you can go straight to the source code, which is one particular solution.

Task 1

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

import java.sql.*;

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

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

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

The URL begins with jdbc:odbc. If you setup your datasource according to the Getting Started exercise, the URL is jdbc:odbc:mage.

Connection con = DriverManager.getConnection(
  url, username, password);

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

DatabaseMetaData dmd = con.getMetaData();

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

You might want to print one or two of the items that interest you. Instead of printing everything you can just find a few things you are interested in and print them out.

Task 6

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

con.close();


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