免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3151 | 回复: 0
打印 上一主题 下一主题

DB4O学习(二)查找、更新、删除 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-01-23 10:21 |只看该作者 |倒序浏览

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
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP