Home Tutorial Java Jdbc JDBC4.0-SQL Exception Handling Enhancements

 
 

JDBC4.0-SQL Exception Handling Enhancements
Posted on: June 8, 2010 at 12:00 AM
Advertisement
In this Section ,we will discuss about enhance added feature in sql exception.

JDBC4.0-SQL Exception Handling Enhancements

Exception handling is an important part of Java programming, especially when connecting to or running a query against a back-end relational database. SQLException is the class that we have been using to indicate database related errors. JDBC 4.0 has several enhancements in SQLException handling. The following are some of the enhancements made in JDBC 4.0 release to provide a better developer's experience when dealing with SQLExceptions:

  1. New SQLException sub-classes
  2. Support for causal relationships
  3. Support for enhanced for-each loop

1.New SQLException classes

The new subclasses of SQLException were created to provide a means for Java programmers to write more portable error-handling code. There are two new categories of SQLException introduced in JDBC 4.0:

  • SQL non-transient exception
  • SQL transient exception

Non-Transient Exception: This exception is thrown when a retry of the same JDBC operation would fail unless the cause of the SQLException is corrected. The new exception classes that are added in JDBC 4.0 as subclasses of SQLNonTransientException ,which are given below :

  • SQLFeatureNotSupportedException
  •  SQLNonTransientConnectionException
  • SQLDataException
  • SQLIntegrityConstraintViolationException
  • SQLInvalidAuthorizationException
  • SQLSyntaxErrorException

Transient Exception: This exception is thrown when a previously failed JDBC operation might be able to succeed when the operation is retried without any intervention by application-level functionality. The new exceptions extending SQLTransientException are listed below :

  • SQLTransientConnectionException
  • SQLTransactionRollbackException
  • SQLTimeoutException

2.Causal Relationships

The SQLException class now supports the Java SE chained exception mechanism (also known as the Cause facility), which gives us the ability to handle multiple SQLExceptions (if the back-end database supports a multiple exceptions feature) thrown in a JDBC operation. This scenario occurs when executing a statement that may throw more than one SQLException .

We can use getNextException() method in SQLException to iterate through the exception chain. Here's some sample code to process SQLException causal relationships:

catch(SQLException ex) {
     while(ex != null) {
        LOG.error("SQL State:" + ex.getSQLState());
        LOG.error("Error Code:" + ex.getErrorCode());
        LOG.error("Message:" + ex.getMessage());
        Throwable t = ex.getCause();
        while(t != null) {
            LOG.error("Cause:" + t);
            t = t.getCause();
        }
        ex = ex.getNextException();
    }
}

 

3.Support for enhanced for-each loop

In the below example, "Example.java" , specifying  "student4"  as a table name that does not exist in the database. It raised a chained exception as follows:

public class Example2 {
  public static void main(String[] args) {
    String dbName = "example";
    String tableName = "student4";
    try {
      con = ds.getConnection();
          stmt = con.createStatement();
      rs = stmt.executeQuery("select * from " + tableName);
    } catch (SQLException sx) {
      for(Throwable e : sx ) {
        System.err.println("Error encountered: " + e);
      }
    }
    finally{
      //close connections 
    }
  }
}

OUTPUT


Error encountered: java.sql.SQLSyntaxErrorException: 
   Table/View 'STUDENT4' does not exist.
Error encountered: java.sql.SQLException: 
   Table/View 'STUDENT4' does not exist.
Exception in thread "main" java.lang.NullPointerException 
   at ex.Examlpe2.main(Examlpe2.java:51)

 

Advertisement

Related Tags for JDBC4.0-SQL Exception Handling Enhancements:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 8, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com