Feb 042013
Article Java

Our previous post “Introduction to Berkeley DB” gave an overview of the features and functionality of this popular DBMS. This post will show practical examples of the usage of Berkeley DB Java Edition in a Java application.

Versions of Berkeley DB for Java

There are two different versions of the Java API for Berkeley DB

Berkeley DB standard

The com.sleepycat.db package is an interface to the same system library used by other languages such as C, Perl and PHP to access BDB databases. This ensures the compatibility, allowing the use in a java application of a database created by an application written in other language.

Berkeley DB Java Edition

The com.sleepycat.je package is a 100% Java package. Databases created with this package can be moved to different platforms to be used by other java applications using the same library.

This post explains how to use Berkeley DB Java Edition.

Installation of Berkeley DB Java Edition

To install Berkeley DB Java edition, navigate in a browser to the Berkeley DB downloads page:


From that page, download the file Berkeley DB Java Edition 5.0.58.tar.gz (13M)

To install, just uncompress and extract the files in a directory.

Note: If you chose to download the zip-compressed version of the file, use the -U option to preserve the capitalization of the file names.

Once installed, the file je-5.0.58/lib/je-5.0.58.jar must be added to the classpath to compile an run a java application that uses it.

In the directory je-5.0.58/lib  there are also other versions of the library, including one for the Android platform:

Creating, opening and closing a database

The following code can be used to open a database, or create a new database:

In Berkeley DB Java Edition, an object of class Environment is mandatory to work with a BDB database. The first step in opening the database is creating this Environment object will a call to “new Environment”. The first argument to this call is the path where the data of the environment (including the databases in it) is located. The second argument is an object of class “EnvironmentConfig”. The call to “envConfig.setAllowCreate(true)” tells the constructor of the Environment object to create the required files if the environment did not previously exist.

Next, the database is opened with a call to “Environment.openDatabase”. This method receives as arguments the name of the database to be opened, and an object of class “DatabaseConfig”. In the same way as with the creation of the environment, a call to setAllowCreate(true) in the database configuration tells the constructor to create a new database if it did not exist.

When the application finishes making use of them, both the database and the environment must be closed like this:

The example program “TestBDB.java” is compiled and executed with the commands:

Writing records to the database

A record is written to the database with a call to the “put()” method of the Database object. This method receives as arguments two objects of the class DatabaseEntry, holding the key and the value of the record.

Reading records from the database

The “get()” method in class Database can be used to retrieve a record. This method returns an object of class DataEntry, that must be converted to the desired object type.

Deleting records

The “delete” method in the Database class is used to delete a record from the database:

Process all records in the database

The database can be traversed with the help of a cursor:



Getting Started with Berkeley DB Java Edition

 Posted by at 5:18 pm

  2 Responses to “How to use Berkeley DB Java Edition”

  1. The best simple example and explanation. Thank you.

  2. Useful examples. thanks!

 Leave a Reply