The Java Developers Almanac 1.4


Order this book from Amazon.

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

e241. Handling a SQL Exception

This example demonstrates how to retrieve the information in a SQLException.
    try {
        // Execute SQL statements...
    } catch (SQLException e) {
        while (e != null) {
            // Retrieve a human-readable message identifying the reason for the exception
            String message = e.getMessage();
    
            // This vendor-independent string contains a code that identifies
            // the reason for the exception.
            // The code follows the Open Group SQL conventions.
            String sqlState = e.getSQLState();
    
            // Retrieve a vendor-specific code identifying the reason for the exception.
            int errorCode = e.getErrorCode();
    
            // If it is necessary to execute code based on this error code,
            // you should ensure that the expected driver is being
            // used before using the error code.
    
            // Get driver name
            String driverName = connection.getMetaData().getDriverName();
            if (driverName.equals("Oracle JDBC Driver") && errorCode == 123) {
                // Process error...
            }
    
            // The exception may have been chained; process the next chained exception
            e = e.getNextException();
        }
    }

 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
e242. Determining If a SQL Warning Occurred
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.