BerkeleyDB

package org.qichao.database;

import java.io.File;
import java.io.UnsupportedEncodingException;

import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.LockMode;
import com.sleepycat.je.OperationStatus;

public class BerkeleyDB {

	private static final String DB_ENVIRONMENT_ROOT = "C:/dbRoot";
	private Environment myDBEnvironment = null;
	private static Database myDB = null;
	/*
	 *打开database环境
	 *@return
	 */
	public void createDBEnvironment()
	{
	 	if(myDBEnvironment != null)
	 	{
	 		return;
	 	}
	 	try
	 	{
	 		EnvironmentConfig config = new EnvironmentConfig();
	 		config.setAllowCreate(true);//如果不存在则创建一个
	 		config.setReadOnly(false);//以只读方式打开,默认为false
	 		config.setTransactional(true);//事务支持,如果为true,则表示当前环境支持事务处理,默认为false,不支持事务处理
	 		myDBEnvironment = new Environment(new File(DB_ENVIRONMENT_ROOT),config);
	 	}catch(DatabaseException e)
	 	{
	 		e.printStackTrace();
	 	}
	}
	/*
	 *关闭database环境
	 */
	public void closeDBEnvironment()
	{
		try
	    {
			if(myDBEnvironment != null)
			{
				myDBEnvironment.cleanLog();//在关闭环境前清理下日志
	   		    myDBEnvironment.close();
	   		}
	   	}catch(DatabaseException e)
	   	{
	   		e.printStackTrace();
	   	}
	}
	/*
	 *创建一个数据库
	 */
	public void createDB()
	{
		//打开一个数据库,数据库名为sampleDatabase,数据库的配置为config
	    if(myDB != null)
	    {
	    	return;
	    }
	    DatabaseConfig dbConfig = new DatabaseConfig();
	    dbConfig.setAllowCreate(true);
	    try
	    {
	    	myDB = myDBEnvironment.openDatabase(null,"sampleDatabase",dbConfig);
	    }catch(DatabaseException e)
	    {
	    	e.printStackTrace();
	    }
	}
	/*
	 *关闭数据库
	 */
	public void closeDB()
	{
		try
	    {
			if(myDB != null)
		    {
		     		myDB.close();
		    }
		}catch(DatabaseException e)
		{
		    	e.printStackTrace();
		}
	}
	//查询某个字段
	public static boolean selectKeyWord(String keyword){
		try{
			DatabaseEntry theKey = new DatabaseEntry(keyword.getBytes("UTF-8"));
			DatabaseEntry theData = new DatabaseEntry();
	    	if(myDB == null)
	    	{
	    		return true;
	    	}
	    	if(myDB.get(null, theKey, theData, LockMode.DEFAULT)==OperationStatus.SUCCESS){
	    		byte[] retData = theData.getData();
	    		String foundData = new String(retData,"UTF-8");
	    		System.out.println("found data:"+foundData+".");
	    		return true;
	    	}else{
	    		return false;
	    	}
		}catch(Exception e){
			e.printStackTrace();
			return true;
		}
	}
	public static void main(String args[])
	{
		BerkeleyDB bService = new BerkeleyDB();
     	bService.createDBEnvironment();
     	bService.createDB();
     	//记录
     	String aKey[] = {"001","002","003","004","005","006","007","008","009","010","011","011","012","013","014","015","016","017","018","019"};
     	String aData[] = {"我是正文内容","我是正文内容","我是正文内容","我是正文内容","我是正文内容","我是正文内容","我是正文内容","我是正文内容","我是正文内容","我是正文内容"
     			,"我是正文内容","我是正文内容","我是正文内容","我是正文内容","我是正文内容","我是正文内容","我是正文内容","我是正文内容","我是正文内容","我是正文内容"};
     	//最好指定编码方式,因为不指定编码会用系统的默认编码来转换,
     	//因为系统的默认编码可能会被人更改
     	boolean bool = selectKeyWord("001");
     	System.out.println(bool);
//     	try
//     	{   	
//     		
//     		for(int i=0;i<aKey.length;i++){
//	     		DatabaseEntry thekey = new DatabaseEntry(aKey[i].getBytes("UTF-8"));
//	     		DatabaseEntry theData = new DatabaseEntry(aData[i].getBytes("UTF-8"));
//	     		myDB.put(null, thekey, theData);
//     		}
//     	}catch(DatabaseException e)
//     	{
//     		e.printStackTrace();
//     	}catch(UnsupportedEncodingException e)
//     	{
//     		e.printStackTrace();
//     	}
     	bService.closeDB();
     	bService.closeDBEnvironment();

	}
}
 

你可能感兴趣的:(BerkeleyDB)