java berkeley db 教程_How to use Berkeley DB Java Edition

在学习爬虫的过程中,使用到了Berkeley DB。入门的使用方法,简洁有用。

——————————————————————————————————————————————————————————————————————

Our previous post “Introduction to Berkeley DB” gave an overview of the features and functionality of this popular DBMS. This post will show practical examples of the usage of Berkeley DB Java Edition in a Java application.

Versions of Berkeley DB for Java

There are two different versions of the Java API for Berkeley DB

Berkeley DB standard

The com.sleepycat.db package is an interface to the same system library used by other languages such as C, Perl and PHP to access BDB databases. This ensures the compatibility, allowing the use in a java application of a database created by an application written in other language.

Berkeley DB Java Edition

The com.sleepycat.je package is a 100% Java package. Databases created with this package can be moved to different platforms to be used by other java applications using the same library.

This post explains how to use Berkeley DB Java Edition.

Installation of Berkeley DB Java Edition

To install Berkeley DB Java edition, navigate in a browser to the Berkeley DB downloads page:

From that page, download the file Berkeley DB Java Edition 5.0.58.tar.gz (13M)

To install, just uncompress and extract the files in a directory.

Note: If you chose to download the zip-compressed version of the file, use the -U option to preserve the capitalization of the file names.

Once installed, the file je-5.0.58/lib/je-5.0.58.jarmust be added to the classpath to compile an run a java application that uses it.

In the directory je-5.0.58/lib  there are also other versions of the library, including one for the Android platform:

Creating, opening and closing a database

The following code can be used to open a database, or create a new database:

In Berkeley DB Java Edition, an object of class Environment is mandatory to work with a BDB database. The first step in opening the database is creating this Environment object will a call to “new Environment”. The first argument to this call is the path where the data of the environment (including the databases in it) is located. The second argument is an object of class “EnvironmentConfig”. The call to “envConfig.setAllowCreate(true)” tells the constructor of the Environment object to create the required files if the environment did not previously exist.

Next, the database is opened with a call to “Environment.openDatabase”. This method receives as arguments the name of the database to be opened, and an object of class “DatabaseConfig”. In the same way as with the creation of the environment, a call to setAllowCreate(true) in the database configuration tells the constructor to create a new database if it did not exist.

When the application finishes making use of them, both the database and the environment must be closed like this:

The example program “TestBDB.java” is compiled and executed with the commands:

Writing records to the database

A record is written to the database with a call to the “put()” method of the Database object. This method receives as arguments two objects of the class DatabaseEntry, holding the key and the value of the record.

Reading records from the database

The “get()” method in class Database can be used to retrieve a record. This method returns an object of class DataEntry, that must be converted to the desired object type.

Deleting records

The “delete” method in the Database class is used to delete a record from the database:

Process all records in the database

The database can be traversed with the help of a cursor:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值