Handling Sql Errors And Warnings In Sqlj Applications; Handling Sql Errors In An Sqlj Application - IBM DB2 Manual

Table of Contents

Advertisement

Handling SQL errors and warnings in SQLJ applications

SQLJ clauses throw SQLExceptions when SQL errors occur, but not when most
SQL warnings occur.
SQLJ generates an SQLException under the following circumstances:
v When any SQL statement returns a negative SQL error code
v When a SELECT INTO SQL statement returns a +100 SQL error code
You need to explicitly check for other SQL warnings.
v For SQL error handling, include try/catch blocks around SQLJ statements.
v For SQL warning handling, invoke the getWarnings method after every SQLJ

Handling SQL errors in an SQLJ application

SQLJ clauses use the JDBC class java.sql.SQLException for error handling.
To handle SQL errors in SQLJ applications, following these steps:
1. Import the java.sql.SQLException class.
2. Use the Java error handling try/catch blocks to modify program flow when an
3. Obtain error information from the SQLException.
The following code prints out the SQL error that occurred if a SELECT statement
fails.
try {
}
catch(SQLException e) {
}
154
Application Programming Guide and Reference for Java
Related concepts
"SQL statement execution in SQLJ applications" on page 112
"Savepoints in SQLJ applications" on page 145
Related tasks
"Committing or rolling back SQLJ transactions" on page 153
"Connecting to a data source using SQLJ" on page 103
statement.
SQL error occurs.
You can use the getErrorCode method to retrieve SQL error codes and the
getSQLState method to retrieve SQLSTATEs.
If you are using the IBM Data Server Driver for JDBC and SQLJ, obtain
additional information from the SQLException by casting it to a DB2Diagnosable
object, in the same way that you obtain this information in a JDBC application.
#sql [ctxt] {SELECT LASTNAME INTO :empname
FROM EMPLOYEE WHERE EMPNO='000010'};
System.out.println("Error code returned: " + e.getErrorCode());
Related concepts
"Example of a simple SQLJ application" on page 101
"SQL statement execution in SQLJ applications" on page 112
Related tasks
"Handling an SQLException under the IBM Data Server Driver for JDBC and
SQLJ" on page 77
"Handling SQL warnings in an SQLJ application" on page 155

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents