Advertisment

Developing Apps using Java DB

author-image
CIOL Bureau
Updated On
New Update

Rahul  Sah

Advertisment

In today’s era of information exchange where applications are being developed for Web-based access or are being made for Java-based devices (PDAs, Mobiles), it has become important to keep their size small and data secured. Java DB provides the answer to both of these concerns.

Java DB is Sun's supported one such embedded database. It is based on Java technology, open-source Apache Derby Database. It is lightweight at 2MB and embeddable within desktop Java technology applications. Such applications can now access powerful database storage with triggers, stored procedures, and can access application specific-data with JDBC as the Java DB resides on same JVM. Also as java is portable across platforms the same applies to Java DB, you can use that across all platforms. Java DB adheres to database standards such as JDBC and ANSI SQL standards. That means it provides functionality expected of a sophisticated relational database, including SQL syntax, concurrency, triggers, and backups. So an application on Java DB can be easily migrated to other open standard databases.

Advertisment
Direct Hit!
Applies To: Java Developers

USP: Developing applications with embedded Java DB

Primary Link: http://developers.sun.com/javadb

Google Keywords: Java DB, Derby

Embedded DB

Before getting to using Java DB, let’s first understand what an embedded DB is. An embedded DB is a database component that comes as a part of the application, and not as a separate running application. That means it comes packed with the main application as in-line code or linked libraries, and the application itself invokes the database.

An embedded database comes handy here as it can be packed with the application and there would not be any need to make a connection to an outside database. Being small in size is not the only feature of an embedded database as it is offers good performance along with affordability. By embedding a database into an application, the software vendor can reduce the price and cost of ownership for the customer. Also as these embedded databases require no or minimal database administration, it helps you save on maintenance costs.

Advertisment

Java based embedded DB

As the use of Java continues to expand, particularly towards the low-end, it drives the need for local persistent storage for both Java programs and data. Applications in these areas require high-performance, small-footprint, self-managing, embedded databases. That’s where Java DB comes in handy. Now let’s see how to use it to build an application.

Here is the application built by us using Java, which uses Java DB to store the data from the Web form

Application development

With this month’s CD we have given a demo of a Web application (System/Labs/PcqContacts) that uses Java DB. This application can be deployed on a local Web-server, for example Tomcat. This application is designed to store contact details of persons as in a phonebook or an address-book. It stores names, telephones, mobiles, emails, addresses and city details. And, these details can be managed, that is, edited, viewed etc, using the form based interface of the application. The database is embedded with the application, so there is no need to set up or manage a separate database server or system.

Advertisment

The derby.jar is the embedded database in the lib folder of the Java DB installation. As we develop a Web application, we need to place this derby.jar file in the WEB-INF/lib folder of our web-project, so that while deploying the web-server could extract the embedded database also.We will now discuss how to establish a connection with the database and set where the database would be residing. The rest of the application code can be sourced from the CD.

First we should load the database driver. Java DB's drivers come with the derby.jar file, so you don't need to download anything else. Load the JDBC driver by referencing it using the Class.forName method. The embedded driver name is org.apache.derby. jdbc.EmbeddedDriver, and you can load it as following.

Class.forName("org.apache.derby.jdbc.EmbeddedDriver");

Advertisment

The driver manager provides database connections. We can retrieve a database connection from the DriverManager by providing the database URL string and the set of properties that influence the connection with database. The URL string is of the form of:

jdbc:derby:

The dbName portion of the URL identifies a specific database. The easiest way to manage your database location in an embedded environment is to set the derby.system.home system property. This property tells Java DB the default home location of all databases. By setting this property, the PcqContacts demo ensures that Java DB always finds the correct application database. The application database is named as ContactsManagerDirectory, and it will exist within the directory indicated by the derby.system.home property. The connection URL for this database would look now as jdbc:derby:ContactsManagerDirectory.

Advertisment

The following code snippet shows how we set the derby.system.home property for the application. We have used .PcqContacts as the subdirectory under user’s home directory to reside the database.

private void setDBSystemDir() {

String userHomeDir = System.getProperty("user.home", ".");

String systemDir = userHomeDir + File.separator + ".PcqContacts";

System.setProperty("derby.system.home", systemDir);

File fileSystemDir = new File(systemDir);

fileSystemDir.mkdir();

}

Now since the location of database is set we can get connection to the database through DriverManager as follows:

Advertisment

DriverManager.getConnection("jdbc:derby:ContactsManagerDirectory”);

Now when the user uses the application for the first time we need to create the database on the fly in the location specified by derby.system.home property. We can create the new database by appending the create=true property to the above database URL while retrieving a connection to the database.

The PcqContacts demo will create the table Contacts under PCQ schema. We create a new Statement and call execute method to run the SQL statement so as to create the table for the database ContactsManagerDirectory. The snippet could be as follows:

Statement stmt = dbConnection.createStatement();

stmt.execute(strCreateContactsTable);

In the snippet strCreateContactsTable stores the SQL statement for creation of the table.

The rest of the coding could be done for insert, edit, update and deletion of the records from the database by using the connection retrieved through DriverManager method. For building the application we can use ANT script that will build the PcqContacts.war file. Placing that into the webapps folder of the Tomcat server can deploy this .war file. As the derby.jar is also packed with the application the server will extract it along with application. Though this demo was for a single user purpose, we can scale this application further up by adding a users table, whereby each user would be able to create a contact directory of its own.

Conclusion

Java DB provides feature rich, robust, small-footprint Java database management system that is cost effective, easy to use and simple to deploy.

Source: PCQuest

tech-news