关于db4o 的工具类

 
 
关于对db40面向对象的数据库的操作工具类使用 
 
 
 
 
package com.example.test;

import java.io.File;

import android.content.Context;
import android.os.Environment;

import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;

public class DB4OUtils {
	//db4o数据库文件的位置 
	public static final String DB_PATH = "/SunLifeEverBright/Banca/advert.db4o";
	public static ObjectContainer instance = null;
	
	private DB4OUtils(){
		
	}
	public static ObjectContainer initDB4O() {
		ObjectContainer db = null;
		if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
			try {
				String dbPath = Environment.getExternalStorageDirectory().toString() + DB_PATH;
				File file = new File(dbPath);
				if (!file.exists())
					file.createNewFile();
				db = Db4oEmbedded.openFile(Db4oEmbedded.newConfiguration(), dbPath);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return db;
	}
	
	/**
	 * 得到ObjectContainer对象    单例来实现
	 * @return
	 */
	public synchronized static ObjectContainer getInstance(){
		if (instance==null) {
			instance = initDB4O();
		}
		return instance;
	}
	
	/**
	 * 关闭db链接
	 */
	public static void close(){
		if (instance!=null) {
			instance.close();
		}
	}
}

你可能感兴趣的:(android,工具类,Uti,db40)