db4o基本用法

 Opening the database
// accessDb4o
ObjectContainer db=Db4o.openFile(Util.DB4OFILENAME);
try {
    // do something with db4o
}
finally {
    db.close();
}
Storing objects
// storeFirstPilot
Pilot pilot1=new Pilot("Michael Schumacher",100);
db.set(pilot1);
System.out.println("Stored "+pilot1);

Retrieving objects

public static void listResult(ObjectSet result) {
    System.out.println(result.size());
    while(result.hasNext()) {
        System.out.println(result.next());
    }
}

Query by Example (QBE)

// retrieveAllPilotQBE
Pilot proto=new Pilot(null,0);
ObjectSet result=db.get(proto);
listResult(result);

// retrieveAllPilots
ObjectSet result=db.get(Pilot.class);
listResult(result);

List <Pilot> pilots = db.query(Pilot.class);

Native Queries (NQ) 

List <Pilot> pilots = db.query(new Predicate<Pilot>() {
    public boolean match(Pilot pilot) {
        return pilot.getPoints() == 100;
    }
});

List <Pilot> result = db.query(new Predicate<Pilot>() {
    public boolean match(Pilot pilot) {
        return pilot.getPoints() > 99
            && pilot.getPoints() < 199
            || pilot.getName().equals("Rubens Barrichello");
   }
});

 SODA Query API (SODA)

// retrieveAllPilots
Query query=db.query();
query.constrain(Pilot.class);
ObjectSet result=query.execute();
listResult(result);

Updating objects

// updatePilot
ObjectSet result=db.get(new Pilot("Michael Schumacher",0));
Pilot found=(Pilot)result.next();
found.addPoints(11);
db.set(found);
System.out.println("Added 11 points for "+found);
retrieveAllPilots(db);

Deleting objects

// deleteFirstPilotByName
ObjectSet result=db.get(new Pilot("Michael Schumacher",0));
Pilot found=(Pilot)result.next();
db.delete(found);
System.out.println("Deleted "+found);
retrieveAllPilots(db);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值