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 driver–specific 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 non–critical 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 DataTruncation–specific 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.

Putting It Together

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:

Query Database. This button loads the records from the database and displays them.
Delete Current Row. Deletes the row from the database currently selected in the JTable.
Insert Row. Inserts a new row in the JTable.
Start Transaction. Starts a transaction. After this, all operations are in the transaction scope until the transaction is committed or rolled back.
Commit Transaction. Commits the transaction if there is one in progress
Rollback Transaction. Rolls back the transaction if there is one in progress.

The example consists of three classes:

JDBCTable. A subclass of JPanel that displays our UI, including a table of data.
JDBCTableModel. A subclass of AbstractTableModel that implements all of the database access routines and keeps track of the current selection from the database.
WindowCloser. A subclass of WindowAdapter used as a convenience class for quitting the application when the user closes the main window.

Let’s 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