Retrieving Errors and Warnings
This section describes how to retrieve native error codes and messages in JDBC and ODBC.
Retrieve a Native Error Code and Message in JDBC
In JDBC, the native error code and message can be retrieved as shown in this example:
private static void printSQLExceptions(SQLException e)
{
while (e != null) {
System.out.println("SQLState: " +
e.getSQLState());
System.out.println("Message : " +
e.getMessage());
System.out.println("Vendor : " +
e.getErrorCode());
e.printStackTrace();
e = e.getNextException();
System.out.println("");
}
e.printStackTrace();
}
. . .
try {
// JDBC method calls here
}
catch (SQLException ex) {
ex.printSQLExceptions();
}
It is possible for a single JDBC operation to generate multiple errors. These errors
can be retrieved using multiple calls to the JDBC method
SQLException
.
Retrieve a Native Error Code and Message in ODBC
In ODBC the native error code and message can be retrieved with the SQLError
function, as shown in the following example:
#define MSG_LNG 512
SQLCHAR szSqlState[MSG_LNG]; /* SQL state string */
SQLINTEGER pfNativeError; /* Native error code */
SQLCHAR szErrorMsg[MSG_LNG];
/* Error msg text buffer pointer */
SQLSMALLINT pcbErrorMsg;
/* Error msg text Available bytes */
SQLRETURN ret = SQL_SUCCESS;
while ( ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO ) {
ret = SQLError(henv, hdbc, hstmt, szSqlState, &pfNativeError,
szErrorMsg, MSG_LNG, &pcbErrorMsg);
switch (ret) {
case SQL_SUCCESS:
fprintf(stderr, "*** %s\n*** ODBC Err = %s, ",
"TimesTen Err = %ld\n",
szErrorMsg, szSqlState, pfNativeError);
break;
case SQL_SUCCESS_WITH_INFO:
fprintf(stderr, "*** Call to SQLError failed with return ",
"code of SQL_SUCCESS_WITH_INFO.\n ",
"*** Need to increase size of ",
"message buffer.\n");
break;
case SQL_INVALID_HANDLE:
fprintf(stderr, "*** Call to SQLError failed with ",
"return code of SQL_INVALID_HANDLE.\n");
break;
case SQL_ERROR:
fprintf(stderr, "*** Call to SQLError failed with ",
"return code of SQL_ERROR.\n");
break;
case SQL_NO_DATA_FOUND:
break;
}
}
It is possible for a single ODBC operation to generate multiple errors. These errors
can be retrieved using multiple calls to the ODBC function SQLError
.