Handling Errors¶
When connecting to an external resource (such as a database server),
errors are bound to occur; your code should include a way to handle
these errors. Both JDBC and the Advanced Server JDBC Connector provide
various types of error handling. The ListEmployees class example
demonstrates how to handle an error using try/catch blocks.
When a JDBC object throws an error (an object of type SQLException or of
a type derived from SQLException), the SQLException object exposes three
different pieces of error information:
The error message.
The SQL State.
A vendor-specific error code.
In the example, the following code displays the value of these components should an error occur:
System.out.println("SQL Exception: " + exp.getMessage());
System.out.println("SQL State: " + exp.getSQLState());
System.out.println("Vendor Error: " + exp.getErrorCode());
For example, if the server tries to connect to a database that does not exist on the specified host, the following error message is displayed:
SQL Exception: FATAL: database "acctg" does not exist
SQL State: 3D000
Vendor Error: 0