Create and run a JDBC program
Getting Started with Derby
32
System.out.println("A non SQL error occured.");
e.printStackTrace();
}
} // END errorPrint
The
SQLExceptionPrint
method iterates through each of the exceptions on the stack.
For each error, the method displays the codes, message, and stacktrace.
// Iterates through a stack of SQLExceptions
static void SQLExceptionPrint(SQLException sqle) {
while (sqle != null) {
System.out.println("\n---SQLException Caught---\n");
System.out.println("SQLState: " + (sqle).getSQLState());
System.out.println("Severity: " + (sqle).getErrorCode());
System.out.println("Message: " + (sqle).getMessage());
sqle.printStackTrace();
sqle = sqle.getNextException();
}
} // END SQLExceptionPrint
To see the output produced by this method, type a wish-list item with more than 32
characters, such as
I wish to see a Java program fail
.
Activity 4: Create and run a JDBC program using the client driver and
Network Server
This activity demonstrates the ease with which a program that embeds Derby can be
modified for a client/server implementation that uses the Derby Network Server.
This activity assumes you have performed the preceding activities and have a
working directory called
DERBYTUTOR
, and have copies of the program files from
the
$DERBY_HOME/demo/programs/workingwithderby/
directory. A basic
knowledge of the
WwdEmbedded.java
program and experience starting and connecting
to the Network Server are helpful. You will need to use a text editor to create the
WwdClient.java
program.
Note: As a convenience, the
workingwithderby
directory contains a program,
WwdClientExample.java
, which has already been edited in the appropriate manner.
You can use this program directly as it is, or you can compare the contents of this
program to the
WwdClient.java
program that you will construct in this activity, to see
how the changes are made.
You will create a Derby client program,
WwdClient.java
, by changing a few lines of
the
WwdEmbedded.java
program. You can run the client program in multiple command
shells, allowing simultaneous update from two or more sources.
You use two command windows (Server-Shell and Client-Shell) in this activity. You
use the Server-Shell to start the Derby Network Server and display Network Server
messages. You use the Client-Shell to edit, compile and run the newly created
WwdClient.java
program. You set the
CLASSPATH
environment variable in the
Client-Shell to support the client JDBC program.
1. Create the
WwdClient
program using the following steps:
a. Open a command window (Client-Shell).
b. Change to the
DERBYTUTOR
directory.
c. Make a copy of the
WwdEmbedded.java
program called
WwdClient.java
,
as shown in the following table.
Table 18.
Command to copy the program