Show TOC

Background documentationError Handling Locate this document in the navigation structure

 

Open SQL/JDBC enhances the standard JDBC error handling mechanism to enable you to detect problems easily.

Features

The JDBC standard offers the java.sql.SQLException class for handling errors of the JDBC methods. The most relevant method for the user is the getMessage() method, which is inherited from the Exception class and returns a string with the error text of the database. The getErrorCode() method, which returns the error number of the database, is also relevant. If an application runs on the AS Java, it can still be sure that if errors occur, Open SQL/JDBC writes the corresponding log messages.

Since the JDBC standard does not handle DUPLICATE KEY errors specifically, Open SQL/JDBC provides the class DuplicateKeyException. Using this class, an application can react specifically to such errors. The following example illustrates the use of the classes SQLException and DuplicateKeyException (where stmt is a Statement object that already exists).

Example Example

  1. try {
    	stmt.executeUpdate ("insert into VEHICLE "
    						+ "(NAME, PRICE, CURRENCY)"
    						+ "values (Audi80, 25000, EUR)");
    } catch (DuplicateKeyException e) {
    // application-specific reaction, for example
    	stmt.executeUpdate ("update VEHICLE set "
    						+ "PRICE = 25000, "
    						+ "CURRENCY = EUR "
    						+ "where NAME = Audi80");
    } catch (SQLException e) {
    // application-specific error reaction, for example
    	System.out.println(e.getMessage());
    	throw new RuntimeException(“Update of VEHICLE failed”, e);
    } 
    
End of the code.