As in all Java™ programs, error handling for JDBC applications is done using try/catch blocks. Methods throw exceptions when an error occurs, and the code in the catch block handles those exceptions.
The basic steps for handling an SQLException in a JDBC program that runs under the IBM® Data Server Driver for JDBC and SQLJ are:
The following code demonstrates how to obtain IBM Data Server Driver for JDBC and SQLJ-specific information from an SQLException that is provided with the IBM Data Server Driver for JDBC and SQLJ. The numbers to the right of selected statements correspond to the previously-described steps.
import java.sql.*; // Import JDBC API package
import com.ibm.db2.jcc.DB2Diagnosable; // Import packages for DB2 1
import com.ibm.db2.jcc.DB2Sqlca; // SQLException support
java.io.PrintWriter printWriter; // For dumping all SQLException
// information
String url = "jdbc:db2://myhost:9999/myDB:" + 2
"retrieveMessagesFromServerOnGetMessage=true;";
// Set properties to retrieve full message
// text
String user = "db2adm";
String password = "db2adm";
java.sql.Connection con =
java.sql.DriverManager.getConnection (url, user, password)
// Connect to a DB2 for z/OS data source
…
try { 4
// Code that could generate SQLExceptions
…
} catch(SQLException sqle) {
while(sqle != null) { // Check whether there are more 5a
// SQLExceptions to process
//=====> Optional IBM Data Server Driver for JDBC and SQLJ-only
// error processing
if (sqle instanceof DB2Diagnosable) { 5d
// Check if IBM Data Server Driver for JDBC and SQLJ-only
// information exists
com.ibm.db2.jcc.DB2Diagnosable diagnosable =
(com.ibm.db2.jcc.DB2Diagnosable)sqle; 5d1
diagnosable.printTrace (printWriter, ""); 5d2
java.lang.Throwable throwable =
diagnosable.getThrowable(); 5d3
if (throwable != null) {
// Extract java.lang.Throwable information
// such as message or stack trace.
…
}
DB2Sqlca sqlca = diagnosable.getSqlca(); 5d4
// Get DB2Sqlca object
if (sqlca != null) { // Check that DB2Sqlca is not null
int sqlCode = sqlca.getSqlCode(); // Get the SQL error code 5d5
String sqlErrmc = sqlca.getSqlErrmc(); 5d6
// Get the entire SQLERRMC
String[] sqlErrmcTokens = sqlca.getSqlErrmcTokens();
// You can also retrieve the
// individual SQLERRMC tokens
String sqlErrp = sqlca.getSqlErrp(); 5d7
// Get the SQLERRP
int[] sqlErrd = sqlca.getSqlErrd(); 5d8
// Get SQLERRD fields
char[] sqlWarn = sqlca.getSqlWarn(); 5d9
// Get SQLWARN fields
String sqlState = sqlca.getSqlState(); 5d10
// Get SQLSTATE
String errMessage = sqlca.getMessage(); 5d11
// Get error message
System.err.println ("Server error message: " + errMessage);
System.err.println ("--------------- SQLCA ---------------");
System.err.println ("Error code: " + sqlCode);
System.err.println ("SQLERRMC: " + sqlErrmc);
If (sqlErrmcTokens != null) {
for (int i=0; i< sqlErrmcTokens.length; i++) {
System.err.println (" token " + i + ": " + sqlErrmcTokens[i]);
}
}
System.err.println ( "SQLERRP: " + sqlErrp );
System.err.println (
"SQLERRD(1): " + sqlErrd[0] + "\n" +
"SQLERRD(2): " + sqlErrd[1] + "\n" +
"SQLERRD(3): " + sqlErrd[2] + "\n" +
"SQLERRD(4): " + sqlErrd[3] + "\n" +
"SQLERRD(5): " + sqlErrd[4] + "\n" +
"SQLERRD(6): " + sqlErrd[5] );
System.err.println (
"SQLWARN1: " + sqlWarn[0] + "\n" +
"SQLWARN2: " + sqlWarn[1] + "\n" +
"SQLWARN3: " + sqlWarn[2] + "\n" +
"SQLWARN4: " + sqlWarn[3] + "\n" +
"SQLWARN5: " + sqlWarn[4] + "\n" +
"SQLWARN6: " + sqlWarn[5] + "\n" +
"SQLWARN7: " + sqlWarn[6] + "\n" +
"SQLWARN8: " + sqlWarn[7] + "\n" +
"SQLWARN9: " + sqlWarn[8] + "\n" +
"SQLWARNA: " + sqlWarn[9] );
System.err.println ("SQLSTATE: " + sqlState);
// portion of SQLException
}
sqle=sqle.getNextException(); // Retrieve next SQLException 5e
}
}