(1) Open database environment
Environmentconfig envcfg = new Environmentconfig ();
When the database environment does not exist, create a database environment that defaults to false.
Envcfg.setallowcreate (true);
Opens as read-only, and defaults to false.
Envcfg.setreadonly (false);
Transaction support, default to False.
Envcfg.settransactional (true);
Environment mydbenv = new Environment (new File (Envhome), envcfg);
System.out.println ("Env Config:" + mydbenv.getconfig ());
(2) Open database
Databaseconfig dbcfg = new Databaseconfig ();
Dbcfg.setallowcreate (true);
Dbcfg.setreadonly (false);
Dbcfg.settransactional (true);
Database mytestdb = mydbenv.opendatabase (null, "Vendordb", dbcfg);
System.out.println ("Database Name:" + mytestdb.getdatabasename ());
(3) Simple crud
C String key = "key-rensanning"; String value = "This is a test!
(000) ";
Databaseentry keyentry = new Databaseentry (key.getbytes ("Utf-8"));
Databaseentry valentry = new Databaseentry (value.getbytes ("Utf-8"));
Operationstatus status = Mytestdb.put (null, keyentry, valentry);
System.out.println ("Put status:" + status);
R databaseentry valget = new Databaseentry ();
Status = Mytestdb.get (null, Keyentry, Valget, Lockmode.default);
if (status = = operationstatus.success) {value = new String (Valget.getdata (), "utf-8");
System.out.println ("Read value:" + value);} U value = "This is a test!
(111) ";
Status = Mytestdb.put (null, Keyentry, New Databaseentry (Value.getbytes ("Utf-8"));
Status = Mytestdb.get (null, Keyentry, Valget, Lockmode.default);
if (status = = operationstatus.success) {value = new String (Valget.getdata (), "utf-8");
System.out.println ("Update value:" + value);}
D status = Mytestdb.delete (null, keyentry);
System.out.println ("Delete Status:" + status);
(4) different ways of writing data
String key = "key-rensanning";
Databaseentry keyentry = new Databaseentry (key.getbytes ("Utf-8"));
Databaseentry valget = new Databaseentry (); String value = "This is a test!
(By ' put ' method '); Database.put (): Writes data to the database and, if duplicate records are not supported, overwrites the existing records corresponding to the update key operationstatus status = Mytestdb.put (null, Keyentry, new
Databaseentry (Value.getbytes ("Utf-8"));
Status = Mytestdb.get (null, Keyentry, Valget, Lockmode.default);
if (status = = operationstatus.success) {value = new String (Valget.getdata (), "utf-8");
System.out.println ("Put value:" + value);} Database.putnooverwrite (): Writes data to the database, but if key already exists, does not overwrite existing data (even if the database supports duplicate key) value = "This is a test!
(By ' putnooverwrite ' method) ";
Status = Mytestdb.putnooverwrite (null, Keyentry, New Databaseentry (Value.getbytes ("Utf-8"));
Status = Mytestdb.get (null, Keyentry, Valget, Lockmode.default);
if (status = = operationstatus.success) {value = new String (Valget.getdata (), "utf-8");
System.out.println ("Putnooverwrite value:" + value);} Set whether a key is allowed to be savedStoring Multiple values * * * databaseconfig dbcfg = new Databaseconfig ();
Dbcfg.setallowcreate (TRUE);
Dbcfg.setreadonly (FALSE);
Dbcfg.settransactional (TRUE);
Dbcfg.setsortedduplicates (TRUE);
Database mytestdb2 = Mytestdb.getenvironment (). OpenDatabase (NULL, "Duplicatesdb", dbcfg);
Database.putnodupdata (): Writes data to the database (this method is used only for databases that support duplicate key), and if the corresponding record for key and value already exists, the result is: operationstatus.keyexist Value = "This is a test!
(By ' putnodupdata ' method) ";
Status = Mytestdb2.putnodupdata (null, Keyentry, New Databaseentry (Value.getbytes ("Utf-8"));
if (status = = operationstatus.success) {value = new String (Valget.getdata (), "utf-8"); System.out.println ("Putnooverwrite value:" + value);}
else if (status = = Operationstatus.keyexist) {System.out.println ("Putnodupdata keyexist:" + key);}
Status = Mytestdb2.get (null, Keyentry, Valget, Lockmode.default);
if (status = = operationstatus.success) {value = new String (Valget.getdata (), "utf-8");
System.out.println ("Putnodupdata value:" + value);} if (null!= mytestdb2) {
Mytestdb2.close ();
MYTESTDB2 = null;
}
(5) Different ways of reading data
String key = "key-rensanning";
String value = "This is a test! (m) ";
Databaseentry keyentry = new Databaseentry (key.getbytes ("Utf-8"));
Databaseentry valentry = new Databaseentry (value.getbytes ("Utf-8"));
Operationstatus status = Mytestdb.put (null, keyentry, valentry);
System.out.println ("Put status:" + status);
Databaseentry valget = new Databaseentry ();
Database.get (): Retrieves key corresponding record
status = Mytestdb.get (null, Keyentry, Valget, lockmode.default);
if (status = = operationstatus.success) {
value = new String (Valget.getdata (), "Utf-8");
System.out.println ("Read value (GET):" + value);
}
Database.getsearchboth (): Retrieving database records based on key and value
status = Mytestdb.getsearchboth (null, Keyentry, Valget, Lockmode.default);
if (status = = operationstatus.success) {
value = new String (Valget.getdata (), "Utf-8");
System.out.println ("Read Value (Getsearchboth):" + value);
}
(6) Reading and writing of objects
C
String key = "Key-rensanning-object";
Person value = new Person (9527, "rensanning", true);
Databaseentry keyentry = new Databaseentry (key.getbytes ("Utf-8"));
Databaseentry valentry = new Databaseentry ();
persontuplebinding personbinding = new persontuplebinding ();
Personbinding.objecttoentry (value, valentry);
Operationstatus status = Mytestdb.put (null, keyentry, valentry);
System.out.println ("Put person Status:" + status);
R
databaseentry valget = new Databaseentry ();
Status = Mytestdb.get (null, Keyentry, Valget, lockmode.default);
if (status = = operationstatus.success) {
value = Personbinding.entrytoobject (valget);
System.out.println ("Read person Value:" + value.getid () + "\ T" + value.getname () + "T" + value.issex ());
}
class Person {
int id;
String name;
Boolean sex;
Public person () {
} public person
(int id, String name, Boolean sex) {
this.id = ID;
this.name = name;
This.sex = sex;
}
public int getId () {return
ID;
}
public void setId (int id) {
this.id = ID;
}
Public String GetName () {return
name;
}
public void SetName (String name) {
this.name = name;
}
public Boolean issex () {return
sex;
}
public void Setsex (Boolean sex) {
this.sex = sex;
}
}
Class Persontuplebinding extends Tuplebinding<person> {
@Override public person
Entrytoobject ( Tupleinput input) {person
p = new Person ();
P.setid (Input.readint ());
P.setname (Input.readstring ());
P.setsex (Input.readboolean ());
return p;
}
@Override public
void Objecttoentry (person p, tupleoutput output) {
output.writeint (P.getid ());
Output.writestring (P.getname ());
Output.writeboolean (P.issex ());
}
(7) Direct Persistence layer application (Dpl:direct persistence Layer)
Environment env = mytestdb.getenvironment ();
Storeconfig storeconfig = new Storeconfig ();
Storeconfig.setallowcreate (TRUE);
Storeconfig.settransactional (TRUE);
Entitystore store = new Entitystore (env, "Storedb", storeconfig);
primaryindex<string, userinfo> pindex = Store.getprimaryindex (String.class, Userinfo.class);
C Pindex.put (New UserInfo ("001", "user001"));
Pindex.put (New UserInfo ("002", "user002"));
Pindex.put (New UserInfo ("003", "user003"));
Pindex.put (New UserInfo ("004", "user004"));
Pindex.put (New UserInfo ("005", "user005"));
R String MyKey = "001";
UserInfo getData = Pindex.get (MyKey);
System.out.println ("Read User 001:" + getData);
U Pindex.put (New UserInfo ("002", "user002222"));
Read all entitycursor<userinfo> cursor = pindex.entities ();
try {iterator<userinfo> i = cursor.iterator ();
while (I.hasnext ()) {System.out.println ("Cursor data:" + i.next ());
}} finally {Cursor.close ();}
D String pkey = "003"; Boolean flag = Pindex.deleTe (Pkey);
System.out.println ("Delete object:" + Pkey + "Result:" + flag);
Close Store if (store!= null) {store.close ();
store = null;
}
@Entity
@SuppressWarnings ("Serial")
class UserInfo implements Serializable {
@PrimaryKey
private String userId;
Private String userName;
Public UserInfo () {
} public
UserInfo (string userId, String userName) {
This.userid = userId;
This.username = UserName;
}
Public String GetUserID () {return
userId;
}
public void Setuserid (String userId) {
This.userid = userId;
}
Public String GetUserName () {return
userName;
}
public void Setusername (String userName) {
this.username = userName;
}
@Override public
String toString () {return
"UserInfo [userid= + userId +", username= "+ UserName +]";
}
}
(9) Cursor operation
C for (int i = 0; i < 5 i++) {mytestdb.put (null, New Databaseentry ("KEY" + (i+1)). GetBytes ("Utf-8"), New Databaseen
Try (("VALUE" + (i+1)). GetBytes ("Utf-8"));
} databaseentry key = new Databaseentry ();
Databaseentry value = new Databaseentry ();
D (by Cursor) Transaction Txn = Mytestdb.getenvironment (). BeginTransaction (null, NULL);
Cursor Cursor1 = mytestdb.opencursor (TXN, NULL);
Operationstatus RESULT1 = Cursor1.getfirst (key, value, NULL); while (result1 = = operationstatus.success) {if ("VALUE3". Equals (New String (Value.getdata (), "Utf-8")) {Cursor1.delet
E ();
} RESULT1 = Cursor1.getnext (key, value, NULL);
} if (Cursor1!= null) {Cursor1.close ();} if (Txn!= null) {Txn.commit ();}
R (by Cursor) Cursor Cursor2 = mytestdb.opencursor (null, NULL);
Operationstatus result2 = Cursor2.getfirst (key, value, NULL); while (result2 = = operationstatus.success) {System.out.println ("Cursor Read Value:" + New String (Value.getdata (), "Utf-8
")); Result2 = Cursor2.getnext (kEY, value, NULL);
} if (Cursor2!= null) {Cursor2.close ();}
http://rensanning.iteye.com/blog/1872481