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: Using MetaData
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

Copy odbc.datasource file from the previous exercise.

Task 2

Load the driver and connect to database.

import java.sql.*;
Use Class.forName to load it, and check for exceptions.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Use the resources read in from odbc.datasource resource file.
Connection con = DriverManager.getConnection(url, username, password);

Task 3

Create a Statement to perform a query.

Use Connection.createStatement to do this.

Task 4

Execute the "select * from instructors" query.

Use Statement.executeQuery to send the SQL SELECT statement. The results will come back as a ResultSet.

ResultSet result = select.executeQuery(
    "SELECT * FROM instructors");
If you are using JDBC directly, without the ODBC bridge, you can probably avoid doing the select statement and just examine the database metadata. Then you can examine the column information directly. Whether or not this works, depends upon your Driver.
ResultSet result = con.getMetaData().getColumns( "", "", "instructors", "" );

Task 5

Get the metadata for the ResultSet.

Use the ResultSet.getMetaData method.

Task 6

Examine the metadata to find out the number and datatype of columns, number of nullable columns, and number of numeric columns. You can discover more if you want to.

Look in the ResultSetMetaData API documentation to find the appropriate methods.

Column numbering is based from 1, not 0.

There are class constants to compare against for nullableness of columns:

  • columnNoNulls - is used to check for columns that do not allow nulls
  • columnNullable - is used to check for columns that do allows nulls
  • columnNullableUnknown - is used to check if nullableness is indeterminate

Task 7

Print the results.

Gathering and printing can be intermixed.

Task 8

Close the statement.

Task 9

Close the connection.



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