Exception handling in IBM DB2 - IBM DB2

JDBC exception handling is done using the try-catch block of the Java application. A DB2 application throws an SQLException whenever it encounters the SQL error while running the SQL statements.

An object of SQLException is created and thrown whenever an error occurs while accessing the database.The object gives the following information regarding the error:

  • Message:
  • Message is a textual representation of the error code. The getMessage method of the SQLException object returns this message string.
  • SQLState:
  • The SQLState string can be retrieved from the SQLException object by calling the SQLState method.
  • ErrorCode:
  • This is an integer value and indicates the error which caused the exception to be thrown. Error codes can be retrieved by calling the getErrorCode method of the SQL Exception object.

Apart from the above information, DB2 JCC driver provides an extra interface com. ibm. db2. jcc. DB2Diagnosable. This interface gives more information regarding the error that occurred while accessing the DB2 database. DB2Diagnosable interface has the following methods:

  • getSqlca
  • This method returns the db2sqlca object, which gives an SQL error code, SQLERRMC values, SQLERRP value, SQLERRD values, SQLWARN values, and SQLState value.
  • getThrowable
  • This method returns the java.lang.Throwable object that causes the SQLException if one exists.
  • PrintTrace
  • This method prints the stack trace information.

If multiple SQLExceptions are thrown, they are chained. The next exception information can be retrieved by calling the getNextException method of the current SQLException object. This method will return null if the current SQLException object is last in the chain. A while loop in the catch block of the program can be used to retrieve all the SQLException objects one by one. Below example shows how to handle the SQLException in the try-catch block.

SQLException handling

The SQLException class has multiple subclasses, which define more specific errors. We explain these classes next.

The SQLWarning object is created whenever there is a database warning that occurred while calling the methods of the following classes:

  • Connection
  • Statement
  • PreparedStatement
  • CallableStatement
  • ResultSet

All these interfaces contain the getWarning method to retrieve the warning information. Note that the creation SQLWarning object does not throw any SQLException. You need to call the getWarning method of the above interface to check if any warning exists or not.

Handling SQL warning

The DataTruncation class is a subclass of the SQLWarning to handle the data truncation whenever an application tries to insert a value larger than the value defined in the database definition. In that case, the data is stored after truncating the value to the specified size and a DataTruncation object is created to provide the information. Whenever truncation happens while reading the value from the database, an SQLWarning object is created instead of DataTruncation. A DataTruncation object is created when truncation occurred during writing to the database.

All the methods in SQLException and SQLWarning classes are inherited in this class. Apart from these, the following methods give more information regarding DataTruncation Exception:

  • getParameter
  • This method returns true if the parameter value is truncated. It returns false if the column value is truncated.
  • getIndex
  • This method will return an integer value, which gives the index of the parameter or column being truncated. This method will return -1 if the index of the column or parameter is unknown.
  • getRead
  • This method will return true if the truncation occurs while reading. False is returned if truncation occurred while writing to the database.
  • getDataSize
  • This methods returns the actual size of the data, which should be read or written to the database without truncation.
  • getTransferSize
  • This method will return the size of the data, which is actually read or written to the database with truncation.

An object of BatchUpdateException is thrown whenever an error occurs while running a set of statements together by using BatchUpdate. This class inherits all the methods from java.lang.Exception class. Apart from that, the following method is provided by the Batch Update Exception for the additional information:

  • getUpdateCounts:
  • This method returns a array whose size is equal to the number of elements in the batch. This array has an entry for each statement in the batch to indicate the failure or success of the statement. This method does the batch update to update the cart table.

Handling BatchUpdateException

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

IBM DB2 Topics