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