使用Berkeley DB来做java开发入门

以下例希望有点帮助,也是自己刚刚研究出来的

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import com.sleepycat.db.Cursor;
import com.sleepycat.db.Database;
import com.sleepycat.db.DatabaseConfig;
import com.sleepycat.db.DatabaseEntry;
import com.sleepycat.db.DatabaseException;
import com.sleepycat.db.DatabaseType;
import com.sleepycat.db.Environment;
import com.sleepycat.db.EnvironmentConfig;
import com.sleepycat.db.OperationStatus;

public class TestBDB {


/**
* 直接用Database进行操做 得到一个游标进行操作 用完后一定要关闭
* @throws DatabaseException
* @throws FileNotFoundException
*/
public Cursor testBDB11() throws FileNotFoundException, DatabaseException{
DatabaseConfig databaseConfig = new DatabaseConfig();//一个BDB数据的配置
databaseConfig.setAllowCreate(true);//是否自动创建
databaseConfig.setTransactional(false);//是使用事物
databaseConfig.setErrorStream(System.err);//出错后输出流
databaseConfig.setErrorPrefix("BDB"); //出错后文件别名
databaseConfig.setType(DatabaseType.BTREE);//BDB存的方式
databaseConfig.setSortedDuplicates(false); //配置数据库,以支持排序,重复数据项。
Database database = new Database("D:/test.db",null,databaseConfig); //参数为:文件 数据名 配置
Cursor cursor = database.openCursor(null, null); //打开数据库的游标
return cursor;
}
/**使用用环境 和JE中使用差不多
* @return
* @throws FileNotFoundException
* @throws DatabaseException
*/
public Cursor testBDB22() throws FileNotFoundException, DatabaseException{

EnvironmentConfig envConfig = new EnvironmentConfig();//一个BDB环境的配置
envConfig.setTransactional(false);//是否使用事物
envConfig.setAllowCreate(true);//是否自动创建
envConfig.setInitializeCache(true);//缓存
envConfig.setInitializeLocking(true);
envConfig.setCacheSize(1000000);//缓存大小

File envHome = new File("E:/DB/Environment"); //环境的目录
Environment env = new Environment(envHome, envConfig);



DatabaseConfig databaseConfig = new DatabaseConfig();//一个BDB数据的配置
databaseConfig.setAllowCreate(true);//是否自动创建
databaseConfig.setTransactional(false);//是否使用事物
databaseConfig.setErrorStream(System.err);//出错后输出流
databaseConfig.setErrorPrefix("BDB"); //出错后文件别名
databaseConfig.setType(DatabaseType.BTREE);//BDB存的方式
databaseConfig.setSortedDuplicates(false); //配置数据库,以支持排序,重复数据项。
Database db = env.openDatabase(null, "test.db", null, databaseConfig);//参数为:事物 文件名 数据名 配置
Cursor cursor = db.openCursor(null, null); //打开数据库的游标
return cursor;
}

/**
* @param cursor 用游标操作
* @throws DatabaseException
*/
public boolean addData(Cursor cursor) throws DatabaseException{
//因为BDB中存得都是byte[]所以把Key和vaule转为byte[],相当一个MAP使用
DatabaseEntry key = new DatabaseEntry(objectToByte("key"));
DatabaseEntry data = new DatabaseEntry(objectToByte("value"));
OperationStatus os = cursor.put(key, data);
return OperationStatus.SUCCESS.equals(os);
}
public boolean delData(Cursor cursor) throws DatabaseException{
//因为BDB中存得都是byte[]所以把Key和vaule转为byte[],相当一个MAP使用
DatabaseEntry key = new DatabaseEntry(objectToByte("key"));
DatabaseEntry data = new DatabaseEntry(objectToByte("value"));
OperationStatus os = cursor.put(key, data);
if(OperationStatus.SUCCESS.equals(os)){
os = cursor.delete();
}
return OperationStatus.SUCCESS.equals(os);
}

/**把一个对象转为byte[]
* @param obj
* @return
*/
public static byte[] objectToByte(Object obj) {
byte[] bytes = null;
// object to bytearray
ByteArrayOutputStream bo = null;
ObjectOutputStream oo = null;
try {
if(obj!=null){
bo = new ByteArrayOutputStream();
oo= new ObjectOutputStream(bo);
oo.writeObject(obj);
bytes = bo.toByteArray();

}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(oo!=null){
oo.close();
}
if(bo!=null){
bo.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

return bytes;
}


/**把byte[]转为一个对象
* @param bytes
* @return
* @throws ClassNotFoundException
*/
public static Object byteToObject(byte[] bytes) throws
ClassNotFoundException {
// bytearray to object
Object obj = null;
ByteArrayInputStream bi = null;
ObjectInputStream oi = null;
try {
if (bytes != null) {
bi = new ByteArrayInputStream(bytes, 0,
bytes.length);
oi = new ObjectInputStream(bi);
obj = oi.readObject();

}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(bi!=null){
bi.close();
}
if(oi!=null){
oi.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
return obj;
}

}

得到一个游标你就可以进行操作,其它的就不写了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值