- 论坛徽章:
- 0
|
1.查询所有
db = Db4o.openFile("customer.yap");
//查询所有customer
Customer allcus=new Customer();
ObjectSet set=db.get(allcus);
while(set.hasNext()){
System.out.println(((Customer)set.next()).getName());
}
2.更新对象
db = Db4o.openFile("customer.yap");
ObjectSet set=db.get(new Customer("persia2"));
Customer c1=(Customer)set.next();
c1.setPhoneNumber("82839681");
db.set(c1);
注意:
查询的操作将在内存中创建一个customer对象,然后通过ID与数据库中的对象来保持联系。
DB4O通过缓存OID作为一个若引用来保持联系。这个联系的维持将在数据库被关闭的时候停止。
要更新一个数据库中的对象,必须确保内存中的是最新的版本,否则将保存另一个新对象:
确保您具有存储或检索对象在同一事务(即自最后的ObjectContainer打开) ,然后您的更新。
如果不是, db4o将假定这是一个新的 对象,而不是一个已储存以前。
重新实体中的toString方法:
public String toString(){
return "[" + this.name + ";"+ this.phoneNumber + "]";
}就可以直接打印该对象了System.out.println(set.next());
3.删除对象
db = Db4o.openFile("customer.yap");
ObjectSet set=db.get(new Customer("persia2"));
Customer c1=(Customer)set.next();
db.delete(c1);
4.数据库配置
通过Db4o.configure().messageLevel(int level) 可以进行日志操作.
共有如下四级的日志选项:
0 No messages
1 Open and close messages
2 Messages for new, update, and delete
3 Messages for activate and deactivate
例如:
public class TestDelete { public static void main(String[] args) {
Db4o.configure().messageLevel(2);
ObjectContainer db = null;
try{
db = Db4o.openFile("customer.yap");
ObjectSet set=db.get(new Customer("customer12"));
Customer c1=(Customer)set.next();
db.delete(c1);
}finally{
if(db!=null){
db.close();
}
}
}
}
输出:
[db4o 6.4.54.11278 2009-02-18 22:57:04]
'customer.yap' opened O.K.
[db4o 6.4.54.11278 2009-02-18 22:57:04]
2817
delete
com.firstdb4o.entity.Customer
[db4o 6.4.54.11278 2009-02-18 22:57:04]
'customer.yap'
close
request
[db4o 6.4.54.11278 2009-02-18 22:57:04]
'customer.yap' closed 5.一个完整的CURD例子:package com.firstdb4o.test;
import java.io.File;
import com.db4o.Db4o;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.firstdb4o.entity.Address;
import com.firstdb4o.entity.Customer;
public class CompleteExample {
/**
* @param args
*/
public static void main(String[] args) {
new File("customer.yap").delete();
Db4o.configure().messageLevel(2);
ObjectContainer db=Db4o.openFile("customer.yap");
try{
//新建
db.set(new Customer("customer1","phone1"));
db.set(new Customer("customer2","phone2"));
ObjectSet result=(ObjectSet)db.get(new Customer());
listResult(result);
//更新
Customer c=new Customer();
c.setName("customer1");
ObjectSet r2=(ObjectSet)db.get(c);
Customer c1=(Customer)r2.next();
c1.setPhoneNumber("phone1modified");
r2.reset();
listResult(r2);
//删除
db.delete(c1);
ObjectSet r3=(ObjectSet)db.get(new Customer());
listResult(r3);
}finally{
if(db!=null){
db.close();
}
}
}
private static void listResult(ObjectSet result) {
while(result.hasNext()){
System.out.println(result.next());
}
System.out.println("-----------------");
}
}控制台输出:[db4o 6.4.54.11278 2009-02-18 23:16:25]
File
not
found
: 'customer.yap' Creating new
file
[db4o 6.4.54.11278 2009-02-18 23:16:26]
336 new com.db4o.ext.Db4oDatabase
[db4o 6.4.54.11278 2009-02-18 23:16:26]
'customer.yap' opened O.K.
[db4o 6.4.54.11278 2009-02-18 23:16:26]
173 new com.firstdb4o.entity.Customer
[db4o 6.4.54.11278 2009-02-18 23:16:26]
181 new com.firstdb4o.entity.Customer
[customer1;phone1]
[customer2;phone2]
-----------------
[customer1;phone1modified]
-----------------
[db4o 6.4.54.11278 2009-02-18 23:16:26]
173
delete
com.firstdb4o.entity.Customer
[customer2;phone2]
-----------------
[db4o 6.4.54.11278 2009-02-18 23:16:26]
'customer.yap'
close
request
[db4o 6.4.54.11278 2009-02-18 23:16:26]
'customer.yap' closed
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/93829/showart_2158285.html |
|