package demos.bdb;
import java.io.File;
import com.sleepycat.je.Cursor;
import com.sleepy cat.je.database;
import com.sleepy cat.je.database config;
import com.sleepy cat.je.database entry;
import com.sleepy cat.je.environment;
import com.sleepy cat.je.environment config;
import com.sleepy cat.je.lock mode;
import com.sleepy cat.je.operation status;
import com.sleepy cat.je.transaction;
import com.sleepy cat.je.transaction config;
/*
*数据库操作系统
*/
公共类BDB {
私有静态数据库BDB; //数据源
隐私保护功能; //环境对象
私密性统计信息=假; //判断是否驾驶
//*
*如何打开数据库
*/
publicstaticvoidstart (字符串路径) {
if (is运行) {
返回;
}
/**************文件处理**************************/
fileenvdir=newfile(path; //操作文件
if (! 确定envDir.exists ()//文件路径是否存在,如果不存在,则创建
{
envDir.mkdir (; //创建
}
/**************环境配置***************************/
environmentconfigenvconfig=newenvironmentconfig (;
env config.set transactional (false ); //不处理事务
ENVconfig.setallowcreate(true; //如果不存在则创建
example env=new environment (env dir,envConfig ); 在//路径中,设置属性并创建
/************创建适配器对象************************/
databaseconfigdbconfig=newdatabaseconfig (;
bconfig.settransactional(false ); //不处理事务
dbconfig.setallowcreate(true; //如果不存在则创建
db config.setsortedduplicates (true; //数据分类
BDB=example env.open database (空,' simpleDb ',dbConfig ); //使用适配器打开数据库
is运行=true; //设定是否运转
}
//*
*如何关闭数据库
*/
公共静态语音停止
if (is运行) {
isrunning=false;
bdb.close (;
exampleEnv.close (;
}
}
公共静态布尔is running (
返回is运行;
}
//*
*数据存储方法set (heredescribesthismethodfunctionwithafewwords ) () ) ) ) ) ) ) ) ) )。
*
* heredescribesthismethodtobesuitablethecondition-tobe (待办事项)
* possible to elect )
*
* @param key
* @param data
*
* void
*/
publicstaticvoidset(byte[]key,byte[] data ) {
databaseentrykeyentry=newdatabaseentry (;
databaseentrydataentry=newdatabaseentry (;
keyentry.setdata(key; //保存数据
数据入口. setdata (数据;
操作statusstatus=BDB.put (null,keyEntry,dataEntry ); //持久化数据
if (状态!=OperationStatus.SUCCESS ) {
thrownewruntimeexception (数据传输状态) status );
}
}
/*
*执行获取,从key值获取
*/
publicstaticvoidselectbykey (字符串密钥) {
数据库输入the key=null;
databaseentrythedata=newdatabaseentry (;
try {
the key=newdatabaseentry (akey.getbytes (' utf-8 ' );
}catch(exceptione ) {
//todo auto-generated catch块
e .打印堆栈跟踪(;
}
if(bdb.get ) null、theKey、theData,
根据lock mode.default (==operation status.success ) )密钥值进行数据查询
//recreate the数据字符串。
byte[] retData=theData.getData (;
stringfounddata=new string (ret data;
system.out.println (' forkey : ' ' akey ' ' found data : ' )。
foundData '.';
}
}
//*
可以查询所有内容并遍历数据
*选择全部(heredescribesthismethodfunctionwithafewwords ) )。
*
* todo (heredescribesthismethodtobesuitablethecondition-tobepossibletoelect ) )。
*
*
* void
*/
公共静态语音选择全部
Cursor cursor=null;
cursor=bdb.opencursor(null,null );
数据库输入the key=null;
数据库输入the data=null;
the key=new数据库条目(;
the data=new数据库条目(;
while(cursor.getnext(thekey,theData,LockMode.DEFAULT )==操作状态. success ) {
system.out.println(newstring ) thedata.getdata ();
}
cursor.close (;
}
//*
*删除方法
* delete (heredescribesthismethodfunctionwithafewwords ) )。
*
* todo (heredescribesthismethodtobesuitablethecondition-tobepossibletoelect ) )。
*
* @param key
*
* void
*/
publicstaticvoiddelete (字符串密钥) {
数据库条目密钥=null;
try {
keyentry=newdatabaseentry (key.getbytes (' utf-8 ' );
}catch(exceptione ) {
e .打印堆栈跟踪(;
}
BDB.delete (空,密钥);
}
}