|
Activity 3: Run a JDBC program using the embedded driver
This activity loads the Derby database
engine using a simple Java JDBC program.
This activity assumes that you have opened a command window and navigated
to the DERBYTUTOR directory.
JDBC is the Java Database Connectivity API and is also the native
API for Derby. The program
uses the embedded driver to create the jdbcDemoDB database
(if the database does not exist) and then connect to the database. You can
then populate a table within the database with text. The program demonstrates
some basic JDBC processing along with related error handling. The Java
compiler and runtime use the classpath, specified by the CLASSPATH environment
variable, to locate the binary files (jar files and class files) that are
needed to run Derby and
other Java applications. Before performing this activity, you need to set
the classpath and compile the WwdEmbedded.java program.
- Copy the program files into the DERBYTUTOR directory
and set the CLASSPATH environment variable.
| Operating System |
Commands |
UNIX
(Korn Shell)
|
cp $DERBY_HOME/demo/programs/workingwithderby/* .
export CLASSPATH=$DERBY_HOME/lib/derby.jar:.
|
Windows
|
copy %DERBY_HOME%\demo\programs\workingwithderby\* .
set CLASSPATH=%DERBY_HOME%\lib\derby.jar;.
|
Important: Include the dot (.)
at the end of each command so that your current working directory is included
in the classpath and the files are copied to the correct location.
- Compile the program source files.
The sample program is contained in two source files:
WwdEmbedded.java and WwdUtils.java.
Issue the following command to compile both at the same time:
javac WwdEmbedded.java WwdUtils.java
Important: A command prompt appears if the compilation
is successful. The binary files WwdEmbedded.class and
WwdUtils.class are created.
If an error message appears, verify that the JDK is properly installed.
- Run the program. The WwdEmbedded.java program populates
a table with wish-list items. The program prompts you for text input (up to
32 characters), stores the text input in a database table, and then lists
the items stored in the table. The program continues to ask for wish-list
items until the you type the command exit or a problem is
encountered. Some basic information on program progress is displayed at the
beginning and the end of the program.
java WwdEmbedded
org.apache.derby.jdbc.EmbeddedDriver loaded.
Connected to database jdbcDemoDB
. . . . creating table WISH_LIST
Enter wish-list item (enter exit to end):
a peppermint stick
__________________________________________________________
On 2010-02-11 17:11:13.838 I wished for a peppermint stick
__________________________________________________________
Enter wish-list item (enter exit to end):
a long vacation
__________________________________________________________
On 2010-02-11 17:11:13.838 I wished for a peppermint stick
On 2010-02-11 17:11:19.381 I wished for a long vacation
__________________________________________________________
Enter wish-list item (enter exit to end):
exit
Closed connection
Database shut down normally
Getting Started With Derby JDBC program ending.
- The WwdEmbedded program
This section describes the WwdEmbedded.java program, highlighting details specific to accessing a Derby database from a JDBC program.
|