The Abook Database
By dananourie on Nov 20, 2007
In several previous issues of Java Technology Fundamentals, we looked at various parts of the Abook application. If you missed those articles, they are listed at the end of this one. In this article, we look at the Abook database.
The database behind the Abook application is rather simple. If a database does not yet exist when you start the application, the application creates one. You don't even need a database server, have to add your own database driver, or need to modify the application to connect to your local installation of some database system.
All of that comes prepackaged with the application and the Java
Runtime Environment, thanks to the Apache Derby
project. Sun provides JavaDB
as its supported distribution of Derby with Java SE 6, though the
derby.jar file is packaged with the Abook
To see whether the database needs to be created, the system looks
for the necessary files in the user's home directory under an
.addressbook subdirectory. This is done with the help
derby.system.home system property, set by the
application itself. When the files are not found, an
APP.ADDRESS table will be created with all the
necessary fields previously seen in the user interface (UI) screens.
Looking in either of the Dao classes found in the
abook.db package --
AddressBookDao -- you'll find the
TABLE string used to create the table:
private static final String strCreateAddressTable = "create table APP.ADDRESS (" + " ID INTEGER NOT NULL PRIMARY KEY GENERATED " "ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1)," + " LASTNAME VARCHAR(30), " + " FIRSTNAME VARCHAR(30), " + " NICKNAME VARCHAR(30), " + " PHONE VARCHAR(20), " + " EMAIL VARCHAR(30), " + " ADDRESS1 VARCHAR(30), " + " ADDRESS2 VARCHAR(30), " + " CITY VARCHAR(30), " + " STATE VARCHAR(30), " + " POSTALCODE VARCHAR(20), " + " COUNTRY VARCHAR(30), " + " WEB_URL VARCHAR(50)," + " CHAT_AIM VARCHAR(50)," + " CHAT_YIM VARCHAR(50)," + " CHAT_MSN VARCHAR(50)," + " NOTE VARCHAR(300)," + " PICTURENAME VARCHAR(100)," + " PICTURE BLOB (64K)" + ")";
With 17 text fields, one image named
BLOB, and an
identity field, the table has a total of 19 columns.
All the code to access and manipulate the database is found in the Dao classes. With only a single table and no joins necessary, there is very little complexity to the system.
But a little bit more needs to be said about the database. As previously mentioned, you don't need to modify the application to connect to your local database system for this to work. The open-source Apache Derby 100% Java technology database is present: Java DB, Sun's supported distribution, is a standard part of the Java SE 6 release. In other words, you don't have to install your own database any more for applications. Java DB is not only for toy applications. It is fully capable for real-world use. And like the Java platform, it runs wherever you have the platform available.
NetBeans provides some additional tools to help you connect to a database and to view or manipulate what is available there. However, because Abook uses the embedded version of Derby, you cannot connect to it from the NetBeans IDE.
If instead you configure Derby using a more typical client-server architecture, where the server is running separately from the client application, you can use the NetBeans IDE to attach to that server and monitor or manipulate what's available in the database. If you are interested in trying out JavaDB with NetBeans in this fashion, use this NetBeans tutorial. A second tutorial, Connecting a GUI to a Java DB Database With NetBeans IDE, is also available. And if you are using the NetBeans 6.0 beta version, use the tutorial Building a Java Desktop Database Application.The Abook articles started with:
For Previous Articles on Abook: