Advance Java

Learn New things from Rixosys

Handling SQLWarning

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:

  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 getWarningsmethod 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 specific warning information in Db2, perform the same steps that you perform to get specific information for an SQLException in Db2.
    6. Invoke the SQLWarning.getNextWarning method to retrieve the next SQLWarning.

Example

The following code illustrates how to obtain generic SQLWarning information. The numbers to the right of selected statements correspond to the previously-described steps.

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          
while (sqlwarn != null) {         // While there are warnings, get and   
                                  // print warning information
  System.out.println ("Warning description: " + sqlwarn.getMessage());   
  System.out.println ("SQLSTATE: " + sqlwarn.getSQLState());             
  System.out.println ("Error code: " + sqlwarn.getErrorCode());          
  sqlwarn=sqlwarn.getNextWarning();     // Get next SQLWarning           
}