Berkeley DB实例
2011年09月01日
package test;
import com.sleepycat.je.*;
import com.sleepycat.bind.EntryBinding;
import com.sleepycat.bind.tuple.TupleBinding;
import com.sleepycat.bind.serial.StoredClassCatalog;
import com.sleepycat.bind.serial.SerialBinding;
import java.io.File;
public class test {
public static void main(String[] args) {
}
/**
* 打开和关闭环境,示例一
*
*/
public void eg1(){
//----打开环境,如果不存在,则创建一个------------
Environment myDbEnvironment=null;
try {
EnvironmentConfig envConfig = new EnvironmentConfig();
envConfig.setAllowCreate(true); //true不存在就创建,false如果不存在则打开环境失败
//envConfig.setReadOnly(true); //true 以只读方式打开,如果是多进程应用,每个进程都要设置为true
//envConfig.setTransactional(true);//true支持事务,false不支持,默认false。可以更改配置文件来设置此参数。
myDbEnvironment = new Environment(new File(".//"), envConfig);//环境所在路径
java.util.List myDbNames = myDbEnvironment.getDatabaseNames(); //得到所有的数据库的名字
for(int i=0; i value,二者都被是有DatabaseEntry封装的。
//这个之前也提过很多次了,DatabaseEntry可以封装原始类型和复杂的对象类型,二者都要被转换为byte array存储,转换可以使用Bind API来完成
//写数据
myDatabase.put(null, theKey, theData);//如果不是可重复数据库,put将会覆盖原有的记录。
//myDatabase.putNoOverwrite(null, theKey, theData);//不允许覆盖,不管是否允许数据重复。
//读数据
//--myDatabase.getSearchBoth(null, theKey, theData, LockMode.DEFAULT);//查找key和data都匹配的记录
//--查询出来的key和data都是byte数组形式。
if (myDatabase.get(null, theKey, theData, LockMode.DEFAULT) ==OperationStatus.SUCCESS)
{
byte[] retData = theData.getData();
String foundData = new String(retData, "UTF-8");
System.out.println("For key: '" + aKey + "' found data: '" +foundData + "'.");
}
//删除数据
myDatabase.delete(null, theKey); //删除数据
} catch (Exception e) {}
//关闭数据库
//如果打开了游标,关闭时JE会发出警告,让你关闭他们先。活动状态的游标在关闭库的过程中会产生意想不到的结果,尤其是其他线程在写库的过程中。确定所有的访问都结束后再关闭库
try {
if (myDatabase != null) {
myDatabase.close();
myDbEnvironment.renameDatabase(null, "sampleDatabase", "test");//重命名,必须先关闭数据库
myDbEnvironment.removeDatabase(null, "sampleDatabase");//删除数据库,必须先关闭数据库
//myDbEnvironment.truncateDatabase(null, myDatabase.getDatabaseName(),true);//删除并回收数据库空间 ,true返回删除的记录的数量,false不返回删除的记录数量值
}
if (myDbEnvironment != null) {
myDbEnvironment.close();
}
} catch (DatabaseException dbe) {
2011年09月01日
package test;
import com.sleepycat.je.*;
import com.sleepycat.bind.EntryBinding;
import com.sleepycat.bind.tuple.TupleBinding;
import com.sleepycat.bind.serial.StoredClassCatalog;
import com.sleepycat.bind.serial.SerialBinding;
import java.io.File;
public class test {
public static void main(String[] args) {
}
/**
* 打开和关闭环境,示例一
*
*/
public void eg1(){
//----打开环境,如果不存在,则创建一个------------
Environment myDbEnvironment=null;
try {
EnvironmentConfig envConfig = new EnvironmentConfig();
envConfig.setAllowCreate(true); //true不存在就创建,false如果不存在则打开环境失败
//envConfig.setReadOnly(true); //true 以只读方式打开,如果是多进程应用,每个进程都要设置为true
//envConfig.setTransactional(true);//true支持事务,false不支持,默认false。可以更改配置文件来设置此参数。
myDbEnvironment = new Environment(new File(".//"), envConfig);//环境所在路径
java.util.List myDbNames = myDbEnvironment.getDatabaseNames(); //得到所有的数据库的名字
for(int i=0; i value,二者都被是有DatabaseEntry封装的。
//这个之前也提过很多次了,DatabaseEntry可以封装原始类型和复杂的对象类型,二者都要被转换为byte array存储,转换可以使用Bind API来完成
//写数据
myDatabase.put(null, theKey, theData);//如果不是可重复数据库,put将会覆盖原有的记录。
//myDatabase.putNoOverwrite(null, theKey, theData);//不允许覆盖,不管是否允许数据重复。
//读数据
//--myDatabase.getSearchBoth(null, theKey, theData, LockMode.DEFAULT);//查找key和data都匹配的记录
//--查询出来的key和data都是byte数组形式。
if (myDatabase.get(null, theKey, theData, LockMode.DEFAULT) ==OperationStatus.SUCCESS)
{
byte[] retData = theData.getData();
String foundData = new String(retData, "UTF-8");
System.out.println("For key: '" + aKey + "' found data: '" +foundData + "'.");
}
//删除数据
myDatabase.delete(null, theKey); //删除数据
} catch (Exception e) {}
//关闭数据库
//如果打开了游标,关闭时JE会发出警告,让你关闭他们先。活动状态的游标在关闭库的过程中会产生意想不到的结果,尤其是其他线程在写库的过程中。确定所有的访问都结束后再关闭库
try {
if (myDatabase != null) {
myDatabase.close();
myDbEnvironment.renameDatabase(null, "sampleDatabase", "test");//重命名,必须先关闭数据库
myDbEnvironment.removeDatabase(null, "sampleDatabase");//删除数据库,必须先关闭数据库
//myDbEnvironment.truncateDatabase(null, myDatabase.getDatabaseName(),true);//删除并回收数据库空间 ,true返回删除的记录的数量,false不返回删除的记录数量值
}
if (myDbEnvironment != null) {
myDbEnvironment.close();
}
} catch (DatabaseException dbe) {