Berkeley DB JE is a general-purpose, transactionally protected, embedded database written in 100% Java (JE makes no JNI calls). As such, it offers the Java developer safe and efficient in-process storage and management of arbitrary data.
import com.sleepycat.je.DatabaseException;import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;import java.io.File;
Environment myDbEnvironment = null;
Database myDatabase = null;
try { // Open the environment. Create it if it does not already exist.
EnvironmentConfig envConfig = new EnvironmentConfig();
envConfig.setAllowCreate(true);
myDbEnvironment = new Environment(new File("/export/dbEnv"), envConfig);
// Open the database. Create it if it does not already exist.
DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setAllowCreate(true);
myDatabase = myDbEnvironment.openDatabase(null, "sampleDatabase",
dbConfig); } catch (DatabaseException dbe) { // Exception handling goes here}
import com.sleepycat.je.DatabaseEntry;import com.sleepycat.je.Database;
import com.sleepycat.je.LockMode;import com.sleepycat.je.OperationStatus;
.....// Environment and database opens omitted for clarity.
// Environment and database may be opened read-only.
String aKey = "myFirstKey";try { // Create a pair of DatabaseEntry objects. theKey
// is used to perform the search. theData is used // to store the data returned by the get() operation.
DatabaseEntry theKey = new DatabaseEntry(aKey.getBytes("UTF-8"));
DatabaseEntry theData = new DatabaseEntry(); // Perform the get.
if (myDatabase.get(null, theKey, theData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
// Recreate the data String.
byte[] retData = theData.getData();
String foundData = new String(retData);
System.out.println("For key: '" + aKey + "' found data: '" + foundData + "'."); } else {
System.out.println("No record found for key '" + aKey + "'."); } } catch (Exception e) {
// Exception handling goes here}
import com.sleepycat.je.DatabaseEntry;import com.sleepycat.je.Database;
// Environment and database opens omitted for clarity.
// Environment and database can NOT be opened read-only.
try {
String aKey = "myFirstKey";
DatabaseEntry theKey = new DatabaseEntry(aKey.getBytes("UTF-8"));
// Perform the deletion. All records that use this key are // deleted.
myDatabase.delete(null, theKey);
} catch (Exception e) { // Exception handling goes here}
import com.sleepycat.je.DatabaseEntry;import com.sleepycat.je.Database;
import com.sleepycat.je.Cursor;import com.sleepycat.je.OperationStatus;
// Create the data to put into the database
String key1str = "My first string";
String data1str = "My first data";
String key2str = "My second string";
String data2str = "My second data";
String data3str = "My third data";
Cursor cursor = null;
try {
// Database and environment open omitted for brevity
DatabaseEntry key1 = new DatabaseEntry(key1str.getBytes("UTF-8"));
DatabaseEntry data1 = new DatabaseEntry(data1str.getBytes("UTF-8"));
DatabaseEntry key2 = new DatabaseEntry(key2str.getBytes("UTF-8"));
DatabaseEntry data2 = new DatabaseEntry(data2str.getBytes("UTF-8"));
DatabaseEntry data3 = new DatabaseEntry(data3str.getBytes("UTF-8"));
// Open a cursor using a database handle
cursor = myDatabase.openCursor(null, null);
// Assuming an empty database.
OperationStatus retVal = cursor.put(key1, data1); // SUCCESS
retVal = cursor.put(key2, data2); // SUCCESS
retVal = cursor.put(key2, data3); // SUCCESS if dups allowed,
// KEYEXIST if not.
} catch (Exception e) { // Exception handling goes here}
finally { // Make sure to close the cursor cursor.close();}
import com.sleepycat.je.DatabaseEntry;import com.sleepycat.je.Database;import com.sleepycat.je.DatabaseException;import com.sleepycat.je.Cursor;import com.sleepycat.je.OperationStatus; import com.sleepycat.je.LockMode; .....Cursor cursor = null;try { ... // Database and environment open omitted for brevity ... // Open the cursor. cursor = myDatabase.openCursor(null, null); // Cursors need a pair of DatabaseEntry objects to operate. These hold // the key and data found at any given position in the database. DatabaseEntry foundKey = new DatabaseEntry(); DatabaseEntry foundData = new DatabaseEntry(); // To iterate, just call getNext() until the last database record has been // read. All cursor operations return an OperationStatus, so just read // until we no longer see OperationStatus.SUCCESS while (cursor.getNext(foundKey, foundData, LockMode.DEFAULT) == OperationStatus.SUCCESS) { // getData() on the DatabaseEntry objects returns the byte array // held by that object. We use this to get a String value. If the // DatabaseEntry held a byte array representation of some other data // type (such as a complex object) then this operation would look // considerably different. String keyString = new String(foundKey.getData()); String dataString = new String(foundData.getData()); System.out.println("Key - Data : " + keyString + " - " + dataString + ""); }} catch (DatabaseException de) { System.err.println("Error accessing database." + de);} finally { // Cursors must be closed. cursor.close();}
import com.sleepycat.je.DatabaseEntry;import com.sleepycat.je.Database;import com.sleepycat.je.DatabaseException;import com.sleepycat.je.Cursor;import com.sleepycat.je.OperationStatus; import com.sleepycat.je.LockMode;.....// For this example, hard code the search key and dataString searchKey = "Al";String searchData = "Fa";Cursor cursor = null;try { ... // Database and environment open omitted for brevity ... // Open the cursor. cursor = myDatabase.openCursor(null, null); DatabaseEntry theKey = new DatabaseEntry(searchKey.getBytes("UTF-8")); DatabaseEntry theData = new DatabaseEntry(searchData.getBytes("UTF-8")); // Open a cursor using a database handle cursor = myDatabase.openCursor(null, null); // Perform the search OperationStatus retVal = cursor.getSearchBothRange(theKey, theData, LockMode.DEFAULT); // NOTFOUND is returned if a record cannot be found whose key begins // with the search key AND whose data begins with the search data. if (retVal == OperationStatus.NOTFOUND) { System.out.println(searchKey + "/" + searchData + " not matched in database " + myDatabase.getDatabaseName()); } else { // Upon completing a search, the key and data DatabaseEntry // parameters for getSearchBothRange() are populated with the // key/data values of the found record. String foundKey = new String(theKey.getData()); String foundData = new String(theData.getData()); System.out.println("Found record " + foundKey + "/" + foundData + "for search key/data: " + searchKey + "/" + searchData); }} catch (Exception e) { // Exception handling goes here} finally { // Make sure to close the cursor cursor.close();}
import com.sleepycat.je.DatabaseEntry;import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseEntry;import com.sleepycat.je.Cursor;
import com.sleepycat.je.OperationStatus;
import com.sleepycat.je.LockMode;
Cursor cursor = null;
try {
// Database and environment open omitted for brevity
// Create DatabaseEntry objects
// searchKey is some String.
DatabaseEntry theKey = new DatabaseEntry(searchKey.getBytes("UTF-8"));
DatabaseEntry theData = new DatabaseEntry();
// Open a cursor using a database handle
cursor = myDatabase.openCursor(null, null);
// Position the cursor. Ignoring the return value for clarity
OperationStatus retVal = cursor.getSearchKey(theKey, theData, LockMode.DEFAULT);
// Replacement data
String replaceStr = "My replacement string";
DatabaseEntry replacementData = new DatabaseEntry(replaceStr.getBytes("UTF-8"));
cursor.putCurrent(replacementData);} catch (Exception e) {
// Exception handling goes here} finally { // Make sure to close the cursor cursor.close();}
这里的操作是用来代替掉指定的关键字的值,但是若有重复记录的话,就无法实现了。
事务的处理 Berkeley DB Java Edition提供了全部的事务的ACID的所有属性。(Atomicity,Consistency,Isolation,Durability)。 在Berkeley DB中使用事务时,可以很方便的配置和使用: