Datasheet
Using Derby
As mentioned, Derby is automatically installed as part of the JDK. Derby provides a command-line tool
called
ij, which is an abbreviation for interactive JDBC scripting tool. This tool provides a way to con-
nect to and manipulate Derby databases. You must have the following JAR files in your classpath before
using this tool. The
derby.jar file contains the JDBC drivers, and derbytools.jar contains the ij
tool itself:
c:\Program Files\Java\jdk1.6.0\db\lib\derby.jar
c:\Program Files\Java\jdk1.6.0\db\lib\derbytools.jar
After your classpath is configured, start the tool and connect to the example database (toursdb) included
with Derby:
c:\>java org.apache.derby.tools.ij
ij version 10.2
ij> connect ‘jdbc:derby:c:\Program Files\Java\jdk1.6.0\db\demo\databases\toursdb’;
ij>
Don’t forget the semicolon at the end of a command. If you leave this off, ij may seem like it’s process-
ing a command, but it isn’t. This provides for ease of entering multiline commands such as creating
tables or complicated select statements. These semicolons are confined to
ij and are not passed to the
database.
The tool works much as you would expect it to, such as issuing a select statement to retrieve a partial
listing of data from the countries table (a table that is part of the example toursdb):
ij> select * from countries where country like ‘A%’;
COUNTRY |C&|REGION
--------------------------------------------------------
Afghanistan |AF|Asia
Albania |AL|Europe
Algeria |DZ|North Africa
American Samoa |AS|Pacific Islands
Angola |AO|Africa
Argentina |AR|South America
Armenia |AM|Europe
Australia |AU|Australia and New Zealand
Austria |AT|Europe
Azerbaijan |AZ|Central Asia
To create a new database from ij, include the parameter create=true to the connection string. Because
you’re already connected to the toursdb, first disconnect. The
select statement proves you’re discon-
nected. Then issue the new
connect statement:
ij> disconnect;
ij> select * from countries;
IJ ERROR: Unable to establish connection
ij> connect ‘jdbc:derby:DerbyTestDB;create=true’;
ij>
4
Part I: Thinking Like a Java Developer
05_777106 ch01.qxp 11/28/06 10:43 PM Page 4