开发步骤:
1.打开对象数据库:
ObjectContainer db = Db4o.openFile(Util.DB4OFILENAME);//Util.DB4OFILENAME为DB4O数据库名.ObjectContainer //db为对象容器,即数据库对象
2.向对象数据库插入对象
public static void storeFirstPilot(ObjectContainer db) {
Pilot pilot1 = new Pilot("tom", 55);
db.set(pilot1);
System.out.println("Stored-->" + pilot1);
}//向对象数据库插入对象
//向对象数据库插入对象
public static void storeObject(ObjectContainer db, Object obj) {
db.set(obj);
System.out.println("Stored Object ---->" + obj);
}
3.检索对象数据
//QBE方式检索对象数据库中所有数据
public static void retrieveAllPilotQBE(ObjectContainer db) {
Pilot proto = new Pilot(null, 0);
ObjectSet result = db.get(proto);
listResult(result);
}
//Native Queries方式检索所有数据
public static void retrieveAllPilots(ObjectContainer db) {
ObjectSet result = db.get(Pilot.class);
listResult(result);
}
//根据对象STRING字段检索
public static void retrievePilotByName(ObjectContainer db) {
Pilot pilot = new Pilot("tom", 0);
ObjectSet os = db.get(pilot);
listResult(os);
}
//根据对象INT字段检索
public static void retrievePilotByInt(ObjectContainer db) {
Pilot pilot = new Pilot(null, 55);
ObjectSet os = db.get(pilot);
listResult(os);
}
//根据对象STRING字段检索后,增加INT值
public static void updateObject(ObjectContainer db) {
ObjectSet os = db.get(new Pilot("tom", 0));
Pilot pi = (Pilot) os.next();
pi.addPoints(100);
db.set(pi);
System.out.println("added for --->" + pi);
retrieveAllPilots(db);
}
//根据对象STRING字段检索后,删除对象
public static void deleteObjectByName(ObjectContainer db, String str) {
ObjectSet os = db.get(new Pilot(str, 0));
Pilot pi = (Pilot) os.next();
db.delete(pi);
System.out.println("Deleted--->" + pi);
retrieveAllPilots(db);
}
//QE方式检索对象
public static ObjectSet queryAllObjectByQE(ObjectContainer db, Class cls,
String field, String str) {
Query query = db.query();
query.constrain(cls);
query.descend(field).constrain(str);
ObjectSet os = query.execute();
return os;
}
public static ObjectSet queryObjectByQE(ObjectContainer db, Class cls,
String field, Integer x) {
Query query = db.query();
query.constrain(cls);
query.descend(field).constrain(x);
/*constrain:
词性及解释 Part of speech and defination
vt. 强迫, 限制, 关押
*/
return query.execute();
}
//否定方式QE检索
public static ObjectSet queryByNegation(ObjectContainer db, Class cls,
String field, String value) {
Query query = db.query();
query.constrain(cls);
query.descend(field).constrain(value).not();
/* descend:
词性及解释 Part of speech and defination
vi. 下降, 世代相传, 屈尊, 袭击
vt. 下降
【法】 把财产传给, 遗传
*/
return query.execute();
}
//conjunction方式QE检索
public static ObjectSet retrieveByConjunction(ObjectContainer db, Class xls,
Object obj1, Object obj2,
String f1, String f2) {
Query query = db.query();
query.constrain(xls);
Constraint constr = query.descend(f1).constrain(obj1);
query.descend(f2).constrain(obj2).and(constr);
/* conjunction:
词性及解释 Part of speech and defination
n. 连接词, 联合, 结合
【计】 合取
*/
return query.execute();
}
//Disjunction方式QE检索,即相当于SQL中的OR查询
public static ObjectSet retrieveByDisjunction(ObjectContainer db, Class xls,
String f1, Object value1,
String f2, Object value2) {
Query query = db.query();
query.constrain(xls);
Constraint constr = query.descend(f1).constrain(value1);
query.descend(f2).constrain(value2).or(constr);
/*Disjunction:
[] 词性及解释 Part of speech and defination
n. 分离, 分裂, 分断
*/
return query.execute();
}
//Comparison方式QE检索,即检索当f1字段值大于value1的对象数据
public static ObjectSet retrieveByComparison(ObjectContainer db,Class xls,String f1,Object value1)
{
Query query=db.query();
query.descend(f1).constrain(value1).greater();
/*comparison:
词性及解释 Part of speech and defination
n. 比较, 对照, 比喻
【经】 比较, 对比
*/
return query.execute();
以下是总结出来的DB4O主要对象图:
}