Datasheet

The database name (in this case, DerbyTestDB) is created as a subdirectory of the directory where you
started the
ij tool. The database appears on disk in the directory C:\DerbyTestDB. Exploring this
directory is strictly for curiosity’s sake you should never have to modify any file in this directory,
including the
service.properties file that may seem tempting to play with. The creation of a
database also creates a
derby.log file at the same level as the DerbyTestDB, so the file in this case is
C:\derby.log. This log file is an error log and is useful to check to get more information about the
inevitable problems that arise during real development. If you create multiple databases they will share
this log file.
Now that you have a new database, create a table, insert some data, and query it:
ij> create table zipcodes(zipcode varchar(5), city varchar(20), state varchar(2));
0 rows inserted/updated/deleted
ij> insert into zipcodes values (‘22030’, ‘Fairfax’, ‘VA’);
1 row inserted/updated/deleted
ij> insert into zipcodes values (‘22003’, ‘Annandale’, ‘VA’);
1 row inserted/updated/deleted
ij> insert into zipcodes values (‘90210’, ‘Beverly Hills’, ‘CA’);
1 row inserted/updated/deleted
ij> select * from zipcodes;
ZIPC&|CITY |STA&
-------------------------------
22030|Fairfax |VA
22003|Annandale |VA
90210|Beverly Hills |CA
3 rows selected
ij>
By default, auto-commit is on from the ij tool, so you don’t have to issue the commit; command to
save changes to the database. You can control auto-commit by issuing the command “autocommit on;”
or “autocommit off;” Type “exit;” to exit from the
ij tool.
Now that you have seen the basics of using the
ij tool, look at an example of querying your newly cre-
ated database from a Java program using the JDBC driver. Because the standard JDBC mechanism is
used, there are no surprises with the import statements:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class DerbyTestDBClient {
public static void main(String[] args)
{
DerbyTestDBClient testClient = new DerbyTestDBClient();
testClient.showZipCodes();
}
5
Chapter 1: Key Java Language Features and Libraries
05_777106 ch01.qxp 11/28/06 10:43 PM Page 5