Skip to content

Apache Derby

Apache Derby, the pure-Java database: run the ij tool, connect with embedded, in-memory and network URLs, and run SQL.

2 min readUpdated

Apache Derby is a relational database written entirely in Java. It runs embedded in the application’s JVM or as a network server, and was shipped with JDK 6 to 8 as Java DB and with GlassFish as JavaDB. Its ij tool is a JDBC command line for running SQL scripts and interactive queries.

Note: the Derby developers retired the project on 2025-10-10. There will be no further releases or bug fixes; 10.17.1.0 is the last release. Existing applications keep working, but choose another embedded database (such as H2 or HSQLDB) for new projects.

Start ij

With a Derby distribution unpacked and DERBY_HOME pointing at it, start ij from the bundled JAR:

Terminal window
java -jar $DERBY_HOME/lib/derbyrun.jar ij

On JDK 8, the bundled Java DB has the same JAR under the JDK’s db/lib directory, for example $JAVA_HOME/db/lib/derbyrun.jar. JDK 9 and later no longer include it.

If $DERBY_HOME/bin is on your PATH, the ij script sets up the class path and starts the tool:

Terminal window
ij

You can also start the class directly. This only works when the Derby JARs (derbytools.jar and the database JARs) are on the CLASSPATH; otherwise Java reports NoClassDefFoundError. The setEmbeddedCP and setNetworkClientCP scripts in $DERBY_HOME/bin set it for you:

Terminal window
java org.apache.derby.tools.ij

Connect to a database

Every ij command ends with a semicolon. The connection URL decides how Derby is used:

Terminal window
ij> connect 'jdbc:derby://localhost:1527/sample;user=app;password=app';
ij> connect 'jdbc:derby:retailpos';
ij> connect 'jdbc:derby:memory:retailpos';
ij> connect 'jdbc:derby:/path/to/workspace/retailpos/retailpos';
  • jdbc:derby://localhost:1527/sample connects to a Derby network server on its default port 1527, with user and password in the URL. GlassFish’s JavaDB and the NetBeans sample database work this way.
  • jdbc:derby:retailpos opens an embedded database in the directory retailpos, relative to the Derby system home (by default the current directory).
  • jdbc:derby:memory:retailpos uses an in-memory database, which disappears when the JVM exits.
  • jdbc:derby:/path/to/... opens an embedded database at an absolute path.

Add ;create=true to an embedded or in-memory URL to create the database if it does not exist, for example connect 'jdbc:derby:memory:retailpos;create=true';.

An embedded database can be opened by only one JVM at a time. If an application server already has it open, connect through its network server instead.

To start a network server of your own from the distribution:

Terminal window
java -jar $DERBY_HOME/lib/derbyrun.jar server start

Run SQL

After connecting, type SQL statements and ij commands at the prompt. show tables lists the tables of the database:

Terminal window
ij> show tables;
ij> select * from customer;
ij> select * from product;

Leave ij with:

Terminal window
ij> exit;

References

Related notes

6 min read

SQL joins

INNER, LEFT, RIGHT, FULL OUTER, CROSS and self joins in SQL, with two small example tables, the queries and their results.

  • Databases
3 min read

Database migration: MySQL to PostgreSQL

Migrate a MySQL database to PostgreSQL with pgloader in Docker, and fix the two authentication errors you are likely to hit.

  • Databases
4 min read

Jakarta EE

Jakarta EE 10 and 11: component versions, deployment descriptor headers, Faces namespaces and a multi-module project layout.

  • Java & Jakarta EE

Type to search the whole site.