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.
Continue reading »