DB2 Version 9.7 for Linux, UNIX, and Windows

Controlling the execution of SQL statements in SQLJ

You can use selected methods of the SQLJ ExecutionContext class to control or monitor the execution of SQL statements.

To use ExecutionContext methods, follow these steps:

  1. Acquire the default execution context from the connection context.

    There are two ways to acquire an execution context:

    • Acquire the default execution context from the connection context. For example:
      ExecutionContext execCtx = connCtx.getExecutionContext();
    • Create a new execution context by invoking the constructor for ExecutionContext. For example:
      ExecutionContext execCtx=new ExecutionContext();
  2. Associate the execution context with an SQL statement.

    To do that, specify an execution context after the connection context in the execution clause that contains the SQL statement.

  3. Invoke ExecutionContext methods.

    Some ExecutionContext methods are applicable before the associated SQL statement is executed, and some are applicable only after their associated SQL statement is executed.

    For example, you can use method getUpdateCount to count the number of rows that are deleted by a DELETE statement after you execute the DELETE statement.

The following code demonstrates how to acquire an execution context, and then use the getUpdateCount method on that execution context to determine the number of rows that were deleted by a DELETE statement. The numbers to the right of selected statements correspond to the previously-described steps.
ExecutionContext execCtx=new ExecutionContext();                       1 
#sql [connCtx, execCtx] {DELETE FROM EMPLOYEE WHERE SALARY > 10000};   2 
System.out.println("Deleted " + execCtx.getUpdateCount() + " rows");   3