最近在研究网络爬虫,需要用到Berkeley DB JAva Edition,学习了一下,照着写了一份代码,如下:
package test;
import java.io.File;
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 MyBerkeleyDB {
/**
* @param args
*/
Environment env = null;
Database db = null;
public MyBerkeleyDB(){
}
public void setUp(String path, long cacheSize){
EnvironmentConfig envC = new EnvironmentConfig();
envC.setAllowCreate(true);
envC.setCacheSize(cacheSize);
try{
env = new Environment(new File(path), envC);
}catch(DatabaseException e){
e.printStackTrace();
}
}
//需要在一个已存在的Environment环境下打开DataBase
public void open(String dbName){
DatabaseConfig dbConfig = new DatabaseConfig();
try{
db = env.openDatabase(null, dbName, dbConfig);
}catch(DatabaseException e){
e.printStackTrace();
}
}
//关闭:先关数据库,再关环境
public void Close(){
try{
if(db != null)
db.close();
if(env != null)
env.close();
}catch(DatabaseException e){
e.printStackTrace();
}
}
public String get(String key) throws Exception{
DatabaseEntry queryEntry = new DatabaseEntry();
DatabaseEntry value = new DatabaseEntry();
queryEntry.setData(key.getBytes("UTF-8"));
OperationStatus status = db.get(null, queryEntry, value, LockMode.DEFAULT);
if(status == OperationStatus.SUCCESS ){
return new String(value.getData());
}
return null;
}
public boolean put(String key, String value) throws Exception{
byte[] thekey = key.getBytes("UTF-8");
byte[] theValue = value.getBytes("UTF-8");
OperationStatus status = db.put(null, new DatabaseEntry(thekey), new DatabaseEntry(theValue));
if(status == OperationStatus.SUCCESS)
return true;
return false;
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
MyBerkeleyDB myBerkeley = new MyBerkeleyDB();
myBerkeley.setUp("E:\\dbEnv", 1000000);
myBerkeley.open("myDataBase");
System.out.println("开始向数据库存入信息");
for(int i = 0; i < 20; i ++)
{
try{
String key = "myKey" + i;
String value = "myValue" +i;
System.out.println("[" + key +"," + value + "]");
myBerkeley.put(key, value);
}catch(Exception e){
e.printStackTrace();
}
}
myBerkeley.Close();
/*MyBerkeleyDB mbdb = new MyBerkeleyDB();
mbdb.setUp("E:\\bdb", 1000000);
mbdb.open("myDB");
while(true) {
try {
for(int i=1; i<=10; i++) {
System.out.println("从Berkeley DB中取出数据...\n");
String k = "myKey" + 2*i;
String v = mbdb.get(k);
System.out.println("[" + k +":" + v +"]");
Thread.sleep(3000);
if(i == 10) {
mbdb.Close();
System.exit(1);
}
}
Thread.sleep(300000);
} catch (Exception e) {
e.printStackTrace();
}
}*/
}
}
却出现了很多异常,如下:
com.sleepycat.je.DatabaseNotFoundException: (JE 5.0.55) Database myDataBase not found.
at com.sleepycat.je.Environment.setupDatabase(Environment.java:797)
at com.sleepycat.je.Environment.openDatabase(Environment.java:607)
at test.MyBerkeleyDB.open(MyBerkeleyDB.java:38)
at test.MyBerkeleyDB.main(MyBerkeleyDB.java:81)
java.lang.NullPointerException
at test.MyBerkeleyDB.put(MyBerkeleyDB.java:70)
at test.MyBerkeleyDB.main(MyBerkeleyDB.java:89)
java.lang.NullPointerException
at test.MyBerkeleyDB.put(MyBerkeleyDB.java:70)
at test.MyBerkeleyDB.main(MyBerkeleyDB.java:89)
java.lang.NullPointerException
at test.MyBerkeleyDB.put(MyBerkeleyDB.java:70)
at test.MyBerkeleyDB.main(MyBerkeleyDB.java:89)
java.lang.NullPointerException
at test.MyBerkeleyDB.put(MyBerkeleyDB.java:70)
at test.MyBerkeleyDB.main(MyBerkeleyDB.java:89)
java.lang.NullPointerException
at test.MyBerkeleyDB.put(MyBerkeleyDB.java:70)
at test.MyBerkeleyDB.main(MyBerkeleyDB.java:89)
java.lang.NullPointerException
at test.MyBerkeleyDB.put(MyBerkeleyDB.java:70)
at test.MyBerkeleyDB.main(MyBerkeleyDB.java:89)