Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
First Steps. (db4o - Object Oriented Database)
1. #2
First Steps
Wildan Maulana
wildan.m@openthinklabs.com
2. First Steps
package com.db4odoc.f1.chapter1; ....
public class Pilot { public String getName() {
private String name; return name;
private int points; }
public Pilot(String name,int points) { public String toString() {
this.name=name; return name+"/"+points;
this.points=points; }
} }
public int getPoints() {
return points;
}
public void addPoints(int points) {
this.points+=points;
}
.....
3. Opening the database
// accessDb4o
ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded
.newConfiguration(), DB4OFILENAME);
try {
// do something with db4o
} finally {
db.close();
}
4. Storing objects
// storeFirstPilot
Pilot pilot1 = new Pilot("Michael Schumacher", 100);
db.store(pilot1);
System.out.println("Stored " + pilot1);
// storeSecondPilot
Pilot pilot2 = new Pilot("Rubens Barrichello", 99);
db.store(pilot2);
System.out.println("Stored " + pilot2);
5. Retrieving objects
public static void listResult(List<?> result){
System.out.println(result.size());
for (Object o : result) {
System.out.println(o); Shorcut for JDK 5
}
}
List <Pilot> pilots = db.query(Pilot.class);
// retrieveAllPilotQBE
Pilot proto = new Pilot(null, 0);
ObjectSet result = db.queryByExample(proto);
listResult(result);
// retrieveAllPilots
ObjectSet result = db.queryByExample(Pilot.class);
listResult(result);
6. Retrieving objects
Query Pillot By Name
// retrievePilotByName
Pilot proto = new Pilot("Michael Schumacher", 0);
ObjectSet result = db.queryByExample(proto);
listResult(result);
Query for Pilots with a specific number of points
// retrievePilotByExactPoints
Pilot proto = new Pilot(null, 100);
ObjectSet result = db.queryByExample(proto);
listResult(result);
7. Updating objects
// updatePilot
ObjectSet result = db
.queryByExample(new Pilot("Michael Schumacher", 0));
Pilot found = (Pilot) result.next();
found.addPoints(11);
db.store(found);
System.out.println("Added 11 points for " + found);
retrieveAllPilots(db);
8. Deleting objects
// deleteFirstPilotByName
ObjectSet result = db
.queryByExample(new Pilot("Michael Schumacher", 0));
Pilot found = (Pilot) result.next();
db.delete(found);
System.out.println("Deleted " + found);
retrieveAllPilots(db);
// deleteSecondPilotByName
ObjectSet result = db
.queryByExample(new Pilot("Rubens Barrichello", 0));
Pilot found = (Pilot) result.next();
db.delete(found);
System.out.println("Deleted " + found);
retrieveAllPilots(db);