Dec 062013
 
Article Java

In our previous post “Introduction to Berkeley DB“, the characteristics and functionality of this type of database has already been introduced. This post presents sample code to use a Berkeley DB in a Java program.

Versions of the Berkeley DB Java library

There are two different versions of the Java library for Berkeley DB:

Berkeley DB standard

The package com.sleepycat.db implements an interface to the same system library used by other languages such as C, Perl and PHP. This ensures the compatibility, un such a way that a Berkeley database previously existing, created by a program written in other language, can be used by a Java program, and viceversa.

Berkeley DB Java Edition

The package com.sleepycat.je is a 100% Java implementation. This ensures the portability of databases created with this library among different platforms.

From the programmer’s point of view, the main difference is that in Berkeley DB Java Edition, creating and opening a database has to be done using the class com.sleepycat.je.Environment.In Berkeley DB standard, using this class is optional.

Other than that, the code used to work with berkeley databases in both libraries is almost identical. It is usually enough to just replace the references to “com.sleepycat.je.*” with “com.sleepycat.db.*” in the import clauses at the top of the source code.

The sample code in this post is written to use Berkeley DB standard.

Installation

It is always possible to install Berkeley DB from scratch, downloading and compiling the source code from the Oracle Berkeley DB download page.

However, there are binary packages available for most platform, making must easier the installation.

For instance, on Debian, Ubuntu, and other linux distros, the installation in done just using apt-get to install the system and java libraries:

once the installation is done, we can check that the java library (that we will need to add to the classpath to compile and run our program) is located under “/usr/share/java”:

Creating, opening and closing a database

The following sample code “TestBDB.java” opens a Berkeley database in a file “test.db”.

First, a “Database” object is created with a call to “new Database()”, passing a “dbConfig” argument that specifies the database configuration.

In that configuration, the flag set with the call to “setAllowCreate(true)”, requests that a new database be created, if it does not exist yet.

When all intended accesses to the database have been done, it should be closed. This can be done with the following sample code:

The source code in “TestBDB.java”, is compiled and executed with the commands:

Writing records to the database

A new record is inserted in the database with a call to the “put()” method in the Database object. The arguments passed to the put() method are two “DatabaseEntry” objects, with the key and the value of the record.

The following code is identical to the code used in Berkely Java Edition, with the only difference of the import clauses at the top, that are importing the classes com.sleepycat.db.* instead of com.sleepycat.je.*

Reading records from the database

A record is retrieved from the Berkeley database with a call to the “get()” method in the Database object. This method returns an object of class DataEntry, that must be cast into the desired type.

Also in this case, the code is the same as the code used with Berkeley Java Edition, with the only difference of the import clauses:

Deleting records

Records are deleted from the database with a call to the “delete” method in the Database object:

Process all records in the database

To go through all records in the database, an object of type “Cursor” is created:

 

References

Getting Started with Berkeley DB

Index of posts related to Java programming

 Posted by at 10:47 am

 Leave a Reply

(required)

(required)