Determining If a SQL Warning Occurred

Some database operations can cause a warning which is not handled by an exception. These warning must be explicitly checked for. An example of a warning is a data truncation error during a read operation (see the DataTruncation class).

There are three places to check for a warning --- on a Connection object, a Statement object, and a ResultSet object. This example demonstrates how to check for warning on each of these objects.

try { // Get warnings on Connection object SQLWarning warning = connection.getWarnings(); while (warning != null) { // Process connection warning // For information on these values, see Handling a SQL Exception String message = warning.getMessage(); String sqlState = warning.getSQLState(); int errorCode = warning.getErrorCode(); warning = warning.getNextWarning(); } // Create a statement Statement stmt = connection.createStatement(); // Use the statement... // Get warnings on Statement object warning = stmt.getWarnings(); if (warning != null) { // Process statement warnings... } // Get a result set ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table"); while (resultSet.next()) { // Use result set // Get warnings on the current row of the ResultSet object warning = resultSet.getWarnings(); if (warning != null) { // Process result set warnings... } } } catch (SQLException e) { }

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.