面向 Java 开发人员的 db4o 指南: db4o 中的数据库重构
2010-04-01 00:00:00 来源:WEB开发网接下来,我填充数据库,如清单 2 所示:
清单 2. ‘t0’ 时的数据库
import java.io.*;
import java.lang.reflect.*;
import com.db4o.*;
import com.tedneward.model.*;
// Version 1
public class BuildV1
{
public static void main(String[] args)
throws Exception
{
new File(".", "persons.data").delete();
ObjectContainer db = null;
try
{
db = Db4o.openFile("persons.data");
Person brianG = new Person("Brian", "Goetz", 39);
Person jason = new Person("Jason", "Hunter", 35);
Person brianS = new Person("Brian", "Sletten", 38);
Person david = new Person("David", "Geary", 55);
Person glenn = new Person("Glenn", "Vanderberg", 40);
Person neal = new Person("Neal", "Ford", 39);
Person clinton = new Person("Clinton", "Begin", 19);
db.set(brianG);
db.set(jason);
db.set(brianS);
db.set(david);
db.set(glenn);
db.set(neal);
db.set(clinton);
db.commit();
// Find all the Brians
ObjectSet brians = db.get(new Person("Brian", null, 0));
while (brians.hasNext())
System.out.println(brians.next());
}
finally
{
if (db != null)
db.close();
}
}
}
更多精彩
赞助商链接