📄 recordengine.java
字号:
package com.vp.echeckregister.utils;
import java.util.Vector;
import javax.microedition.rms.RecordComparator;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordFilter;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
public final class RecordEngine {
private static final int MIN_SIZE = 500;
private RecordEngine() {
}
public static RecordStore createRecordStore(String tableName) {
RecordStore rs = null;
try {
rs = RecordStore.openRecordStore(tableName, true);
} catch (RecordStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
public static synchronized int addRecord(String tableName, byte[] data) {
int recordId = -1;
RecordStore rs = null;
try {
rs = RecordStore.openRecordStore(tableName, true);
int availableSize = rs.getSizeAvailable();
if (availableSize - data.length > MIN_SIZE) {
recordId = rs.addRecord(data, 0, data.length);
} else {
throw new RecordStoreFullException(
"RecordStore available size is smaller than MIN_SIZE");
}
} catch (RecordStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
closeRecordStore(rs);
}
return recordId;
}
public static synchronized void setRecord(String tableName, byte[] data, int recordId) {
RecordStore rs = null;
try {
rs = RecordStore.openRecordStore(tableName, true);
byte[] temp = rs.getRecord(recordId);
int availableSize = rs.getSizeAvailable();
if (availableSize - (data.length - temp.length) > MIN_SIZE) {
rs.setRecord(recordId, data, 0, data.length);
} else {
throw new RecordStoreFullException(
"RecordStore available size is smaller than MIN_SIZE");
}
} catch (RecordStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
closeRecordStore(rs);
}
}
public static synchronized byte[] getRecord(String tableName, int recordId) {
byte[] data = null;
RecordStore rs = null;
try {
rs = RecordStore.openRecordStore(tableName, true);
data = rs.getRecord(recordId);
} catch (RecordStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
closeRecordStore(rs);
}
return data;
}
public static synchronized boolean deleteRecord(String tableName, int recordId) {
boolean boo = false;
RecordStore rs = null;
try {
rs = RecordStore.openRecordStore(tableName, true);
rs.deleteRecord(recordId);
boo = true;
} catch (RecordStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
closeRecordStore(rs);
}
return boo;
}
public static synchronized int[] getRecordIds(String tableName, RecordFilter filter,
RecordComparator comparator) {
RecordStore rs = null;
RecordEnumeration re = null;
int[] ids = null;
try {
rs = RecordStore.openRecordStore(tableName, true);
re = rs.enumerateRecords(filter, comparator, false);
int i = 0;
while (re.hasNextElement()) {
ids[i] = re.nextRecordId();
i++;
}
} catch (RecordStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
closeRecordStore(rs);
}
return ids;
}
public static synchronized Vector getRecords(String tableName, RecordFilter filter,
RecordComparator comparator) {
Vector recordVec = new Vector();
int[] recordIds = getRecordIds(tableName, filter, comparator);
if (null != recordIds) {
for (int i = 0; i < recordIds.length; i++) {
recordVec.addElement(getRecord(tableName, recordIds[i]));
}
}
return recordVec;
}
private static void closeRecordStore(RecordStore rs) {
if (null != rs) {
try {
rs.closeRecordStore();
} catch (RecordStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
rs = null;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -