Getting started with Derby – The java database bundled with Java 6


I often need a database to perform some quick tests. I usually start my Oracle instance. This takes some time and for most things it’s overkill. So a database that starts up quickly and is easy to use and install was on my wish list.
Derby is very easy to install and because it’s bundled with Java 6 you don’t even have to download or install it (not that downloading 2 Mb is such a bad thing by the way).
My main goal is to get you up and running with Derby in no-time. There are enough reviews of Derby and strangely enough it’s also documented pretty well.


Derby was named Cloudscape and JBMS, so that explains why there is a version 10 and you never heard of Derby before. Derby is bundled with JDK 1.6.0. You can find the files that make up Derby in the \db\lib directory of your JDK.

Today I will show how to run Derby as a network server. It is possible to run an embedded version of Derby, but for most web projects I prefer to keep my database and code separated. The purpose of using Derby for me is a temporary replacement  for a ‘normal’ database like Oracle or MySQL.

Creating the database

The databse consists of a directory with some files in it. Go to a directory on your file system where you want the database to be created and perform the following command:

java -jar %JAVA_HOME%\db\lib\derbyrun.jar ij

ij is the command line tool for your database. I couldn’t find out how they came up with the name of this tool, probably just two random characters. Create and connect to the database on the command line:

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

Derby does not have a CREATE DATABASE command, this is the only way to create a database.

Let’s create a table and add some records to it:

CREATE TABLE EMP<br />(<br />&nbsp; EMPNO&nbsp;&nbsp;&nbsp;&nbsp; INT,<br />&nbsp; ENAME&nbsp;&nbsp;&nbsp;&nbsp; VARCHAR(10),<br />&nbsp; JOB&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; VARCHAR(9),<br />&nbsp; MGR&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; INT,<br />&nbsp; HIREDATE&nbsp; DATE,<br />&nbsp; SAL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NUMERIC(7,2),<br />&nbsp; COMM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NUMERIC(7,2),<br />&nbsp; DEPTNO&nbsp;&nbsp;&nbsp; NUMERIC(2)<br />);<br /><br />CREATE UNIQUE INDEX PK_EMP ON EMP (EMPNO);<br /><br />INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO ) VALUES ( 7839, 'KING', 'PRESIDENT', NULL, '11/17/1981', 5000, NULL, 10); <br />INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO ) VALUES ( 7698, 'BLAKE', 'MANAGER', 7839, '05/01/1981', 2850, NULL, 30);<br />INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO ) VALUES ( 7782, 'CLARK', 'MANAGER', 7839, '06/09/1981', 2450, NULL, 10);<br />INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO ) VALUES ( 7566, 'JONES', 'MANAGER', 7839, '04/02/1981', 2975, NULL, 20);

This is just a simple table as an example. Derby is a full blown database with support for views, triggers, stored procedures and even an XML data type.

You can use the ij tool to see the results of a select * from emp;
But you probably believe me when I say that you’ll get some results. Let’s run Derby in network mode and hook it up via JDBC to a new application.

Starting and stopping the network server

Go to the directory where your database is created and execute the following command:

java -jar %JAVA_HOME%\db\lib\derbynet.jar start

You will see a message that Derby is started:

Apache Derby Network Server - - (453926) started and ready to accept connections on port 1527 at 2007-05-22 09:29:29.225 GMT

To shutdown the database:

java -jar %JAVA_HOME%\db\lib\derbynet.jar shutdown

The shutdown command can be performed in any directory. The JDBC url of this database is jdbc:derby://localhost:1527/firstdb

Connecting via JDBC

The driver needed to connect to Derby via JDBC is org.apache.derby.jdbc.ClientDriver. This driver can be found in the derbyclient.jar file (also located in the db\lib dir of your Java 6 JDK). Since I do everything with Maven2 my dependency is

&lt;dependency&gt;<br />&nbsp;&nbsp;&nbsp; &lt;groupId&gt;org.apache.derby&lt;/groupId&gt;<br />&nbsp;&nbsp;&nbsp; &lt;artifactId&gt;derbyclient&lt;/artifactId&gt;<br />&nbsp;&nbsp;&nbsp; &lt;version&gt;;/version&gt;<br />&lt;/dependency&gt;

Maybe it is possible to use a dependency from Sun, but this way I’m sure I have an up to date version of the client.

I created a simple Spring project to run our select * from emp test from a Java environment. I added two beans to my Spring config file:

<code><font color="#ffffff">&nbsp;&nbsp;&nbsp; </font><font color="#000000">&lt;bean&nbsp;id=</font><font color="#2a00ff">&quot;dataSource&quot;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#7f0055"><strong>class</strong></font><font color="#000000">=</font><font color="#2a00ff">&quot;org.springframework.jdbc.datasource.DriverManagerDataSource&quot;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">destroy-method=</font><font color="#2a00ff">&quot;close&quot;</font><font color="#000000">&gt;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">&lt;property&nbsp;name=</font><font color="#2a00ff">&quot;driverClassName&quot;&nbsp;</font><font color="#000000">value=</font><font color="#2a00ff">&quot;org.apache.derby.jdbc.ClientDriver&quot;</font><font color="#000000">/&gt;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">&lt;property&nbsp;name=</font><font color="#2a00ff">&quot;url&quot;&nbsp;</font><font color="#000000">value=</font><font color="#2a00ff">&quot;jdbc:derby://localhost:1527/firstdb&quot;</font><font color="#000000">/&gt;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp; </font><font color="#000000">&lt;/bean&gt;</font><br /><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp; </font><font color="#000000">&lt;bean&nbsp;id=</font><font color="#2a00ff">&quot;testDao&quot;&nbsp;</font><font color="#7f0055"><strong>class</strong></font><font color="#000000">=</font><font color="#2a00ff">&quot;nl.amis.TestDao&quot;</font><font color="#000000">&gt;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">&lt;property&nbsp;name=</font><font color="#2a00ff">&quot;dataSource&quot;&nbsp;</font><font color="#000000">ref=</font><font color="#2a00ff">&quot;dataSource&quot;</font><font color="#000000">/&gt;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp; </font><font color="#000000">&lt;/bean&gt;</font></code>

Then a simple DAO to get some records and print them to the debug console:

<code><font color="#7f0055"><strong>package&nbsp;</strong></font><font color="#000000">nl.amis;</font><br /><br /><font color="#7f0055"><strong>import&nbsp;</strong></font><font color="#000000">org.apache.log4j.Logger;</font><br /><font color="#7f0055"><strong>import&nbsp;</strong></font><font color="#000000">org.springframework.jdbc.core.JdbcTemplate;</font><br /><font color="#7f0055"><strong>import&nbsp;</strong></font><font color="#000000">org.springframework.jdbc.core.namedparam.NamedParameterJdbcDaoSupport;</font><br /><font color="#7f0055"><strong>import&nbsp;</strong></font><font color="#000000">java.util.List;</font><br /><br /><font color="#7f0055"><strong>public&nbsp;class&nbsp;</strong></font><font color="#000000">TestDao&nbsp;</font><font color="#7f0055"><strong>extends&nbsp;</strong></font><font color="#000000">NamedParameterJdbcDaoSupport&nbsp;</font><font color="#000000">{<br /></font><font color="#ffff
ff">&nbsp;&nbsp;&nbsp; </font><font color="#7f0055"><strong>private&nbsp;static&nbsp;final&nbsp;</strong></font><font color="#000000">Logger&nbsp;log&nbsp;=&nbsp;Logger.getLogger</font><font color="#000000">(</font><font color="#000000">TestDao.</font><font color="#7f0055"><strong>class</strong></font><font color="#000000">)</font><font color="#000000">;</font><br /><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>public&nbsp;</strong></font><font color="#7f0055"><strong>void&nbsp;</strong></font><font color="#000000">getEmp</font><font color="#000000">()&nbsp;{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">JdbcTemplate&nbsp;template&nbsp;=&nbsp;getJdbcTemplate</font><font color="#000000">()</font><font color="#000000">;</font><br /><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">List&nbsp;list&nbsp;=&nbsp;template.queryForList</font><font color="#000000">(</font><font color="#2a00ff">&quot;SELECT * FROM EMP&quot;</font><font color="#000000">)</font><font color="#000000">;</font><br /><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">log.debug</font><font color="#000000">(</font><font color="#000000">list</font><font color="#000000">)</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp; </font><font color="#000000">}</font><br /><font color="#000000">}</font></code>

Of course you can use plain JDBC to connect to the database, for me Spring is the fastest way to connect to a database and print formatted debug statements (instead of something like ResultSet@2535).


Derby is around for a while and I did a Hibernate course with it (but then everything was set up for me and I couldn’t imagine how easy it was to install and run Derby). But I should’ve worked with it a lot earlier. Derby isn’t some kind of ‘stupid’ database where you are limited in it’s usage. I was surprised Derby supports the XML Data Type. Before I started this article I thought of Derby as a temporary replacement for a normal database. But Derby IS a normal database and I’m going to use it for my home brewn projects in the near future.



About Author

1 Comment