O'Reilly logo

Java Cookbook by Ian F. Darwin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Program: DBM

This program lets you use the original Unix DBM (database access method) routines from Java. DBM is actually emulated using the newer Berkeley Database (DB) routines, but the DBM interface is more traditional, and simpler. DBM was used in Section 20.3 to provide a name-to-login database, which is similar to how many modern Unixes actually store name and password information. That recipe also showed how to use it to display your Netscape history, even under MS-Windows.

I’ll now show the Java version of the DBM library, DBM.java (see Example 26-10).

Example 26-10. DBM.java

import java.io.*; /** This class provides a dbm-compatible interface to the Unix-style * database access methods described in dbm(3) (which is on some Unixes * a front-end to db(3). * <P>Each unique record in the database is a unique key/value pair, * similar to a java.util.Hashtable but stored on persistent medium, not * kept in memory. Dbm was originally optimized for Unix for fast * access to individual key/value pairs. */ public class DBM { /** Since you can only have one DBM database in use at a time due * to implementation restrictions, we enforce this rule with a * class-wide boolean. */ protected static boolean inuse = false; /** Save the filename for messages, etc. */ protected String fileName; /** Construct a DBM given its filename */ public DBM(String file) { synchronized(this) { if (inuse) throw new IllegalArgumentException( "Only one DBM object at a time per Java Machine"); inuse = true; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required