SQL/MX Programming Manual for Java
SQLJ Programming
HP NonStop SQL/MX Programming Manual for Java—523726-003
3-61
SQL Exceptions
This code handles SQL exceptions and prints an error message if an exception is
thrown during the SELECT operation:
import java.sql.*;
import sqlj.runtime.*;
public class SQLExceptionExample {
public SQLExceptionExample() {
}
public static void main (String [] args) {
int sqlCode = 0; // Holds SQLCODE
String sqlState = null; // Holds SQLSTATE
String empname = null;
try {
#sql {SELECT last_name INTO :empname
FROM samdbcat.persnl.employee
WHERE empnum = 65};
} // End of try block
catch(SQLException se) {
sqlCode = se.getErrorCode(); // Gets SQLCODE
sqlState = se.getSQLState(); // Gets SQLSTATE
if (sqlState.equals("22001")) {
// Include code to handle error 22001
...
}
else {
// Include code to handle other errors
...
}
System.out.println(se.getMessage());
} // End of catch block
} // End of main() method
} // End of class SQLExceptionExample