DB2 Version 10.1 for Linux, UNIX, and Windows

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.

The basic steps for retrieving SQL warning information are:

  1. Optional: During connection to the database server, set properties that affect SQLWarning objects.

    If you want full message text from a data server when you execute SQLWarning.getMessage calls, set the retrieveMessagesFromServerOnGetMessage property to true.

    If you are using IBM® Data Server Driver for JDBC and SQLJ type 2 connectivity to a DB2® for z/OS® data source, and you want extended diagnostic information that is similar to the information that is provided by the SQL GET DIAGNOSTICS statement when you execute SQLWarning.getMessage calls, set the extendedDiagnosticLevel property to EXTENDED_DIAG_MESSAGE_TEXT (241).

  2. Immediately after invoking a method that connects to a database server or executes an SQL statement, invoke the getWarnings method to retrieve an SQLWarning object.
  3. Perform the following steps in a loop:
    1. Test whether the SQLWarning object is null. If not, continue to the next step.
    2. Invoke the SQLWarning.getMessage method to retrieve the warning description.
    3. Invoke the SQLWarning.getSQLState method to retrieve the SQLSTATE value.
    4. Invoke the SQLWarning.getErrorCode method to retrieve the error code value.
    5. If you want DB2-specific warning information, perform the same steps that you perform to get DB2-specific information for an SQLException.
    6. Invoke the SQLWarning.getNextWarning method to retrieve the next SQLWarning.
The following code illustrates how to obtain generic SQLWarning information. The numbers to the right of selected statements correspond to the previously-described steps.
Figure 1. Example of processing an SQLWarning

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 
}