Java provides two levels of error conditions at which to handle exceptions thrown by JDBC statements: SQLException and SQLWarning. The SQLException class provides information on the errors that have occurred during database access. This information consists of nature of error, error message, SQL state and vendor-specific information. If SQLExceptions are not handled, the program will terminate. The SQLWarning conditions are sub-classes of the SQLException condition: these provide information on the database access warnings. These can be ignored.
Constructors of the SQLException class are shown below:
public SQLException (String reason, String sqlstate, int vendorcode);
public SQLException (String reason, String sqlstate);
public SQLException (String reason);
public SQLException ();
The SQLException class accepts three parameters: reason, sqlstate and vendorcode. The parameter reason gives the description of the exception. The second parameter, sqlstate, explains the XOPEN code for identifying the exception and the third one, vendor code, is the vendor-specific code for the exception.
In general we use three methods of the SQLException. These are listed in Table.
Table Methods of the class SQLException.
Name of method | Description |
public int getErrorCode () | Returns the integer value containing the vendor-specific code. |
public SQLException getNextException () | Returns the next exception in the exception chain. |
public String getSQLState () | Returns the string containing the SQL state. |
Another exception thrown by the JDBC is the ClassNotFoundException. This exception is thrown by the static method Class.forName () at the time of loading the driver class. If the driver class is not found then this exception is thrown.
The objects of classes Connection, Statement and ResultSet also throw SQLWarning exceptions. The constructors for the SQLWarning class are the same as those of the SQLException class.