// 环境

EnvironmentConfig envConfig = new EnvironmentConfig();

envConfig.setTransactional(true);

envConfig.setAllowCreate(true);

Environment env = new Environment(new File("E:/tmpp"), envConfig);

// 数据库

DatabaseConfig dbConfig = new DatabaseConfig();

dbConfig.setTransactional(true);

dbConfig.setAllowCreate(true);

Database db = env.openDatabase(null, "TestLi", dbConfig);

// 添加数据

db.put(null, new DatabaseEntry("1".getBytes()), new DatabaseEntry("1".getBytes()));

// 读取数据

DatabaseEntry theData = new DatabaseEntry();

if (db.get(null, new DatabaseEntry("1".getBytes("UTF-8")), theData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {

byte[] retData = theData.getData();

String foundData = new String(retData, "UTF-8");

System.out.println("For key: '" + 1 + "' found data: '" + foundData + "'.");

} else {

System.out.println("No record found for key '" + 1 + "'.");

}

// 游标

Cursor cursor = db.openCursor(null, null);

DatabaseEntry foundKey = new DatabaseEntry();

DatabaseEntry foundData = new DatabaseEntry();

while (cursor.getNext(foundKey, foundData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {

String keyString = new String(foundKey.getData(), "UTF-8");

String dataString = new String(foundData.getData(), "UTF-8");

System.out.println("Key | Data : " + keyString + " | " + dataString + "");

}

DatabaseEntry searchKey = new DatabaseEntry("2".getBytes("UTF-8"));

DatabaseEntry searchData = new DatabaseEntry("2".getBytes("UTF-8"));

if (cursor.getSearchKey(searchKey, searchData, LockMode.DEFAULT) == OperationStatus.NOTFOUND) {

System.out.println(searchKey + "/" + searchData + " not matched in database " + db.getDatabaseName());

} else {

String key = new String(searchKey.getData(), "UTF-8");

String data = new String(searchData.getData(), "UTF-8");

System.out.println("Found record " + key + "|" + data);

}

cursor.close();

// 删除数据

db.delete(null, new DatabaseEntry("1".getBytes("UTF-8")));

// 二级库

// 其他操作

db.close();

// env.sync();// 提交事务

// env.removeDatabase(null, "TestLi"); //删除除数据,先关闭

// env.cleanLog(); //清理日志

env.close();