|
Training Index
SQL Warning/Exception Handling
by Magelang Institute
[Table of Contents]
Prerequisites
Skeleton Code
This exercise results in an application that will directly deal with
many runtime problems you may have while using JDBC. These problems existed in
the earlier exercises, but you dealt with them in a more general way, just catching
problems with a generic catch (Exception) block.
In addition, the application will behave differently based upon the
capabilities of the data source/driver. You will look at the database metadata and do an
INSERT statement differently based upon the capabilities of the source.
When dealing with exceptions in this exercise, try to be as specific as possible
in your catch blocks.
Perform the following tasks:
Copy the odbc.datasource file from the previous exercise.
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.
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 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.
Using the DatabaseMetaData, determine the level of ANSI92 SQL supported by the
database and the SQL grammar level supported by the ODBC driver.
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.
Close everything.
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 © 1996 Magelang Institute. All Rights Reserved.
|
|