Thursday, 7 August 2008

Hacking Java DB

Introduction to Java DB

Java DB is a pretty mature technology, that evolved out of IBM Cloudscape.
I like it better than some of the other offerings out there like HypersonicDB.

Java DB 10.4 using JDBC4 has a documented API and developers guide.
It uses an Apache 2.0 license. If you need to recap what JDBC 4.0 is all about, here's an old article on Artima.com with explanations.

To verify Derby has been properly installed on your system use the sysinfo tool.

java org.apache.derby.tools.sysinfo

should return your Java version, vendor and other configuration information.

Getting started with Java DB using ij

ij is the interactive Java tool for querying Derby. To test it out create and connect to a new database as follows.

ij> connect 'jdbc:derby:MyDbTest;create=true';
ij> exit;

This creates a directory MyDbTest and a file derby.log. Now you can connect to the new database:

ij> connect 'jdbc:derby:MyDbTest';
ij> show schemas;

will show all the schemas in the database e.g. APP, SYS, SQLJ.

Aliasing Makes Life Easier

Tip: to make the shell automatically recognise the Derby protocol, you can alias ij to the following:

alias ij="java -Dij.protocol=jdbc:derby: org.apache.derby.tools.ij"

See how easy this makes creating and connecting to a database:

connect 'TestDb;create=true';
connect 'TestDb';

Brilliant!

Note: -D is the standard way to set command-line flags in ij. If you have more than one flag to set, just do -D multiple times.

Embedded and Client-server Mode

Derby has both an embedded and client-server mode. In embedded mode, a Derby database may not be used concurrently between two VMs.

No comments:

Blog Archive