The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.sql  [73 examples] > Connections  [10 examples]

e242. 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 e241 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) {
    }

 Related Examples
e235. Connecting to an Oracle Database
e236. Connecting to a MySQL Database
e237. Connecting to a SQLServer Database
e238. Listing All Available Parameters for Creating a JDBC Connection
e239. Determining If a Database Supports Transactions
e240. Committing and Rolling Back Updates to a Database
e241. Handling a SQL Exception
e243. Getting the Driver of a Connection
e244. Setting the Number of Rows to Prefetch When Executing a SQL Query

See also: Batching    Database Meta Data    Deleting Data    Drivers    Importing and Exporting    Inserting and Updating Data    Oracle OBJECTs    Oracle VARRAYs    Procedures and Functions    Retrieving Data    Scrollable Result Sets    Tables    Updatable Result Sets   


© 2002 Addison-Wesley.