This example program is a minimal JDBC application written in Java. JDBC is the primary API for interacting with Apache Derby using Java. This program:
This program is intended to run in Virtual Machines for the Java Platform supporting Java SE 6 or newer.
In embedded mode, the application starts up an instance of Derby within the current Java Virtual Machine (JVM) and shuts down the instance before it completes. No network access is involved. Only one JVM can access a database at a time.
In a client/server environment, the application demonstrates the use of the Derby network client driver by connecting to the Network Server and running the demo. Note that the client drivers allow multiple instances of the application to run at the same time. However, the SQL operations performed by this demo will cause failures when multiple simultaneous instances of the application are run. Use of a client driver to connect to the Network Server in this application is intended only to demonstrate this type of connection. The SimpleApp demo is not suited for simultaneous executions because it creates and drops the table on which it operates.
Note that in this document, file paths and environment variables are referenced using UNIX notation unless noted otherwise. This means that if, for example, you are using a Windows platform, you must substitute file separators, path separators and environment variable references to the Windows-specific notation, for example:
Element | Example, UNIX | Example, Windows |
---|---|---|
File separator | /home/path/file | c:\path\file |
(CLASS)PATH separator | derby.jar:derbytools.jar | derby.jar;derbytools.jar |
Environment Variable reference | $DERBY_HOME | %DERBY_HOME% |
Also note that this document refers to the JVM launch command as java, and that it is being assumed that the JVM installation's launcher is available via the system's PATH. Refer to the documentation of your JVM / runtime environment for details on how to run a Java program using that JVM.
If you decide to modify the demo application code, or use it as a model for your own JDBC application, be aware that any change may affect the behavior of the program and the results from running it. For example, the data verification code is very specific to the data this demo inserts into the database, and will not work without modification with other databases.
For more information about how to use Derby, refer to the included documentation and the Apache Derby web site.
If you are relatively new to databases or JDBC, the Oracle JDBC basics tutorial may be a good starting point.
Before running this demo, you should see the following files and directories in the demo/programs/simple/ directory:
This file.
Source code for the example program. Examine this file to see how the application behaves in the various environments.
Properties file for the Derby system. This demo program runs with default settings, but you can use this file to tune Derby if you want to.
The compiled class file, runnable by Java SE JVMs.
After running the demo, you will see some new files and directories. These will be located in the directory that the system property derby.system.home points to, or the current directory (user.dir) if derby.system.home is not set for the embedded or Network Server JVM. If you are following the instructions on this page, you will find the new files in the directory demo/programs/simple/, relative to this Derby installation. New files are:
The directory that makes up the derbyDB database. You must not modify anything in this directory, or you will corrupt the database. The directory was created when the application connected with Derby, using the attribute create=true in the database connection URL. The database name, derbyDB, was also set in the database connection URL.
The directory that holds the database transaction logs for the derbyDB database.
The directory that holds the data for the derbyDB database.
An internal file that holds boot-time configuration parameters for the derbyDB database; do not edit.
The log file with Derby progress and error messages.
To remove the effects of running the demo program, simply delete the database directory (derbyDB) and derby.log. Note that if you are running the demo in a client/server environment you most likely need to shut down the Derby Network Server before being able to delete the database directory.
The Derby embedded driver is a JDBC driver included in binary distributions of Apache Derby, in the directory $DERBY_HOME/lib/. The embedded driver should be used when you want to run Derby in the same JVM as your application, for example when no direct network access to the database system is needed.
Class name: | org.apache.derby.jdbc.EmbeddedDriver |
Library: | derby.jar |
UNIX (ksh/bash)
export DERBY_HOME=/home/user/derby/db-derby-10.x.y.z-bin
Windows:
set DERBY_HOME=c:\programs\derby\db-derby-10.x.y.z-bin
This may be done as follows:
UNIX (ksh/bash):
export CLASSPATH=.:${DERBY_HOME}/lib/derby.jar
WINDOWS:
set CLASSPATH=.;%DERBY_HOME%\lib\derby.jar
You run the utility like this:
java org.apache.derby.tools.sysinfo -cp arguments
So for the arguments you need here, run it like this (all on one line):
java org.apache.derby.tools.sysinfo -cp embedded SimpleApp.class
If your environment is set up correctly, the utility shows output indicating success. It looks like this:
FOUND IN CLASS PATH: Derby embedded engine library (derby.jar) /home/user/derby/db-derby-10.x.y.z-bin/lib/derby.jar user-specified class (SimpleApp) /home/user/derby/db-derby-10.x.y.z-bin/demo/programs/simple SUCCESS: All Derby related classes found in class path.
If something is missing from your classpath environment, the utility indicates what is missing. For example, if you neglected to add the directory containing the SimpleApp class to your classpath, the utility would indicate as such:
FOUND IN CLASS PATH: Derby embedded engine library (derby.jar) /home/user/derby/db-derby-10.x.y.z-bin/lib/derby.jar NOT FOUND IN CLASS PATH: user-specified class (SimpleApp) (SimpleApp not found.)
java SimpleApp
The demo program's default framework is embedded, so there is no need to specify this explicitly.A successful run produces the following output:
SimpleApp starting in embedded mode Connected to and created database derbyDB Created table location Inserted 1956 Webster Inserted 1910 Union Updated 1956 Webster to 180 Grand Updated 180 Grand to 300 Lakeshore Verified the rows Dropped table location Committed the transaction Derby shut down normally SimpleApp finished
If any error messages appear, and you are unable to resolve the error(s), ask for help on the derby-user mailing list.
You will need to set up both the client process and the server process to run the demo application as a client connecting to the Network server. The Network Server needs to be running before you can connect using a client driver. This demo includes support for the Derby client driver.
You must start the Network Server before trying to run the demo application in one of the client modes.
Examples:
UNIX (ksh/bash):
export DERBY_HOME=/home/user/derby/db-derby-10.x.y.z-bin
Windows:
set DERBY_HOME=c:\programs\derby\db-derby-10.x.y.z-bin
Examples:
UNIX (ksh/bash):
java -jar $DERBY_HOME/lib/derbyrun.jar server start
Windows:
java -jar %DERBY_HOME%\lib\derbyrun.jar server start
When the server starts, the server console output will resemble something like this:
Security manager installed using the Basic server security policy. Apache Derby Network Server - <version> - (<svnrevision>) started and ready to accept connections on port 1527 at <timestamp>
(When you are done with the demo, you may shut down the network server for example by passing the arguments server shutdown to derbyrun.jar in a new command window.)
A note on running the Network ServerYou may start the server in a number of other ways, including:
Note that unless you set the system property derby.system.home, Derby's default database location is the working directory of the Network Server JVM. |
The Derby client driver is a JDBC driver included in binary distributions of Apache Derby, in the directory $DERBY_HOME/lib/. It is recommended that you use this driver to connect to the Derby Network Server, as this client driver is developed and maintained by the Apache Derby development community.
Class name: | org.apache.derby.jdbc.ClientDriver |
Library: | derbyclient.jar |
UNIX (ksh/bash):
export DERBY_HOME=/home/user/derby/db-derby-10.x.y.z-bin
Windows:
set DERBY_HOME=c:\programs\derby\db-derby-10.x.y.z-bin
This may be done as follows:
UNIX (ksh/bash):
export CLASSPATH=.:${DERBY_HOME}/lib/derbyclient.jar
WINDOWS:
set CLASSPATH=.;%DERBY_HOME%\lib\derbyclient.jar
You run the utility like this:
java org.apache.derby.tools.sysinfo -cp arguments
So for the arguments you need here, run it like this (all on one line):
java org.apache.derby.tools.sysinfo -cp client SimpleApp.class
If your environment is set up correctly, the utility shows output indicating success. It looks like this:
FOUND IN CLASS PATH: Derby Client libraries (derbyclient.jar) /home/user/derby/db-derby-10.x.y.z-bin/lib/derbyclient.jar user-specified class (SimpleApp) /home/user/derby/db-derby-10.x.y.z-bin/demo/programs/simple SUCCESS: All Derby related classes found in class path.
java SimpleApp derbyclient
The derbyclient argument tells the program to use the Derby client driver instead of its default driver (the embedded driver).
A successful run produces the following output:
SimpleApp starting in derbyclient mode Connected to and created database derbyDB Created table location Inserted 1956 Webster Inserted 1910 Union Updated 1956 Webster to 180 Grand Updated 180 Grand to 300 Lakeshore Verified the rows Dropped table location Committed the transaction SimpleApp finished
If any error messages appear, and you are unable to resolve the error(s), ask for help on the derby-user mailing list.
Last updated for Apache Derby Version 10.11