Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click Here!
Click Here!
ITKnowledge
Search this book:
 
Search the site:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


SQLException

When a database access error occurs, a SQLException is thrown that provides information on the error. Each SQLException provides several kinds of information:

  A description of the error, used as the Java Exception message
  A SQLstate string conforms to the XOPEN SQLstate conventions
  A vendor-specific error code, usually the actual DBMS error code
  A chain to a next exception, if any

The following methods may be used within a program on a SQLException object.

SQLException

String getMessage();
String getSQLState();
int getErrorCode();
SQLException getNextException();

String getMessage();

The message is used as the Java exception message, is available via this method, and is returned as string.

String getSQLState();

The code getSQLstate conforms to the XOPEN SQLstate definition.

int getErrorCode();

This code returns the vendor specific exception code. You should refer to the documentation for more information.

SQLException getNextException();

This method is used to get the SQLException chained to this one.

In all our examples, we were able to catch SQLExceptions without doing anything special once we caught them. The next example, Listing 5-28, shows how to use exception information. Because exceptions signal an abnormal condition, you use the provided information to try to continue the program execution.

Listing 5-28: Catching SQLExceptions.

// catching SQLExceptions
import java.sql.*;
class SimpleExample
{
  public static void main(String args[])
  {
  String url = “jdbc:odbc:mysource”;
         try
         {
         Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
         DriverManager.setLogStream(
                 java.lang.System.out);
                 Connection myConnection =
                 DriverManager.getConnection(url,
                         “javauser”, “hotjava”);
         Statement myStatement =
                         myConnection.createStatement();
         ResultSet rs = myStatement.executeQuery(
                   “SELECT * FROM employees”);
         // ...
         myStatement.close();
                 myConnection.close();
         }
         catch(SQLException ex)
         {
                 // there may be multiple error objects
                 // chained together
                 System.out.println(“\n*** SQLException caught ***\n”);
                 while (ex != null)
                 {
                         System.out.println(“SQLState: “ +
                                 ex.getSQLState());
                         System.out.println(“Message: “ +
                                 ex.getMessage());
                         System.out.println(“Vendor Code: “ +
                                 ex.getErrorCode());
                         System.out.println(“”);
                                 ex.getNextException();
                 }
         }
         catch(java.lang.Exception ex)
         {
                 ex.printStackTrace();
         }
  }
}

SQLWarning

This class provides information on database access warnings. Such warnings are silently chained to the class instance whose method caused it to be reported until they are fetched with the following method.

Connection, statement, or result set

SQLWarning getWarnings();

SQLWarning getWarnings();

This method returns a SQLWarning object or null if no warning occurred.

The SQLWarning class, that extends SQLException, provides:

SQLWarning

String getMessage();
String getSQLState();
int getErrorCode();
SQLWarning getNextWarning();

String getMessage();

The warning message is available via this method, returned as string.

String getSQLState();

The code getSQLstate conforms to the XOPEN SQLstate definition.

int getErrorCode();

This code returns the vendor-specific warning code. Refer to the vendor’s documentation for more information.

SQLException getNextWarning();

This method is used to get the SQLWarning chained to this one.

The following example, Listing 5-29, shows how to check for warnings. We explicitly call the getWarnings() method on the Connection, statement, and result set objects. The code for checkWarnings() is a generic method to handle all warnings that may occur during the program execution.

Listing 5-29: Checking SQLWarnings.

// checking SQLWarnings
import java.sql.*;
class SimpleExample
{
  public static void main(String args[])
  {
  String url = “jdbc:odbc:mysource”;
         try
         {
         Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
         DriverManager.setLogStream(
                 java.lang.System.out);
                 Connection myConnection =
                 DriverManager.getConnection(url,
                         “javauser”, “hotjava”);
         checkWarnings(myConnection.getWarnings());
         Statement myStatement =
                          myConnection.createStatement();
         checkWarnings(myStatement.getWarnings());
         ResultSet rs = myStatement.executeQuery(
                   “SELECT * FROM employees”);
         checkWarnings(rs.getWarnings());
         // ...
         myStatement.close();
                 myConnection.close();
         }
         catch(SQLException ex)
         {
                  // there may be multiple error objects
                  // chained together
                  System.out.println(“\n*** SQLException caught ***\n”);
                  while (ex != null)
                  {
                            System.out.println(“SQLState: “ +
                                    ex.getSQLState());
                            System.out.println(“Message: “ +
                                    ex.getMessage());
                            System.out.println(“Vendor Code: “ +
                                    ex.getErrorCode());
                            System.out.println(“”);
                                    ex.getNextException();
                  }
         }
         catch(java.lang.Exception ex)
         {
                  ex.printStackTrace();
         }
  }
  private static void checkWarnings(SQLWarning warn)
                                     throws SQLException
  {
         if (warn != null)
         {
         // there may be multiple warnings chained
                  // together
                  System.out.println(“\n*** SQLWarning caught ***\n”);
                  while (warn != null)
                  {
                           System.out.println(“SQLState: “ +
                                   warn.getSQLState());
                           System.out.println(“Message: “ +
                                   warn.getMessage());
                           System.out.println(“Vendor Code: “ +
                                   warn.getErrorCode());
                           System.out.println(“”);
                                   warn.getNextWarning();
                  }
         }
  }
}


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.