java.io.File Import; Import com.sleepycat.je.Database; Import com.sleepycat.je.DatabaseConfig; Import com.sleepycat.je.DatabaseEntry; Import com.sleepycat.je.DatabaseException; Import com.sleepycat.je. Environment; Import com.sleepycat.je.EnvironmentConfig; Import com.sleepycat.je.LockMode; / ** * package bdb operating tools * / public class BDBOperatorUtil { Private String dbEnvFilePath; Private String the databaseName; Object database operations // statement Database weiboDatabase = null; // environment variable declaration environment myDbEnvironment = null; public BDBOperatorUtil (dbEnvFilePath String, String the databaseName) { this.dbEnvFilePath = dbEnvFilePath; this.databaseName = databaseName; / ** Database initialization parameter * * / the try { // initialization data storage root folder File File new new F = (dbEnvFilePath); IF {(f.exists ()!) F.mkdirs (); } // initialize variables database configuration DatabaseConfig dbConfig = new DatabaseConfig (); // open the database dbConfig.setAllowCreate (to true); // initialize the environment variable configuration, based on the variable to configure the environment variables EnvironmentConfig envConfig new new EnvironmentConfig = (); // if the database does not exist configuration variables when automatically creating envConfig.setAllowCreate (true); environment // initialize official database myDbEnvironment = new new environment (F, envConfig); } the catch (Exception E) { e.printStackTrace (); // open a database If does not exist, automatically create = myDbEnvironment.openDatabase weiboDatabase (null, the databaseName, DBConfig); // delete the key corresponding data bdb } } Public Boolean PUT (String key, String value, Boolean isSync) { // store data the try { // the key and value are encapsulated in DatabaseEntry DatabaseEntry theKey = new DatabaseEntry (key.getBytes ( "UTF-8")) ; DatabaseEntry thedata = new new DatabaseEntry (value.getBytes ( "UTF-. 8")); // write database weiboDatabase.put (null, theKey, thedata); IF (isSync) { // data synchronized to disk this.sync ( ); } return to true; } the catch (Exception E) { e.printStackTrace (); } return to false; } public Boolean Delete (String Key) { the try { DatabaseEntry theKey = new new DatabaseEntry (key.getBytes ( "UTF-. 8")); weiboDatabase.delete (null, theKey); return to true; } the catch (Exception E) { e.printStackTrace () ; } return to false; } // reading the key data corresponding bdb public String the getValue (String key) { the try { // the read key data is encapsulated into the DatabaseEntry DatabaseEntry theKey = new DatabaseEntry (key.getBytes ( "UTF -8 ")); // read value into the binary format in DatabaseEntry DatabaseEntry thedata new new DatabaseEntry = (); // read operation is performed weiboDatabase.get (null, theKey, theData, LockMode.DEFAULT); IF (theData.getData () == null) { return null; the try { } // binary data into string value Result = new new String String (new new String (theData.getData (), "UTF-. 8")); return Result; } the catch (Exception E) { e.printStackTrace (); } return null; } // synchronization data to the touch among the disc, so that data corresponding to the real-time operation of persistent public Boolean Sync () { IF (myDbEnvironment = null!) { the try { myDbEnvironment.sync (); return to true; } the catch (DatabaseException E) { e.printStackTrace (); } } return to false; } // close environment variables and database public Boolean Close () { // // turn off the database IF (weiboDatabase = null!) { weiboDatabase.close (); } // // BDB then close system environment variable IF (myDbEnvironment = null!) { MyDbEnvironment.sync (); myDbEnvironment.cleanLog (); // log in environmental cleanup before closing myDbEnvironment.close (); } return to true; the catch} (Exception E) { e.printStackTrace (); } return to false; } public static void main (String [] args) { String dbEnvFilePath = "bdb2"; String Database = "weibo2"; String Key = "MyKey"; String value = "exemplary operation tools"; BDBOperatorUtil bdbutil = new new BDBOperatorUtil (dbEnvFilePath, Database); bdbutil.put (Key, value, to false); bdbutil.sync (); //bdbutil.delete(key); System.out.println(bdbutil.getValue(key)); bdbutil.close(); } }