# Activity 3: Run a JDBC program using the Embedded driver

This activity loads the Derby database engine using a simple Java JDBC program. JDBC is the Java Database Connectivity API and is also the native API for Derby. The program uses the embedded driver to create (if needed) and then connect to the `jdbcDemoDB` database. You can then populate a table within the database with text. The program demonstrates some basic JDBC processing along with related error handling.

This activity assumes that you have opened a command window, navigated to the `DERBYDBS` directory, and set the DERBY_HOME environment variable.
The CLASSPATH environment variable is used by Java to locate the binary files (jarfiles and classfiles) needed to run Derby and other Java applications. Before performing this activity, you need to set the CLASSPATH and compile the `WwdEmbedded.java` Java program.

1. Copy the program files into the `DERBYDBS` directory and set the CLASSPATH:
   On Windows platforms:

         copy %DERBY_HOME%\demo\workingwithderby\* .

         set CLASSPATH=%DERBY_HOME%\lib\derby.jar;.

   On UNIX Korn Shell platforms:

         cp $DERBY_HOME/demo/workingwithderby/* .

         export 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: Only a command prompt will be displayed if the compilation is successful. The binary file `WwdEmbedded.class` will be created. If an error is displayed please verify that the Java development kit is properly installed.
3. Run the program: The `WwdEmbedded.java` program populates a table with wish list items. It is a simple Java program that prompts the User for text input (up to 32 characters), stores the text entered in a database table and then lists the items stored in the table. The program will continue to ask for wish list items until the word `exit` is entered 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 2006-09-21 15:11:50.412 I wished for a peppermint stick
         ________________________________________________________

       Enter wish-list item (enter exit to end):
       an all expenses paid vacation

         ________________________________________________________
       On 2006-09-21 15:11:50.412 I wished for a peppermint stick
       On 2006-09-21 15:12:47.024 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.

- The WwdEmbedded program
  This section describes the WwdEmbedded.java program, highlighting details specific to accessing a Derby database from a JDBC program.

Previous topic: Activity 2: Run SQL using the Client driver
Next topic: Activity 4: Create and run a JDBC program using the Client driver and Network Server