免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2305 | 回复: 0

DB4O学习(一) [复制链接]

论坛徽章:
0
发表于 2010-01-23 10:20 |显示全部楼层

1.DB4O版本:6.4,ObjectManager版本7.2
  因为之前创建数据库的时候同时进行检索,可以是这个原因经常导致database locked异常,
以为是DB4O版本的问题,之前是7.4,ObjectManager打不开yap文件。
  现在这个版本可以了。估计是跟Server2003的文件的只读性有关。我后来该了文件只读属性,
现在可以了。估计跟DB4O版无关。

2.创建数据库,保存对象
   创建java project,导入db4o-6.4.54.11278-java5.jar到项目build path。

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 Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
        
        ObjectContainer db = null;
        try{
        
            db=Db4o.openFile("customer.yap");
            Address addr=new Address("address2");
         Customer cs=new Customer("persia2",addr);
            db.set(cs);
        }finally{
            
            if(db!=null){
                db.close();
            }
        }
    }
}
ObjectContainer db=Db4o.openFile("customer.yap"); 创建该文件,若文件已经存在,则再次打开。也可以指定路径:ObjectContainer db = Db4o.openFile("C:/myDb.yap"); 3.查询:
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 Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
        
        ObjectContainer db = null;
        try{
        //    File file=new File("customer.yap");
        //    String fullPath=file.getAbsolutePath();
        //    System.out.println("path:"+fullPath);
            db = Db4o.openFile("customer.yap");
            
            Customer example=new Customer("persia2");
            ObjectSet set=db.get(example);
            Customer cust=(Customer)set.next();
            System.out.println("query:"+cust.getAddress().getStreet());
        //    db.set(cs);
        }finally{
            
            if(db!=null){
                db.close();
            }
        }
    }
}
查询结果:
query:address2


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/93829/showart_2158284.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP