SQL/MX Programming Manual for Java

SQLJ Programming
HP NonStop SQL/MX Programming Manual for Java523726-003
3-63
SQL Warnings
SQL Warnings
SQL warnings are stored in a java.sql.SQLWarnings object and do not generate
SQL exceptions. To check for an SQL warning, you must invoke the getWarnings()
method after you execute an SQLJ clause. Because the getWarnings() method is
part of an execution context class, you must invoke it by using the execution context,
explicit or implicit, that is associated with the SQLJ clause. See Execution Contexts on
page 3-20.
SQL Warnings in an Explicit Execution Context
This example shows how an SQLJ program that uses an explicit execution context
returns SQL warnings. The program updates an exact numeric column with a floating-
point value, resulting in the truncation of the fractional part of the value. After the
UPDATE operation, the getWarnings() method of the execution context returns a
truncation warning:
import java.sql.*;
import sqlj.runtime.*;
public class SQLWarningExample {
public SQLWarningExample() {
}
public static void main (String [] args) {
SQLWarning warn;
Float floatQty = new Float(37.5);
try {
ExecutionContext execCtx = new ExecutionContext();
// Set an exact numeric column to a floating-point value,
// resulting in a truncation warning at run time
#sql [execCtx] { UPDATE samdbcat.invent.partloc
SET qty_on_hand = :floatQty
WHERE loc_code = G76
AND partnum = 7301 };
// Handles SQL warnings from the UPDATE statement
warn = execCtx.getWarnings();
if (warn != null) {
System.out.println("Warning: " + warn);
}
} // End of try block
// Handles SQL exceptions from the try block
catch (SQLException se) {
System.err.println("SQL exception: " + se);
}