一、Berkeley DB Java Edition简介
二、Berkeley DB Java Edition的使用及举例说明
根据Berkeley DB的文档介绍,在这里简单的介绍一下用Berkeley DB开发的步骤:
import com.sleepycat.je.Environment; import com.sleepycat.je.DatabaseException; import com.sleepycat.je.EnvironmentConfig; import java.io.File; ...... Environment myDbEnvironment = null; try { EnvironmentConfig envConfig = new EnvironmentConfig(); envConfig.setAllowCreate(true); /**这里的"/export/dbEnv"就是指定的数据库开发环境的目录*/ myDbEnvironment = new Environment(new File("/export/dbEnv"), envConfig); } catch (DatabaseException dbe) { // Exception handling goes here } |
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;......String aKey = "key";String aData = "data";try { DatabaseEntry theKey = new DatabaseEntry(aKey.getBytes("UTF-8")); DatabaseEntry theData = new DatabaseEntry(aData.getBytes("UTF-8"));} catch (Exception e) { // Exception handling goes here} |
import com.sleepycat.je.DatabaseEntry; ...... byte[] myKey = theKey.getData(); byte[] myData = theData.getData(); String key = new String(myKey); String data = new String(myData); ...... 这样就可以按照Java中的String类,来取出数据了。 |
import com.sleepycat.je.DatabaseEntry; import com.sleepycat.je.Database; ..... String aKey = "myFirstKey"; String aData = "myFirstData"; try { DatabaseEntry theKey = new DatabaseEntry(aKey.getBytes("UTF-8")); DatabaseEntry theData = new DatabaseEntry(aData.getBytes("UTF-8")); myDatabase.put(null, theKey, theData); } catch (Exception e) { // 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.Environment;import com.sleepycat.je.Database;import com.sleepycat.je.DatabaseException;import com.sleepycat.je.Environment;import com.sleepycat.je.CursorConfig;import com.sleepycat.je.Cursor;import java.io.File;......Environment myDbEnvironment = null;Database myDatabase = null;Cursor myCursor = null;try { myDbEnvironment = new Environment(new File("/export/dbEnv"), null); myDatabase = myDbEnvironment.openDatabase(null, "myDB", null); myCursor = myDatabase.openCursor(null, null);} catch (DatabaseException dbe) { // 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();} |