IBM DB2 Manual page 95

Table of Contents

Advertisement

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.
Figure 25. Processing an SQLException under the IBM Data Server Driver for JDBC and
SQLJ
import java.sql.*;
import com.ibm.db2.jcc.DB2Diagnosable; // Import packages for DB2
import com.ibm.db2.jcc.DB2Sqlca;
java.io.PrintWriter printWriter;
String url = "jdbc:db2://myhost:9999/myDB:" +
"retrieveMessagesFromServerOnGetMessage=true;";
String user = "db2adm";
String password = "db2adm";
java.sql.Connection con =
java.sql.DriverManager.getConnection (url, user, password)
...
try {
// Code that could generate SQLExceptions
...
} catch(SQLException sqle) {
while(sqle != null) {
//=====> Optional IBM Data Server Driver for JDBC and SQLJ-only
// error processing
if (sqle instanceof DB2Diagnosable) {
// 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;
diagnosable.printTrace (printWriter, "");
java.lang.Throwable throwable =
diagnosable.getThrowable();
if (throwable != null) {
// Extract java.lang.Throwable information
// such as message or stack trace.
...
}
DB2Sqlca sqlca = diagnosable.getSqlca();
if (sqlca != null) {
int sqlCode = sqlca.getSqlCode(); // Get the SQL error code 5d5
String sqlErrmc = sqlca.getSqlErrmc();
String[] sqlErrmcTokens = sqlca.getSqlErrmcTokens();
// Import JDBC API package
// SQLException support
// For dumping all SQLException
// information
// Set properties to retrieve full message
// text
// Connect to a DB2 for z/OS data source
// Check whether there are more
// SQLExceptions to process
// Get DB2Sqlca object
// Check that DB2Sqlca is not null
// Get the entire SQLERRMC
// You can also retrieve the
Chapter 3. JDBC application programming
1
2
4
5a
5d
5d1
5d2
5d3
5d4
5d6
79

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents