DB2 10.5 for Linux, UNIX, and Windows

Handling SQL warnings in an SQLJ application

Other than a +100 SQL error code on a SELECT INTO statement, warnings from the data server do not throw SQLExceptions. To handle warnings from the data server, you need to give the program access to the java.sql.SQLWarning class.

About this task

If you want to retrieve data-server-specific information about a warning, you also need to give the program access to the com.ibm.db2.jcc.DB2Diagnosable interface and the com.ibm.db2.jcc.DB2Sqlca class.

Procedure

To retrieve data-server-specific information about a warning:

  1. Set up an execution context for that SQL clause. See "Control the execution of SQL statements in SQLJ" for information on how to set up an execution context.
  2. To check for a warning from the data server, invoke the getWarnings method after you execute an SQLJ clause.

    getWarnings returns the first SQLWarning object that an SQL statement generates. Subsequent SQLWarning objects are chained to the first one.

  3. To retrieve data-server-specific information from the SQLWarning object with the IBM® Data Server Driver for JDBC and SQLJ, follow the instructions in "Handle an SQLException under the IBM Data Server Driver for JDBC and SQLJ".

Example

The following example demonstrates how to retrieve an SQLWarning object for an SQL clause with execution context execCtx. The numbers to the right of selected statements correspond to the previously-described steps.
ExecutionContext execCtx=myConnCtx.getExecutionContext();    1 
                                    // Get default execution context from 
                                    // connection context
SQLWarning sqlWarn;
…
#sql [myConnCtx,execCtx] {SELECT LASTNAME INTO :empname
  FROM EMPLOYEE WHERE EMPNO='000010'};
if ((sqlWarn = execCtx.getWarnings()) != null)               2 
System.out.println("SQLWarning " + sqlWarn);