为了学习的需要配置了BerkeleyDB,以下为测试而找的实例。
package test;
import java.io.File;
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;
import com.sleepycat.je.OperationStatus;
public class test{
private Environment env;
private Database db;
public test() {
}
public void setUp(String path, long cacheSize) {
EnvironmentConfig envConfig = new EnvironmentConfig(); 创建一个EnvironmentConfig配置对象
envConfig.setAllowCreate(true);//允许创建一个数据库环境
envConfig.setCacheSize(cacheSize);//设置数据库缓存的大小
try {
env = new Environment(new File(path),envConfig);//使用一个路径何以个EnviromentConfig配置对象
//创建enviroment环境
} catch (DatabaseException e) {
e.printStackTrace();
}
}
public void open(String dbName) {
DatabaseConfig dbConfig = new DatabaseConfig();//创建一个DatabaseConfig配置对象
dbConfig.setAllowCreate(true);//允许创建数据库
try {
db = env.openDatabase(null, dbName, dbConfig);
} catch (DatabaseException e) {
e.printStackTrace();
}
}
public void close() {
try {
if(db != null) {
db.close();
}
if(env != null) {
env.close();
}
} catch (DatabaseException e) {
e.printStackTrace();
}
}
public String get(String key) throws Exception {//获取数据
DatabaseEntry queryKey = new DatabaseEntry();
DatabaseEntry value = new DatabaseEntry();
queryKey.setData(key.getBytes("UTF-8"));
OperationStatus status = db.get(null, queryKey, value,
LockMode.DEFAULT);
if (status == OperationStatus.SUCCESS) {
return new String(value.getData());
}
return null;
}
public boolean put(String key, String value) throws Exception {//像数据库中写书据
byte[] theKey = key.getBytes("UTF-8");
byte[] theValue = value.getBytes("UTF-8");
OperationStatus status = db.put(null, new DatabaseEntry(theKey),
new DatabaseEntry(theValue));
if(status == OperationStatus.SUCCESS) {
return true;
}
return false;
}
public boolean delete(String key) throws Exception {//删除指定的数据
byte[] theKey = key.getBytes("UTF-8");
OperationStatus status = db.delete(null, new DatabaseEntry(theKey));
if(status == OperationStatus.SUCCESS) {
return true;
}
return false;
}
public boolean update(String key, String value) throws Exception {//修改数据
byte[] updateKey = key.getBytes("UTF-8");
byte[] updateValue = value.getBytes("UTF-8");
OperationStatus status = db.put(null, new DatabaseEntry(updateKey), new DatabaseEntry(updateValue));
if (status == OperationStatus.SUCCESS) {
return true;
}
return false;
}
public static void main(String[] args) {
test mbdb = new test();
mbdb.setUp("/home/lenovo/TempTest", 1000000);
mbdb.open("myDB");
System.out.println("开始向Berkeley DB中存入数据...");
for(int i=0; i<20; i++) {
try {
String key = "myKey"+i;
String value = "myValue"+i;
// System.out.println("[" + key + ":" + value + "]");
mbdb.put(key , value);
} catch (Exception e) {
e.printStackTrace();
}
}
try {
mbdb.delete("myKey10");
mbdb.delete("myKey11");
mbdb.update("myKey12", "Changed");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while(true) {//从数据库中读取数据
try {
for(int i=0; i<1000; i++) {
System.out.println("从Berkeley DB中取出数据...");
String k = "myKey" + i;
String v = mbdb.get(k);
System.out.println("[" + k + ":" + v +"]");
Thread.sleep(1000);
if(i == 20) {
mbdb.close();
System.exit(0);
}
}
Thread.sleep(3000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
代码来自http://blog.sina.com.cn/s/blog_757dbb830100qwl1.html