Berkeley DB 开源的文件数据库
开源的文件数据库,介于关系数据库和内存数据库之间,按键值对方式存储
下面是一个实例,是网上抄的,然后自己本机运行的
package org; import java.io.File; import java.io.UnsupportedEncodingException; import java.util.concurrent.TimeUnit; 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 BerkeleyDB { private Environment environment = null; //数据库环境 private DatabaseConfig dbConfig = null; //数据库配置 private Database database = null; //数据库对象 private String fileName = "D:\\wzx"; private String dbName = "wzx"; public void openDatabase(){ EnvironmentConfig envConfig = new EnvironmentConfig(); envConfig.setAllowCreate(true); envConfig.setTransactional(true); envConfig.setReadOnly(false); envConfig.setTxnTimeout(10000, TimeUnit.MILLISECONDS); envConfig.setLockTimeout(10000, TimeUnit.MILLISECONDS); File file = new File(fileName); if(!file.exists()){ file.mkdirs(); } environment = new Environment(file, envConfig); dbConfig = new DatabaseConfig(); dbConfig.setAllowCreate(true); dbConfig.setTransactional(true); dbConfig.setReadOnly(false); if(database == null) database = environment.openDatabase(null, dbName, dbConfig); } public boolean insert(String key, String value, boolean isOverWrite){ try { DatabaseEntry theKey = new DatabaseEntry(key.getBytes("UTF-8")); DatabaseEntry theData = new DatabaseEntry(value.getBytes("UTF-8")); OperationStatus os = null; Transaction transaction = null; TransactionConfig txnConfig = new TransactionConfig(); txnConfig.setSerializableIsolation(true); transaction = environment.beginTransaction(null, txnConfig); if(isOverWrite){ os = database.put(transaction, theKey, theData); }else{ os = database.putNoOverwrite(transaction, theKey, theData); } transaction.commit(); if(os == OperationStatus.SUCCESS){ return true; }else{ return false; } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return false; } public String query(String key){ try { DatabaseEntry theKey = new DatabaseEntry(key.getBytes("UTF-8")); DatabaseEntry theData = new DatabaseEntry(); Transaction transaction = null; TransactionConfig txnConfig = new TransactionConfig(); txnConfig.setSerializableIsolation(true); transaction = environment.beginTransaction(null, txnConfig); OperationStatus os = database.get(transaction, theKey, theData, LockMode.DEFAULT); transaction.commit(); if(os == OperationStatus.SUCCESS){ String value = new String(theData.getData(), "UTF-8"); return value; }else{ return ""; } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public void close(){ if(database != null){ database.close(); } if(environment != null){ environment.cleanLog(); environment.close(); } } public static void main(String[] args) { BerkeleyDB berkeleyDb = new BerkeleyDB(); berkeleyDb.openDatabase(); /*berkeleyDb.insert("China", "北京", true); berkeleyDb.insert("US", "纽约", true);*/ System.out.println(berkeleyDb.query("China")); } }
相关推荐
CoderToy 2020-11-16
技术之博大精深 2020-10-16
emmm00 2020-11-17
bianruifeng 2020-11-16
云中舞步 2020-11-12
世樹 2020-11-11
暗夜之城 2020-11-11
张荣珍 2020-11-12
amienshxq 2020-11-14
ASoc 2020-11-14
yungpheng 2020-10-19
loveyouluobin 2020-09-29
尘封飞扬 2020-09-29
Coder技术文摘 2020-09-29
lbyd0 2020-11-17
BigYellow 2020-11-16
sushuanglei 2020-11-12
我心似明月 2020-11-09