bdb數據庫java操作手冊

     package demos.bdb;

import java.io.File;
import com.sleepycat.je.Cursor;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.LockMode;
import com.sleepycat.je.OperationStatus;
import com.sleepycat.je.Transaction;
import com.sleepycat.je.TransactionConfig;

/*
* 數據庫操作類
*/
public class BDB {

private static Database bdb; // 數據源
private static Environment exampleEnv;// 環境對象
private static boolean isrunning = false;// 判斷是否運行

/**
* 打開數據庫方法
*/
public static void start(String path) {
if (isrunning) {
return;
}
/******************** 文件處理 ***********************/
File envDir = new File(path);// 操作文件
if (!envDir.exists())// 判斷文件路徑是否存在,不存在則創建
{
envDir.mkdir();// 創建
}

/******************** 環境配置 ***********************/
EnvironmentConfig envConfig = new EnvironmentConfig();
envConfig.setTransactional(false); // 不進行事務處理
envConfig.setAllowCreate(true); // 如果不存在則創建一個
exampleEnv = new Environment(envDir, envConfig);// 通過路徑,設置屬性進行創建

/******************* 創建適配器對象 ******************/
DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setTransactional(false); // 不進行事務處理
dbConfig.setAllowCreate(true);// 如果不存在則創建一個
dbConfig.setSortedDuplicates(true);// 數據分類

bdb = exampleEnv.openDatabase(null, "simpleDb", dbConfig); // 使用適配器打開數據庫
isrunning = true; // 設定是否運行
}

/**
* 關閉數據庫方法
*/
public static void stop() {
if (isrunning) {
isrunning = false;
bdb.close();
exampleEnv.close();
}
}

public static boolean isrunning() {
return isrunning;
}

/**
* 數據存儲方法 set(Here describes this method function with a few words)
*
* TODO(Here describes this method to be suitable the condition - to be
* possible to elect)
*
* @param key
* @param data
*
* void
*/
public static void set(byte[] key, byte[] data) {
DatabaseEntry keyEntry = new DatabaseEntry();
DatabaseEntry dataEntry = new DatabaseEntry();
keyEntry.setData(key); // 存儲數據
dataEntry.setData(data);

OperationStatus status = bdb.put(null, keyEntry, dataEntry);// 持久化數據

if (status != OperationStatus.SUCCESS) {
throw new RuntimeException("Data insertion got status " + status);
}
}

/*
* 執行獲取,根據key值獲取
*/
public static void selectByKey(String aKey) {
DatabaseEntry theKey =null;
DatabaseEntry theData = new DatabaseEntry();
try {
theKey = new DatabaseEntry(aKey.getBytes("utf-8"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


if (bdb.get(null,theKey, theData,
LockMode.DEFAULT) == OperationStatus.SUCCESS) { //根據key值,進行數據查詢
// Recreate the data String.
byte[] retData = theData.getData();
String foundData = new String(retData);
System.out.println("For key: '" + aKey + "' found data: '"
+ foundData + "'.");
}

}


/**
* 查詢所有,可遍歷數據
* selectAll(Here describes this method function with a few words)
*
* TODO(Here describes this method to be suitable the condition - to be possible to elect)
*
*
* void
*/
public static void selectAll() {
Cursor cursor = null;
cursor=bdb.openCursor(null, null);
DatabaseEntry theKey=null;
DatabaseEntry theData=null;
theKey = new DatabaseEntry();
theData = new DatabaseEntry();

while (cursor.getNext(theKey, theData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
System.out.println(new String(theData.getData()));
}
cursor.close();

}


/**
* 刪除方法
* delete(Here describes this method function with a few words)
*
* TODO(Here describes this method to be suitable the condition - to be possible to elect)
*
* @param key
*
* void
*/
public static void delete(String key) {
DatabaseEntry keyEntry =null;
try {
keyEntry = new DatabaseEntry(key.getBytes("utf-8"));
} catch (Exception e) {
e.printStackTrace();
}
bdb.delete(null, keyEntry);
}


}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章