Db4o数据库的连接和基本CURD操作

一、Db4o的数据库连接(ObjectContainer) 
      1、指定数据库存放的物理地址

   final static String DB4OFILENAME = System.getProperty("user.home") + "/formula1.db4o";
         2、 使用ObjectContainer获得数据库连接,并在存储完成后关闭数据库连接
      
	ObjectContainer container = Db4oEmbedded.openFile(DB4OFILENAME);
	try{//对数据库进行管理
	}finally{
		container.close();
	}

二、向Db4o数据库存储对象(store)

     /**
     * 面向对象数据的存储
     * @param obj类的实例
     */
    public void dataStore(Object obj){//初始化数据库连接并打开
        ObjectContainer db=Db4oEmbedded.openFile(Db4oEmbedded
                .newConfiguration(), DB4OFILENAME);
        try {
            db.store(obj);//存储语句就是这个关键字store
        }
        finally {
            db.close();//关闭数据库连接
        }
    }

三、从Db4o数据库中查询对象(NQ查询方式)

	ObjectContainer container = Db4oEmbedded.openFile("DB4OFILENAME.db4o");
	try {//Pilot的List
		List<Pilot> pilots = container.query(new Predicate<Pilot>() {
			public boolean match(Pilot o) {
				return o.getName().container("Joe");
			}//container模糊查询
		});//从List表中逐个找出Pilot对象
		for (Pilot pilot : pilots) {
			System.out.println(pilot.getName());
		}
	} finally {
		container.close();
	}
四、更新Db4o数据库中的对象(NQ查询方式,set方法,store方法)
	ObjectContainer container = Db4oEmbedded.openFile("DB4OFILENAME");
	try {//Pilot的List
		List<Pilot> pilots = container.query(new Predicate<Pilot>() {
			public boolean match(Pilot o) {//查询
				return o.getName().equals("Joe");
			}//equals方法是一个精确查询。
		});//取List中的第一条数据。
		Pilot aPilot = pilots.get(0);
		//通过对象的Set方法对“值”重置
		aPilot.setName("New Name");
		// store方法进行update
		container.store(aPilot);
	} finally {
		container.close();
	}
五、更新Db4o数据库中的对象(NQ查询方式,delete方法)
	ObjectContainer container = Db4oEmbedded.openFile("DB4OFILENAME");
	try {//Pilot的List
		List<Pilot> pilots = container.query(new Predicate<Pilot>() {
			public boolean match(Pilot o) {
				return o.getName().equals("Joe");
			}//equals精确查询
		});//取得List的第一条数据
		Pilot aPilot = pilots.get(0);
		//delete完成删除操作
		container.delete(aPilot);
	} finally {
		container.close();
	}

转载于:https://www.cnblogs.com/ponyblog/archive/2012/01/13/2321747.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值