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 program.

  1. Copy the program files into the DERBYTUTOR directory and set the CLASSPATH environment variable, as shown in the following table.
    Table 1. Commands to copy the program files and set the CLASSPATH environment variable
    Operating System Command
    UNIX (Korn Shell)
    cp $DERBY_HOME/demo/programs/workingwithderby/* .
    export CLASSPATH=$DERBY_HOME/lib/derby.jar:.
    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.
  2. Compile the program source files. The sample program is contained in two source files: and Issue the following command to compile both at the same time:
    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.
  3. Run the program. The 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 2012-09-14 09:46:37.77 I wished for a peppermint stick
    Enter wish-list item (enter exit to end):
    a long vacation
    On 2012-09-14 09:46:37.77 I wished for a peppermint stick
    On 2012-09-14 09:46:51.654 I wished for a long vacation
    Enter wish-list item (enter exit to end):
    Closed connection
    Database shut down normally
    Getting Started With Derby JDBC program ending.