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.

  1. 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.
  2. Compile the WwdEmbedded.java program.
    javac WwdEmbedded.java
    
    Important: A command prompt appears if the compilation is successful. The binary file WwdEmbedded.class is created. If an error message appears, verify that the JDK is properly installed.
  3. 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 2007-05-04 15:16:16.795 I wished for a peppermint stick
      __________________________________________________________
    Enter wish-list item (enter exit to end):
    an all expenses paid vacation
      __________________________________________________________
    On 2007-05-04 15:16:16.795 I wished for a peppermint stick
    On 2007-05-04 15:16:46.875 I wished for an all expenses paid vacation
      __________________________________________________________
    Enter wish-list item (enter exit to end):
    exit
    Closed connection
    Database shut down normally
    Working With Derby JDBC program ending.