Getting Started with Apache Derby (Java DB)

Apache Derby (or Java DB) provides a small set of libraries that can be added directly to any Java program that needs database storage capabilities, such as a desktop application, network service, or Web application. Generated Derby databases may also be bundled with your application, to remove the need for any separate installation or configuration. To support this, the Derby libraries may directly read database files that are held in JAR archives. The documentation refers to this as the embedded mode.

As a stand-alone network server, Derby supports online backup, replication between a master and multiple slaves, XA transactions, LDAP user authentication, and SSL/TLS for encrypted connections. The standard distribution includes a Web interface for Derby services in the form of a Java servlet for you to install on your preferred applications server. You may, of course, control Derby instances with your own code through the APIs. Derby also complies with the Java Management Extensions (JMX) standard for remote monitoring and management.

In either mode, Derby has all of the features that you would expect from a modern SQL database including transactions, stored procedures, views, triggers, and XML data types. Derby supports encrypted database files.

You can access Derby database files and servers from any external JRuby or Java application, by using the bundled JDBC drivers. Command-line utilities are provided for developers and administrators. The supplied documentation is comprehensive.

Java DB is simply the brand name for versions of Apache Derby that are packaged and supported by Sun Microsystems. The software is the same as that released by the Derby project of the Apache Software Foundation. Apache Derby and Java DB are covered by the Apache Software License, an open source license that permits the product to be freely used in both open source and proprietary applications.

As a historical note, Derby was originally released as a commercial product called Cloudscape. IBM bought it, and subsequently donated it to the Apache Foundation for development as an Open Source project.

Installing Java DB

For convenience, this section only gives instructions for Java DB, the Sun build of Derby.

Installing Java DB on Windows

Sun bundles Java DB with their Java Development Kit (JDK), and this JDK is the simplest way to install a JRE and Java DB on Microsoft Windows. You can download the JDK from the Sun Website:

If you want to use Java DB to provide databases for your applications, choose the Java DB component from the list of options when you install the Sun JDK.

Remember to add the JDK to the beginning of your PATH environment variable. To do this, choose Start > Control Panel > System > Advanced > Environment Variables, find path in the System Variables list, and Edit it to add:

C:\Program Files\Java\jdk1.6.0_06\bin\

Substitute jdk1.6.0_06 with the correct directory name for your JDK version.

Items in the PATH must be separated by semi-colons.

To test this, open a Command Prompt window and type:

java -version

To use Java DB, add the directory for the Java DB program files to your PATH:

C:\Program Files\Sun\JavaDB\bin\

Java DB also requires a separate DERBY_HOME environment variable that points to the root directory for your Java DB installation:

Installing Java DB on Linux

For Linux, Sun collaborates with other vendors and the community on a fully open source JDK (the OpenJDK). All of the major Linux distributions include packages for the OpenJDK.

To install the OpenJDK on an Ubuntu system:

sudo apt-get install openjdk-6-jdk

The OpenJDK does not include Java DB. To install the correct version of Java DB on Ubuntu:

sudo apt-get install sun-javadb-client sun-javadb-core

Note that the Ubuntu repositories include other, older Java DB packages. Use the sun-javadb-* packages with the OpenJDK, not the sun-java6-javadb or sunwderby packages.

To enable Java DB, add the bin directory for Java DB to your PATH environment variable, and set a new DERBY_HOME environment variable. On Ubuntu and Debian systems, add these lines to the end of the .bashrc file in your home directory:

This will take effect the next time that you login or open a new terminal window.

Administration Tools

Three command-line administration utilities are supplied with Derby:

dblook, for exporting schema information

ij, an interactive SQL command shell

The sysinfo diagnostics utility

If you have added the environment variables, you may run any of these commands by typing the name of the utility in a terminal window.

Derby also includes built-in system procedures for administrative tasks, such as backup, consistency checking, and bulk import and export. All of these are provided in the SYSCS_UTIL schema, so that you may run them from any SQL connection.

This article includes some brief notes on ij below, but the supplied Guides are the definitive resource on using the applications and system procedures.

Creating and Using Derby Databases

Derby databases are stored as a directory with several subdirectories. You use a standard URL syntax to refer to either local or remote databases in commands and connection strings. The supplied ij command-line utility can connect to both local and remote databases.

For a local database, the syntax looks like this:

jdbc:derby:databaseName;URLAttributes

By default, Derby will simply check the current working directory for a subdirectory with the specified name. You may override this, and explicitly set the default parent directory that Derby should check with the property (Java setting) derby.system.home.

Alternatively, you can specify a relative or absolute path to the database directory itself in the JDBC string:

jdbc:derby:/srv/databases/thisdb
jdbc:derby:../databases/thisdb

The recommended practice is to define a root databases directory for your application and place all of the databases for that application in it. Configure your application to use the embedded Derby JDBC driver when the databases will be part of that application and executed by the same instance of the Java runtime (the JVM).

Derby does not require or support a CREATEDATABASE statement. To create a new database, simply append create=true to the connection string. For example, if you start ij and run this statement, it automatically creates a new database in the current working directory:

CONNECT 'jdbc:derby:mydb;create=true';

Once your connection is active, you may immediately run further statements against the new database, such as CREATETABLE.

To create a new database for a running Derby service, include the create option in the connection URL in exactly the same way:

There is also no separate command to drop a database. To destroy a database, just detach it from any service that might access it, and then delete the database files.

Using the ij Utility

The ij utility does not connect to any database until you explicitly specify at least one connection. You may define connections either as part of the command, in a properties (settings) file, or with a CONNECT statement once ij has started. You may have multiple open connections and switch between them.

To specify connections or other properties as part of the command, use the -D switch. The ij.connection property creates a connection that has whatever name you specify after the period, and assigns it the JDBCURL that you give. In the example above, the new connection has the name myconnection.

You may use ij to run scripts, and, optionally, output the results to a text file. Simply add the names of the required files to the command-line:

ij myscript.sql
ij myscript.sql output.txt

Running Derby as a Service

Refer to the supplied Server and Administration Guide for information on running and maintaining a Derby service.

By default, Derby services only accept connections from the local system (localhost). If you permit network access, note that you need to explicitly enable TLS/SSL encryption, and also user authentication. Derby refers to TLS/SSL protected access as peerAuthentication.

If you enable user authentication then you may either maintain accounts within Derby, or work with an external directory service (such as LDAP). The Security section of the Developers Guide provides an overview.

The Web interface is supplied as a WAR file in the lib subdirectory of your Derby installation: derby.war. Refer to the Server and Administration Guide for more information.

Using Derby with JRuby on Rails

JRuby on Rails supports both Java DB network database servers, and embedded copies of Java DB. In the latter case, Java DB works in the same way as SQLite works with the standard distribution of Rails – databases are generated as required inside the application itself.

To use Java DB embedded databases, specify the Derby adapter and a path in the config/database.yml configuration file of your JRuby on Rails application:

To configure your Rails application to use a Java DB network server, your database.yml settings must specify the JDBC Active Record adapter, set the Derby ClientDriver as the driver, and provide a URL. For example: