001 |
package demos.bdb; |
002 |
003 |
import java.io.File; |
004 |
import com.sleepycat.je.Cursor; |
005 |
import com.sleepycat.je.Database; |
006 |
import com.sleepycat.je.DatabaseConfig; |
007 |
import com.sleepycat.je.DatabaseEntry; |
008 |
import com.sleepycat.je.Environment; |
009 |
import com.sleepycat.je.EnvironmentConfig; |
010 |
import com.sleepycat.je.LockMode; |
011 |
import com.sleepycat.je.OperationStatus; |
012 |
import com.sleepycat.je.Transaction; |
013 |
import com.sleepycat.je.TransactionConfig; |
014 |
015 |
/* |
016 |
*
数据库操作类 |
017 |
*/ |
018 |
public
class BDB { |
019 |
020 |
private
static Database bdb; // 数据源 |
021 |
private
static Environment exampleEnv;// 环境对象 |
022 |
private
static boolean isrunning = false;// 判断是否运行 |
023 |
024 |
/** |
025 |
*
打开数据库方法 |
026 |
*/ |
027 |
public
static void start(String path) { |
028 |
if
(isrunning) { |
029 |
return; |
030 |
} |
031 |
/********************
文件处理 ***********************/ |
032 |
File
envDir = new File(path);// 操作文件 |
033 |
if
(!envDir.exists())// 判断文件路径是否存在,不存在则创建 |
034 |
{ |
035 |
envDir.mkdir();//
创建 |
036 |
} |
037 |
038 |
/********************
环境配置 ***********************/ |
039 |
EnvironmentConfig
envConfig = new EnvironmentConfig(); |
040 |
envConfig.setTransactional(false);
// 不进行事务处理 |
041 |
envConfig.setAllowCreate(true);
// 如果不存在则创建一个 |
042 |
exampleEnv
= new Environment(envDir, envConfig);// 通过路径,设置属性进行创建 |
043 |
044 |
/*******************
创建适配器对象 ******************/ |
045 |
DatabaseConfig
dbConfig = new DatabaseConfig(); |
046 |
dbConfig.setTransactional(false);
// 不进行事务处理 |
047 |
dbConfig.setAllowCreate(true);//
如果不存在则创建一个 |
048 |
dbConfig.setSortedDuplicates(true);//
数据分类 |
049 |
050 |
bdb
= exampleEnv.openDatabase(null, "simpleDb", dbConfig); // 使用适配器打开数据库 |
051 |
isrunning
= true; // 设定是否运行 |
052 |
} |
053 |
054 |
/** |
055 |
*
关闭数据库方法 |
056 |
*/ |
057 |
public
static void stop() { |
058 |
if
(isrunning) { |
059 |
isrunning
= false; |
060 |
bdb.close(); |
061 |
exampleEnv.close(); |
062 |
} |
063 |
} |
064 |
065 |
public
static boolean isrunning() { |
066 |
return
isrunning; |
067 |
} |
068 |
069 |
/** |
070 |
*
数据存储方法 set(Here describes this method function with a few words) |
071 |
* |
072 |
*
TODO(Here describes this method to be suitable the condition - to be |
073 |
*
possible to elect) |
074 |
* |
075 |
*
@param key |
076 |
*
@param data |
077 |
* |
078 |
*
void |
079 |
*/ |
080 |
public
static void set(byte[] key, byte[] data) { |
081 |
DatabaseEntry
keyEntry = new DatabaseEntry(); |
082 |
DatabaseEntry
dataEntry = new DatabaseEntry(); |
083 |
keyEntry.setData(key);
// 存储数据 |
084 |
dataEntry.setData(data); |
085 |
086 |
OperationStatus
status = bdb.put(null, keyEntry, dataEntry);// 持久化数据 |
087 |
088 |
if
(status != OperationStatus.SUCCESS) { |
089 |
throw
new RuntimeException("Data insertion got status " + status); |
090 |
} |
091 |
} |
092 |
093 |
/* |
094 |
*
执行获取,根据key值获取 |
095 |
*/ |
096 |
public
static void selectByKey(String aKey) { |
097 |
DatabaseEntry
theKey =null; |
098 |
DatabaseEntry
theData = new DatabaseEntry(); |
099 |
try
{ |
100 |
theKey
= new DatabaseEntry(aKey.getBytes("utf-8")); |
101 |
}
catch (Exception e) { |
102 |
//
TODO Auto-generated catch block |
103 |
e.printStackTrace(); |
104 |
} |
105 |
|
106 |
|
107 |
if
(bdb.get(null,theKey, theData, |
108 |
LockMode.DEFAULT)
== OperationStatus.SUCCESS) { //根据key值,进行数据查询 |
109 |
//
Recreate the data String. |
110 |
byte[]
retData = theData.getData(); |
111 |
String
foundData = new String(retData); |
112 |
System.out.println("For
key: '" + aKey + "' found data: '" |
113 |
+
foundData + "'."); |
114 |
} |
115 |
|
116 |
} |
117 |
|
118 |
|
119 |
/** |
120 |
*
查询所有,可遍历数据 |
121 |
*
selectAll(Here describes this method function with a few words) |
122 |
* |
123 |
*
TODO(Here describes this method to be suitable the condition - to be possible to elect) |
124 |
* |
125 |
* |
126 |
*
void |
127 |
*/ |
128 |
public
static void selectAll() { |
129 |
Cursor
cursor = null; |
130 |
cursor=bdb.openCursor(null,
null); |
131 |
DatabaseEntry
theKey=null; |
132 |
DatabaseEntry
theData=null; |
133 |
theKey
= new DatabaseEntry(); |
134 |
theData
= new DatabaseEntry(); |
135 |
|
136 |
while
(cursor.getNext(theKey, theData, LockMode.DEFAULT) == OperationStatus.SUCCESS) { |
137 |
System.out.println(new
String(theData.getData())); |
138 |
} |
139 |
cursor.close(); |
140 |
|
141 |
} |
142 |
|
143 |
|
144 |
/** |
145 |
*
删除方法 |
146 |
*
delete(Here describes this method function with a few words) |
147 |
* |
148 |
*
TODO(Here describes this method to be suitable the condition - to be possible to elect) |
149 |
* |
150 |
*
@param key |
151 |
* |
152 |
*
void |
153 |
*/ |
154 |
public static void delete(String
key) { |
155 |
DatabaseEntry
keyEntry = null ; |
156 |
try { |
157 |
keyEntry
= new DatabaseEntry(key.getBytes( "utf-8" )); |
158 |
} catch (Exception
e) { |
159 |
e.printStackTrace(); |
160 |
} |
161 |
bdb.delete( null ,
keyEntry); |
162 |
} |
163 |
|
164 |
|
165 |
} |