java连接BerkeleyDB数据库使用实例

Berkeley DB是一个开源的文件数据库,介于关系数据库与内存数据库之间,使用方式与内存数据库类似,它提供的是一系列直接访问数据库的函数,而不是像关系数据库那样需要网络通讯、SQL解析等步骤。
package cn.yws.berkeleydb;

import java.io.File;
import java.io.Reader;
import java.io.Writer;

import com.sleepycat.bind.tuple.IntegerBinding;
import com.sleepycat.bind.tuple.StringBinding;
import com.sleepycat.je.Cursor;
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;
import com.sleepycat.je.Transaction;

/**
 * BerkeleyDB数据库使用实例
 * 需要jdbc jar包je-3.2.23.jar
 * @author 542335496@qq.com
 * 
 */
public class MyBerkeleyDB {
	private Database database = null;
	private Environment dbEnvironment = null;

	public Database getDatabase() {
		return database;
	}

	public void setDatabase(Database database) {
		this.database = database;
	}

	public Environment getDbEnvironment() {
		return dbEnvironment;
	}

	public void setDbEnvironment(Environment dbEnvironment) {
		this.dbEnvironment = dbEnvironment;
	}

	public MyBerkeleyDB() {
		initDB();
	}

	public void close() {
		try {

			if (database != null) {
				database.close();
			}
			if (dbEnvironment != null) {
				dbEnvironment.close();
			}
		} catch (DatabaseException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		// Writer();
		MyBerkeleyDB berkeleyDB = new MyBerkeleyDB();
		
		berkeleyDB.insertOrReader(true, 10, 10);
		berkeleyDB.insertOrReader(false, 10, 10);
		berkeleyDB.WriterData();
		berkeleyDB.reader();
		berkeleyDB.close();
		berkeleyDB = null;
	}

	public void insertOrReader(boolean doInsert, int offset, int numRecords) {
		try {
			DatabaseEntry keyEntry = new DatabaseEntry();
			DatabaseEntry dataEntry = new DatabaseEntry();
			if (doInsert) {
				for (int i = offset; i < numRecords + offset; i++) {
					Transaction txn = dbEnvironment.beginTransaction(null, null);
					IntegerBinding.intToEntry(i, keyEntry);
					IntegerBinding.intToEntry(i + 1, dataEntry);
					OperationStatus status = database.put(txn, keyEntry, dataEntry);
					if (status != OperationStatus.SUCCESS) {
						throw new DatabaseException("Data insertion got status "
								+ status);
					}
					txn.commitNoSync();
				}
			} else {
				/* retrieve the data */
				Transaction txn = dbEnvironment.beginTransaction(null, null);
				Cursor cursor = database.openCursor(txn, null);

				while (cursor.getNext(keyEntry, dataEntry, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
					System.out.println("key=" + IntegerBinding.entryToInt(keyEntry)
							+ " data=" + IntegerBinding.entryToInt(dataEntry));
					//删除数据
					cursor.delete();

				}
				cursor.close();
				txn.commitNoSync();
			}
		} catch (DatabaseException e) {
			e.printStackTrace();
		}
	}

	private void WriterData() {
		try {
			// 数据库实体:键值对
			DatabaseEntry keyEntry = new DatabaseEntry();
			DatabaseEntry valueEntry = new DatabaseEntry();
			// 开启事务
			Transaction transaction = dbEnvironment
					.beginTransaction(null, null);
			// 存入键值对
			StringBinding.stringToEntry("key003", keyEntry);
			StringBinding.stringToEntry("content 测试的  ", valueEntry);
			// 录入数据库,返回操作结果
			OperationStatus status = database.put(transaction, keyEntry,
					valueEntry);
			if (status != OperationStatus.SUCCESS) {
				throw new DatabaseException("data insert failed,status:"
						+ status);
			}
			// 同步
			transaction.commitNoSync();
		} catch (DatabaseException e) {
			e.printStackTrace();
		}

	}

	private void initDB() {
		try {
			// Berkeley数据库环境配置
			EnvironmentConfig envConfig = new EnvironmentConfig();
			// 配置事务
			envConfig.setTransactional(true);
			// 不存在就创建环境
			envConfig.setAllowCreate(true);
			// 设置数据库保存路径
			File dbPath = new File("c:/fiang/");
			if (!dbPath.exists()) {
				dbPath.mkdirs();
			}
			// 创建数据库环境
			dbEnvironment = new Environment(dbPath, envConfig);
			// 创建事务
			Transaction transaction = dbEnvironment
					.beginTransaction(null, null);
			// 数据库配置
			DatabaseConfig dbConfig = new DatabaseConfig();
			dbConfig.setTransactional(true);
			// 如果没有数据库配置就创建
			dbConfig.setAllowCreate(true);
			// 数据库设置一个key不允许存储多个值
			dbConfig.setSortedDuplicates(false);
			// 打开数据库
			database = dbEnvironment.openDatabase(transaction, "MyBerkeleyDB",
					dbConfig);
			// 提交事务
			transaction.commit();
		} catch (DatabaseException e) {
			e.printStackTrace();
		}

	}

	// 读取数据
	private void reader() {
		try {
			DatabaseEntry keyEntry = new DatabaseEntry();
			DatabaseEntry dataEntry = new DatabaseEntry();
			// 创建事务
			Transaction transaction = dbEnvironment
					.beginTransaction(null, null);
			// 打开游标
			Cursor cursor = database.openCursor(transaction, null);

			while (cursor.getNext(keyEntry, dataEntry, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
				
				
				// 实体转字符串
				System.out.println("key="
						+ StringBinding.entryToString(keyEntry) + " data="
						+ StringBinding.entryToString(dataEntry));

			}
			cursor.close();
			transaction.commitNoSync();
		} catch (DatabaseException e) {
			e.printStackTrace();
		}
	}

	private static void Writer() {
		try {
			// Berkeley数据库环境配置
			EnvironmentConfig envConfig = new EnvironmentConfig();
			// 配置事务
			envConfig.setTransactional(true);
			// 不存在就创建环境
			envConfig.setAllowCreate(true);
			// 设置数据库保存路径
			File dbPath = new File("c:/fiang/");
			if (!dbPath.exists()) {
				dbPath.mkdirs();
			}
			// 创建数据库环境
			Environment dbEnvironment = new Environment(dbPath, envConfig);
			// 创建事务
			Transaction transaction = dbEnvironment
					.beginTransaction(null, null);
			// 数据库配置
			DatabaseConfig dbConfig = new DatabaseConfig();
			dbConfig.setTransactional(true);
			// 如果没有数据库配置就创建
			dbConfig.setAllowCreate(true);
			// 数据库设置一个key不允许存储多个值
			dbConfig.setSortedDuplicates(false);
			// 打开数据库
			Database database = dbEnvironment.openDatabase(transaction,
					"MyBerkeleyDB", dbConfig);
			// 提交事务
			transaction.commit();
			// 数据库实体:键值对
			DatabaseEntry keyEntry = new DatabaseEntry();
			DatabaseEntry valueEntry = new DatabaseEntry();
			// 开启事务
			transaction = dbEnvironment.beginTransaction(null, null);
			// 存入键值对
			StringBinding.stringToEntry("key", keyEntry);
			StringBinding.stringToEntry("content内容数据", valueEntry);
			// 录入数据库,返回操作结果
			OperationStatus status = database.put(transaction, keyEntry,
					valueEntry);
			if (status != OperationStatus.SUCCESS) {
				throw new DatabaseException("data insert failed,status:"
						+ status);
			}
			// 同步
			transaction.commitNoSync();
			// 关闭数据库
			database.close();
			dbEnvironment.close();

		} catch (DatabaseException e) {
			e.printStackTrace();
		}

	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值