?? database.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 + -