Handling an SQLWarning under the IBM Data Server Driver for JDBC and SQLJ
Unlike SQL errors, SQL warnings do not cause JDBC methods
to throw exceptions. Instead, the Connection, Statement, PreparedStatement, CallableStatement
,
and ResultSet classes contain getWarnings methods,
which you need to invoke after you execute SQL statements to determine
whether any SQL warnings were generated.
Procedure
The basic steps for retrieving SQL warning information are:
Example
String url = "jdbc:db2://myhost:9999/myDB:" + 1
"retrieveMessagesFromServerOnGetMessage=true;";
// Set properties to retrieve full message
// text
String user = "db2adm";
String password = "db2adm";
java.sql.Connection con =
java.sql.DriverManager.getConnection (url, user, password)
// Connect to a Db2
for z/OS data source
Statement stmt;
ResultSet rs;
SQLWarning sqlwarn;
…
stmt = con.createStatement(); // Create a Statement object
rs = stmt.executeQuery("SELECT * FROM EMPLOYEE");
// Get the result table from the query
sqlwarn = stmt.getWarnings(); // Get any warnings generated 2
while (sqlwarn != null) { // While there are warnings, get and 3a
// print warning information
System.out.println ("Warning description: " + sqlwarn.getMessage()); 3b
System.out.println ("SQLSTATE: " + sqlwarn.getSQLState()); 3c
System.out.println ("Error code: " + sqlwarn.getErrorCode()); 3d
sqlwarn=sqlwarn.getNextWarning(); // Get next SQLWarning 3f
}