public void testBdb()throws Exception{
EnvironmentConfig envConfig = new EnvironmentConfig();
envConfig.setAllowCreate(true);//如果不存在则创建一个 File f=new File("d:/test/bdbtest"); if(!f.exists()){
f.mkdirs();
}
Environment myDbEnvironment = new Environment(f, envConfig); DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setAllowCreate(true); Database myDatabase = myDbEnvironment.openDatabase(null, "sampleDatabase", dbConfig); //打开一个数据库,数据库名为sampleDatabase StoredClassCatalog classCatalog = new StoredClassCatalog(myDatabase);
EntryBinding dataBinding = new SerialBinding(classCatalog, ArrayList.class);//绑定list对象 for(int i=0;i <10;i++){
DatabaseEntry theKey=new DatabaseEntry(String.valueOf(i).getBytes("UTF-8"));//---------------------
DatabaseEntry theData=new DatabaseEntry();
ArrayList list=new ArrayList();//创建一个新的list加入数据库
list.add(i);
dataBinding.objectToEntry(list, theData);
myDatabase.put(null, theKey, theData);
}
Cursor cursor = myDatabase.openCursor(null, null);
DatabaseEntry foundKey = new DatabaseEntry();
DatabaseEntry foundData = new DatabaseEntry();
//int count=0;
while (cursor.getNext(foundKey, foundData, LockMode.DEFAULT) ==
OperationStatus.SUCCESS) {//遍历数据库
//count++; //if(count> 7){
String keyString = new String(foundKey.getData(), "UTF-8");//-------------------------
ArrayList list=(ArrayList)dataBinding.entryToObject(foundData);//在此处报异常
System.out.println(keyString+":"+list.size());
//} } cursor.close();
if (myDatabase != null) {
myDatabase.close();
}
if (myDbEnvironment != null) {
myDbEnvironment.cleanLog();
myDbEnvironment.close();
} }
当我运行以上方法的时候会报
Exception in thread "main" com.sleepycat.util.RuntimeExceptionWrapper: invalid type code: 03
at com.sleepycat.bind.serial.SerialBinding.entryToObject(SerialBinding.java:124)
at cn.yy3g.wcs.service.MainTest.testBdb(MainTest.java:156)
at cn.yy3g.wcs.service.MainTest.main(MainTest.java:51)
Caused by: java.io.StreamCorruptedException: invalid type code: 03
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1356)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
at com.sleepycat.bind.serial.SerialBinding.entryToObject(SerialBinding.java:122)
... 2 more 异常。原因是前几条信息好像是数据库的配置信息, 请问 如果我想遍历数据库那么么应该怎样写才正确呢。bdb有哪个方法提供了这个遍历接口呢 ???找了半天没找到,郁闷中,求高人指教。
EnvironmentConfig envConfig = new EnvironmentConfig();
envConfig.setAllowCreate(true);//如果不存在则创建一个 File f=new File("d:/test/bdbtest"); if(!f.exists()){
f.mkdirs();
}
Environment myDbEnvironment = new Environment(f, envConfig); DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setAllowCreate(true); Database myDatabase = myDbEnvironment.openDatabase(null, "sampleDatabase", dbConfig); //打开一个数据库,数据库名为sampleDatabase StoredClassCatalog classCatalog = new StoredClassCatalog(myDatabase);
EntryBinding dataBinding = new SerialBinding(classCatalog, ArrayList.class);//绑定list对象 for(int i=0;i <10;i++){
DatabaseEntry theKey=new DatabaseEntry(String.valueOf(i).getBytes("UTF-8"));//---------------------
DatabaseEntry theData=new DatabaseEntry();
ArrayList list=new ArrayList();//创建一个新的list加入数据库
list.add(i);
dataBinding.objectToEntry(list, theData);
myDatabase.put(null, theKey, theData);
}
Cursor cursor = myDatabase.openCursor(null, null);
DatabaseEntry foundKey = new DatabaseEntry();
DatabaseEntry foundData = new DatabaseEntry();
//int count=0;
while (cursor.getNext(foundKey, foundData, LockMode.DEFAULT) ==
OperationStatus.SUCCESS) {//遍历数据库
//count++; //if(count> 7){
String keyString = new String(foundKey.getData(), "UTF-8");//-------------------------
ArrayList list=(ArrayList)dataBinding.entryToObject(foundData);//在此处报异常
System.out.println(keyString+":"+list.size());
//} } cursor.close();
if (myDatabase != null) {
myDatabase.close();
}
if (myDbEnvironment != null) {
myDbEnvironment.cleanLog();
myDbEnvironment.close();
} }
当我运行以上方法的时候会报
Exception in thread "main" com.sleepycat.util.RuntimeExceptionWrapper: invalid type code: 03
at com.sleepycat.bind.serial.SerialBinding.entryToObject(SerialBinding.java:124)
at cn.yy3g.wcs.service.MainTest.testBdb(MainTest.java:156)
at cn.yy3g.wcs.service.MainTest.main(MainTest.java:51)
Caused by: java.io.StreamCorruptedException: invalid type code: 03
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1356)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
at com.sleepycat.bind.serial.SerialBinding.entryToObject(SerialBinding.java:122)
... 2 more 异常。原因是前几条信息好像是数据库的配置信息, 请问 如果我想遍历数据库那么么应该怎样写才正确呢。bdb有哪个方法提供了这个遍历接口呢 ???找了半天没找到,郁闷中,求高人指教。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货