SQL/MX Programming Manual for Java

SQLJ Programming
HP NonStop SQL/MX Programming Manual for Java523726-003
3-65
Chained Exceptions and Warnings
next warning in the chain. The setNextWarning() method accepts an SQL warning
and adds it to the end of the chain.
This example shows how an SQLJ program, which uses a positioned UPDATE
statement to update multiple rows, returns an SQL exception or warning from a chain.
An exception or warning is added to the chain each time an exception or warning
occurs during the execution of the UPDATE statement in the while loop:
import java.sql.*;
import sqlj.runtime.*;
#sql public iterator NamIter implements ForUpdate
(String Last_Name, double Salary);
public class ChainedExceptionExample {
public ChainedExceptionExample() {
}
public static void main (String [] args) {
SQLWarning warn;
NamIter iter;
String empname = null;
double avgsalary = 0;
double salary = 0;
try {
ExecutionContext execCtx = new ExecutionContext();
#sql {SELECT AVG(salary) INTO :avgsalary
FROM samdbcat.persnl.employee};
#sql iter = {SELECT last_name, salary
FROM samdbcat.persnl.employee
FOR UPDATE};
while (iter.next()) {
if (iter.Salary() < avgsalary) {
#sql [execCtx] {UPDATE samdbcat.persnl.employee
SET salary = 1.1 * salary
WHERE CURRENT OF :iter};
System.out.println("Updated " +
execCtx.getUpdateCount()
+ " row(s)");
// Handles chained SQL warnings from the UPDATE
// statement
warn = execCtx.getWarnings();
while (warn != null) {
System.out.println("Warning: " + warn.getMessage();
warn = warn.getNextWarning();
}
}
}