📄 phonerecordstore.java
字号:
/*
* Created on 2005-2-3
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package net.garrey.util;
import javax.microedition.rms.RecordComparator;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStoreException;
import net.garrey.model.PhoneItem;
import java.util.Vector;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class PhoneRecordStore {
private static final String RECORDSTORE_NAME="PHONE_DB";
private static RecordStore phone;
private UIController controller;
public PhoneRecordStore(UIController control){
controller=control;
}
//打开RecordStore,没有则创建
public void openPhone() {
try {
phone = RecordStore.openRecordStore(RECORDSTORE_NAME, true);
}catch (RecordStoreException ex) {
phone=null;
}
}
//关闭RecordStore
public void closePhone() {
if (phone!= null) {
try {
phone.closeRecordStore();
phone=null;
} catch (RecordStoreException ex) {}
}
}
//增加记录
public int addPhone(PhoneItem item) {
try {
this.openPhone();
byte[] data=item.getBytes();
int id=phone.getNextRecordID();
int rec =phone.addRecord(data,0,data.length);
this.closePhone();
return id;
} catch (RecordStoreException ex) { }
return -1;
}
//更新记录
public void updatePhone(PhoneItem item){
try {
this.openPhone();
byte[] data=item.getBytes();
phone.setRecord(item.getId(),data,0,data.length);
this.closePhone();
} catch (RecordStoreException ex) { }
}
//访问一条记录
public PhoneItem getPhone(int id){
PhoneItem item=null;
try {
this.openPhone();
new PhoneItem(id,phone.getRecord(id));
this.closePhone();
} catch (RecordStoreException ex) { }
return item;
}
//删除一条记录
public void deletePhone(int id){
try {
this.openPhone();
phone.deleteRecord(id);
this.closePhone();
} catch (RecordStoreException ex) {}
}
//访问所有记录
public Vector getPhones(){
Vector items=new Vector(10,3);
this.openPhone();//打开RecordStore
RecordEnumeration enum=null;
int ind=0;
try{
PhoneItem item=null;
enum=phone.enumerateRecords(null,new InnerComparator(),false);
while(enum.hasPreviousElement()){
ind=enum.previousRecordId();
item=new PhoneItem(ind,phone.getRecord(ind));
items.addElement(item);
}
}catch(Exception ex){ex.printStackTrace();}
finally{
try{
enum.destroy();
}catch(Exception e){}
this.closePhone();//关闭RecordStore
}//end finally
return items;
}
//一个简单的比较器
private class InnerComparator implements RecordComparator{
public int compare(byte[] rec1, byte[] rec2){
if(rec1.length>rec2.length)
return FOLLOWS;
else if(rec1.length<rec2.length)
return PRECEDES;
else
return EQUIVALENT;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -