Java Reference
In-Depth Information
throw new SQLException("Unable to access database
table",
new
java.io.IOException("File
I/O problem"));
}
catch (SQLException sqlex)
{
while (sqlex != null)
{
System.err.println("SQL
error
:
"+sqlex.getMessage());
System.err.println("SQL
state
:
"+sqlex.getSQLState());
System.err.println("Error
code:
"+sqlex.getErrorCode());
System.err.println("Cause: "+sqlex.getCause());
sqlex = sqlex.getNextException();
}
}
Connectionsmustbeclosedwhennolongerneeded; Connection declaresa void
close() method for this purpose. Because Connection implements
java.lang.AutoCloseable , you can use the try-with-resources statement (see
Chapter 3 ) to have this method automatically called whether or not an exception is
thrown.
Assuming that Java DB hasn't been configured (by setting the DERBY_HOME and
CLASSPATH environment variables), you should expect the following output:
SQL
error
:
No
suitable
driver
found
for
jd-
bc:derby:employee;create=true
SQL state : 08001
Error code: 0
Cause: null
If you've configured Java DB, you should observe no output.
SQLException declares several subclasses (e.g.,
java.sql.BatchUpdateException —an error has occurred during a batch up-
date operation). Many of these subclasses are categorized under
java.sql.SQLNonTransientException -
and
Search WWH ::




Custom Search