⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 database.java

📁 开发框架。 一.说明: 此框架的意图是解决手机软件开发中常遇到
💻 JAVA
字号:
package org.gggeye.easymf.rms;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;
import javax.microedition.rms.RecordStoreNotOpenException;

import org.gggeye.easymf.log.Logger;

/**
 * 一个简单的存储数据库
 * 
 * 首先要考虑下我们手机的存储策略
 * 1. 手机存储容量小
 * 2. 手机存储API简陋
 * 3. 手机需要持久的数据少
 * 
 * 从上面几点可以得出结论我们的存储策略就是简单,方便,快捷
 * 因此,用key-value的存储策略就OK了
 * 其中key -- String,Value -- byte[]
 * 
 * Demo. 下面是增加一条指定关键字的例子
 * 
 * DataBase db = DataBase.openDataBase("test");
 * db.add("my", "www.3geye.net".toBytes());
 * db.close();
 * 
 * 
 * @author wuhua
 * <a href="http://wuhua.3geye.net">我的博客</a>
 *
 */
public class DataBase {
	
	/**
	 * 用来存储数据的对象
	 */
	RecordStore recordStore;
	
	/**
	 * 构造函数
	 * @param _dataName
	 * @throws RecordStoreFullException
	 * @throws RecordStoreNotFoundException
	 * @throws RecordStoreException
	 */
	DataBase(String _dataName) throws RecordStoreFullException, RecordStoreNotFoundException, RecordStoreException{
		recordStore = RecordStore.openRecordStore(_dataName, true);
	}
	
	/**
	 * 打开数据库
	 * @param _dataName
	 * @return
	 */
	public final  static  DataBase openDataBase(String _dataName){		
		try {
			return new DataBase(_dataName);
		} catch (RecordStoreFullException e) {
			Logger.info(e);
		} catch (RecordStoreNotFoundException e) {
			Logger.info(e);
		} catch (RecordStoreException e) {
			Logger.info(e);
		}
		return null;
	}
	/**
	 * 添加一条key-value记录
	 * @param _key
	 * @param _value
	 * @throws RecordStoreNotOpenException
	 * @throws RecordStoreFullException
	 * @throws RecordStoreException
	 */
	public final void add(String _key, byte[] _value) throws RecordStoreNotOpenException, RecordStoreFullException, RecordStoreException{
		int tIndex = findIdByKey(_key);
//		if(index != -1 )
//			throw new java.lang.IllegalArgumentException(_key + " Record exists!");
		byte[] tValue = getData(_key, _value);
		if(tIndex == -1){
			this.recordStore.addRecord(tValue, 0, tValue.length);
		}else{
			this.recordStore.setRecord(tIndex, tValue, 0, tValue.length);
		}
	}
	
	/**
	 * 修改一天指定key的记录
	 * @param _key
	 * @param _value
	 * @throws RecordStoreNotOpenException
	 * @throws InvalidRecordIDException
	 * @throws RecordStoreFullException
	 * @throws RecordStoreException
	 */
	public final void update(String _key, byte[] _value) throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreFullException, RecordStoreException{
		int index = findIdByKey(_key);
		if(index == -1 )
			throw new java.lang.IllegalArgumentException(_key + " Record not exists!");
		byte[] tValue = getData(_key, _value);
		 
		this.recordStore.setRecord(index, tValue, 0, tValue.length);
	}
	
	/**
	 * 删除记录
	 * @param _key
	 * @throws RecordStoreNotOpenException
	 * @throws InvalidRecordIDException
	 * @throws RecordStoreException
	 */
	public final void delete(String _key) throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException{
		int index = findIdByKey(_key);
		if(index == -1 )
			throw new java.lang.IllegalArgumentException(_key + " Record not exists!");
		this.recordStore.deleteRecord(index);
	}
	
	/**
	 * 删除整个表
	 * @throws RecordStoreNotFoundException
	 * @throws RecordStoreNotOpenException
	 * @throws RecordStoreException
	 */
	public final void deleteAll() throws RecordStoreNotFoundException, RecordStoreNotOpenException, RecordStoreException{
		RecordStore.deleteRecordStore(this.recordStore.getName());
	}
	
	public final void close(){
		try {
			this.recordStore.closeRecordStore();
		} catch(Exception e){
			Logger.info(e);
		}
	}
	
	/**
	 * 整合数据
	 * 整合Key(String) - Value(byte[])
	 * @param _key
	 * @param _value
	 * @return
	 */
	private final byte[] getData(String _key, byte[] _value){
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        try {
			dos.writeUTF(_key);
			dos.write(_value);
			dos.flush();
			return baos.toByteArray();
 
		} catch (IOException e) {
			Logger.info(e);
		}finally{
			try {
				baos.close();
				dos.close();
			} catch (IOException e) {
				Logger.info(e);
			}	
		}
		return null;
       
	}
	
	private final int findIdByKey(String _key) {
		int index = -1;
		RecordEnumeration re;
		try {
			re = this.recordStore.enumerateRecords(null, null, false);
			int num = re.numRecords();

			for (int i = 1; i <= num; i++) {
				int id = re.nextRecordId();
				byte[] body = this.recordStore.getRecord(id);
				ByteArrayInputStream bais = new ByteArrayInputStream(body);
				DataInputStream dis = new DataInputStream(bais);
				String key = dis.readUTF();
				if(key != null && key.equals(_key)){
					index = id;
					break;
				}
			}

		} catch (Exception e) {
			Logger.info(e);
		}
		return index;	
	}
	
	public final   RecordEnumeration list() throws RecordStoreNotOpenException{		  
		return  recordStore.enumerateRecords(null, null, false);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -