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: SQL Warning/Exception Handling
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 the odbc.datasource file from the previous exercise.

Task 2

There are two try blocks that try to load the JDBC-ODBC driver. The first will fail, the second will succeed so you can continue with the rest of the exercise. For both, fill in the catch block with the proper exception.

The exception to check for is SQLException

Task 3

Next, the system attempts to create a Connection to the DriverManager using an invalid URL. Catch the appropriate exception here, too. Also, in the catch block, display the error code, message, and state of the exception.

The methods you need to know about are getErrorCode, getSQLState and getMessage.

Task 4

The printSQLWarnings method is needed for the rest of the code. It is your job to create it. This method accepts one parameter of a SQLWarning. For each SQLWarning in the chain, print out all the available information about the warning. If the SQLWarning happens to be a DataTruncation warning, display information about that, too.

Call printSQLWarnings to print out any warning messages for every time you use a Connection, ResultSet, or Statement.

The getNextWarning() allows you to loop through all the warnings.

Also, use instanceof to check for DataTruncation warnings.

Task 5

Using the DatabaseMetaData, determine the level of ANSI92 SQL supported by the database and the SQL grammar level supported by the ODBC driver.

The supportsANSI92FullSQL, supportsANSI92IntermediateSQL, and supportsANSI92EntryLevelSQL methods report the ANSI92 SQL level. While, supportsExtendedSQLGrammar, supportsCoreSQLGrammar, and supportsMinimumSQLGrammar report the grammar level.

Task 6

Create and populate a new table: students2 from information in an existing table, students.
  • First, make sure students2 doesn't exist. This only matters if you are going to run the program multiple times.
  • Next, create the students2 table, with the following SQL clause:
    create table students2 (name varchar (32), class varchar (32))
  • Then, populate the table, by getting information from the identical fields from the student table.
    • If Core SQL Grammar is supported, this can be done with one statement. And this is already done for you in skeleton.
    • If Minimum SQL Grammar is supported, you need to loop through each record of students to insert into students2.
  • Finally, fill in the exception-handling code for the catch block of all this.

  • The drop table students2 clause should be in its own try/catch block so failure doesn't stop everything.
  • To create table:Statement.executeUpdate("create table students2 (name varchar (32), class varchar (32))");
  • The insert into students2 (name, class) values ('name', 'class') SQL clause should suffice for the insert. Review the Selecting exercise if you need a refresher on reading through the records.

Task 7

Close everything.



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