继续上篇《java之BerkeleyDB(一)》
上篇我们先直接看了个例子:
有几个注意的,在BDB数据库里,默认是不能有重复的两个相同的键,当然可以通过config配置sortedDupli...来设置可以,所以在读取数据库值的时候必须考虑两种情况,是否存在相同的键的记录
JE provides two basic mechanisms for the storage and retrieval of database key/data pairs:
-
The
Database.put()
andDatabase.get()
methods provide the easiest access for all non-duplicate records in the database. These methods are described in this section. -
Cursors provide several methods for putting and getting database records. Cursors and their database access methods are described in Using Cursors.
-
Database.put()
-
Database.putNoOverwrite()
-
Database.putNoDupData()
-
Database.get() 这个利用key来找
-
Database.getSearchBoth() 这个利用Key和value共同找记录
上篇我们简单的打开了数据库和进行简单的存储
这个对于我们存储数据的基本类型还行,但是涉及到用户自定义类的时候,单靠之前的转换函数来从string转换程对象是不妨便的,我们来看看BDB为我们准备来什么?
引自使用手册:
DatabaseEntry
can hold any kind of data from simple Java primitive types to complex Java objects so long as that data can be represented as a Java byte
array. Note that due to performance considerations,you should not use Java serialization to convert a Java object to a byte
array. Instead, use the Bind APIs to perform this conversion (see