9) Invoke the DB2Sqlca.getSqlWarn method to retrieve the SQLWARN
values in an array.
10) Invoke the DB2Sqlca.getSqlState method to retrieve the SQLSTATE
value.
11) Invoke the DB2Sqlca.getMessage method to retrieve error message text
from the data source.
e. Invoke the SQLException.getNextException method to retrieve the next
SQLException.
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
Figure 25. Processing an SQLException under the IBM Data Server Driver for JDBC and
SQLJ
Chapter 3. JDBC application programming 79