The errorPrint and SQLExceptionPrint methods
Getting Started with Derby
31
myWishes.close();
Shut down the database
DATABASE SHUTDOWN SECTION: If an application starts the Derby engine,
the application should shut down all databases before exiting. The attribute
;shutdown=true
in the Derby connection URL performs the shutdown. When the
Derby engine is shutdown, all booted databases will automatically shut down. The
shutdown process cleans up records in the transaction log to ensure a faster startup the
next time the database is booted.
Tip: You can shut down individual databases without shutting down the engine by
including the database name in the connection URL.
"This section verifies that the embedded driver is being used, then issues the shutdown
command and catches the shutdown exception to confirm that the Derby engine shut
down cleanly. The shutdown status is displayed before the program exits.
if (driver.equals("org.apache.derby.jdbc.EmbeddedDriver")) {
boolean gotSQLExc = false;
try {
DriverManager.getConnection("jdbc:derby:;shutdown=true");
} catch (SQLException se) {
if ( se.getSQLState().equals("XJ015") ) {
gotSQLExc = true;
}
}
if (!gotSQLExc) {
System.out.println("Database did not shut down normally");
} else {
System.out.println("Database shut down normally");
}
}
> Important: The XJ015 error (successful shutdown of the Derby engine) and the 08006
error (successful shutdown of a single database) are the only exceptions thrown by
Derby that might indicate that an operation succeeded. All other exceptions indicate that
an operation failed. You should check the log file to be certain.
The errorPrint and SQLExceptionPrint methods
DERBY EXCEPTION REPORTING CLASSES: The two methods at the end of the file,
errorPrint
and
SQLExceptionPrint
, are generic exception-reporting methods
that can be used with any JDBC program. This type of exception handling is required
because often multiple exceptions (
SQLException
) are chained together and then
thrown. A
while
loop is used to report on each error in the chain. The program starts
this process by calling the
errorPrint
method from the
catch
block of the code that
accesses the database.
// Beginning of the primary catch block: uses errorPrint method
} catch (Throwable e) {
/* Catch all exceptions and pass them to
** the exception reporting method */
System.out.println(" . . . exception thrown:");
errorPrint(e);
}
The
errorPrint
method prints a stack trace for all exceptions except a
SQLException
. Each
SQLException
is passed to the
SQLExceptionPrint
method.
static void errorPrint(Throwable e) {
if (e instanceof SQLException)
SQLExceptionPrint((SQLException)e);
else {