DB4O操作的简单示例

package com.test;

import java.io.File;
import java.util.List;

import com.db4o.Db4o;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.db4o.config.Configuration;
import com.db4o.io.CachedIoAdapter;
import com.db4o.io.RandomAccessFileAdapter;
import com.db4o.query.Constraint;
import com.db4o.query.Query;



public class Test {

/**
* @param args
*/
private static MessageDao messageDao = null;
    private static Configuration configuration = null;
    private static ObjectContainer db = null;
    private static String path= "./dat/JCNEP3161_chatHistory.db";
   
public Test()
{
configuration = Db4o.newConfiguration();
//      configuration.io(new RandomAccessFileAdapter());
configuration.io(new CachedIoAdapter(new RandomAccessFileAdapter()));
configuration.optimizeNativeQueries(true);
     
configuration.objectClass(MyMessage.class).objectField("from").indexed(true);
configuration.objectClass(MyMessage.class).objectField("to").indexed(true);
configuration.objectClass(MyMessage.class).objectField("body").indexed(true);

configuration.objectClass(MyMessage.class).objectField("date").indexed(true);
configuration.objectClass(MyMessage.class).objectField("date_time").indexed(true);
}
   
public static void main(String[] args) {



/* Test test = new Test();
test.openDB4O(path);
for(int i=0;i<30;i++)
{
MyMessage mm = new MyMessage();
mm.setBody("你好"+i);
mm.setDate("2010-07-"+i);
mm.setFrom("JCNEP4095");
mm.setFromName("张三");
mm.setTo("JCNEP3160");
mm.setToName("李四");
db.store(mm);
}

db.close();*/



Test test = new Test();
test.openDB4O(path);
/*
* 查询所有数据
* List<MyMessage> list = test.getAllMessage();
for(MyMessage mm : list)
{
System.out.println(mm.getFrom()+":"+mm.getTo()+":内容:"+mm.getBody()+":日期:"+mm.getDate());
}*/


/*
* 根据ID查询
* List<MyMessage> list = test.getMessageByID("JCNEP3160");
for(MyMessage mm : list)
{
System.out.println(mm.getFrom()+":"+mm.getTo()+":内容:"+mm.getBody()+":日期:"+mm.getDate());
}
*/

/*
* 根据BODY来查询
* List<MyMessage> list = test.getMessageByBody("我爱你2");
for(MyMessage mm : list)
{
System.out.println(mm.getFrom()+":"+mm.getTo()+":内容:"+mm.getBody()+":日期:"+mm.getDate());
}*/

List<MyMessage> list = test.getMessageByDate("2010-07-10","2010-07-20");
for(MyMessage mm : list)
{
System.out.println(mm.getFrom()+":"+mm.getTo()+":内容:"+mm.getBody()+":日期:"+mm.getDate());
}
}

//根据时间段查询
public List<MyMessage> getMessageByDate(String begin,String end)
{
List<MyMessage> mymessage=null;
Query query = this.db.query();
query.constrain(MyMessage.class);
//表示date字段大于等于开始时间,小于等于结束时间
query.descend("date").constrain(begin).greater().equal();
query.descend("date").constrain(end).smaller().equal();

ObjectSet objset = query.execute();
mymessage = objset;

return mymessage;

}

//根据body编码查询
public List<MyMessage> getMessageByBody(String body)
{
List<MyMessage> mymessage=null;
Query query = this.db.query();
query.constrain(MyMessage.class);

Constraint ct = query.descend("body").constrain(body);

ObjectSet objset = query.execute();
mymessage = objset;

return mymessage;

}

//根据ID编码查询
public List<MyMessage> getMessageByID(String To)
{
List<MyMessage> mymessage=null;
Query query = this.db.query();
query.constrain(MyMessage.class);
//表示DB文件中TO字段的值为To的数据 Constraint ct = query.descend("to").constrain(To);

ObjectSet objset = query.execute();
mymessage = objset;

return mymessage;

}
//查询所有DB中的数据
public List<MyMessage> getAllMessage()
{
List<MyMessage> mymessage=null;

Query query = this.db.query();
query.constrain(MyMessage.class);

ObjectSet objset = query.execute();
mymessage = objset;

return mymessage;
}

public static void openDB4O(String path) {
        File tempFile = new File(path);
        String tempStr = tempFile.getAbsolutePath();
        int index = tempStr.lastIndexOf(File.separatorChar);
        if (index > 0) {
            String path2 = tempStr.substring(0, tempStr.lastIndexOf(File.separatorChar));
            File file = new File(path2);
            if (!file.exists()) {
                file.mkdirs();
            }
        }
        db = Db4o.openFile(configuration, path);
    }

}

你可能感兴趣的:(DB)