| Previous | Table of Contents | Next |
SQLExceptions
SQLException is a subclass of exception consisting of a string error message, a string containing the SQL state as defined by the XOPEN SQL state specification, and a driverspecific int used as an additional error code. Here is an example:
try
{
Statement s = con.createStatement();
}catch (SQLException e)
{
System.println.out(e);
System.println.out(Database Driver/Source error code: +
e.getErrorCode());
System.println.out(XOPEN SQL State: + e.getSQLState());
}
If multiple errors were generated, multiple SQLException instances can be chained together. To get access to the next error, call getNextException() on the SQLException object. To add to the chain, call setNextException().
SQLWarnings
The SQLWarning class is a subclass of SQLException, but it is typically used for noncritical errors. Usually, it is not thrown as an exception. Rather, it is typically up to the program to query for SQLWarnings by calling the getWarnings() method of any Connection, Statement, or ResultSet object. Each time these objects are used, they clear out the warnings by calling clearWarnings(). This means that if you do not poll for messages, you will not get them.
Data Truncation
The DataTruncation class is a subclass of SQLWarning. A data truncation occurs when you perform a query and do not or cannot process all of the results. For example, you might get a data truncation warning if you perform a query that returns 15 rows, and you process only the first one with your result set before closing it. DataTruncation warnings are issued as part of a chain of SQLWarning messages. This means that as you process a SQLWarning chain of messages, you must look specifically for DataTruncation objects if you want to call DataTruncationspecific methods. Finding a DataTruncation requires you to perform an instance of DataTruncation check on each SQLWarning.
The following example illustrates this process:
SQLWarning warning = stmt.getWarnings();
while (warning != null)
{
System.out.println(warning.getMessage());
if(warning instanceof DataTruncation)
{
DataTruncation dt = (DataTruncation)warning;
if(dt.getParameter())
System.out.print(The parameter had );
System.out.print(A trunction error in column: +
dt.getIndex() +
should have tranfered +
dt.getDataSize() + bytes. +
Actually transfered +
dt.getTransferSize() + bytes);
if(dt.getRead())
System.out.print( while reading.);
else
System.out.print(.);
System.out.println();
}
warning = warning.getNextWarning();
}
Notice that getWarnings continue to be called until null is returned. Null signifies that there are no more errors.
The next section puts together the concepts discussed in this chapter by demonstrating a larger application.
The final example for this chapter is a complete Java application. This application uses the Java Foundation Classes (JFC), which are included with Java 2. If you are unfamiliar with JFC, you may want to read our book, Programming with JFC (John Wiley & Sons, 1998). This example is not meant to demonstrate the optimal use of all JDBC features. Rather, this is a combination of JDBC features that have been put in one application to demonstrate several capabilities of JDBC 1.0. The example illustrated in Figure 3.13 displays a list of inventory items from our INV database. Some columns are calculated. At the bottom of the main screen is a series of buttons:
The example consists of three classes:
Lets look at each of these classes in detail.
JDBCTable
The following code defines the JDBCTable class. The JDCTable class displays the main JPanel and places a JTable and six buttons in the table. JDBCTable handles the action for each button. The JDBCTable main method starts by creating a new JDBCTableModel, which takes a URL, a driver class name, a user name, and a password as parameters. The table uses a JDBCTableModel to connect to the database. Once the model connects to the database, the JDBCTable installs this model in the JTable.
Figure 3.13 JDBCTable application.
| Previous | Table of Contents | Next |