PhoneRecordStore.java
上传用户:heyongping
上传日期:2022-07-01
资源大小:95k
文件大小:5k
- /*
- * 创建于 2009-12-04
- * 实现手机虚拟电话簿,添加记录,编辑记录,删除记录,查询记录等
- *
- * 更改注释格式可以:Window - Preferences - Java - Code Style - Code Templates
- */
- package net.garrey.util;
- import javax.microedition.rms.RecordComparator;
- import javax.microedition.rms.RecordFilter;
- import javax.microedition.rms.RecordStore;
- import javax.microedition.rms.RecordEnumeration;
- import javax.microedition.rms.RecordStoreException;
- import net.garrey.model.PhoneItem;
- import net.garrey.view.PhoneFind;
- import java.util.Vector;
- /**
- * 作者: 张三
- * 实现对RecordStore进行操作
- */
- 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) { }
- System.out.println("aaaaaaaaaaaaaaaa"+item);
- 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 enum1=null;
- int ind=0;
- try{
- PhoneItem item=null;
- enum1=phone.enumerateRecords(null,new InnerComparator(),false);
- while(enum1.hasPreviousElement()){
- ind=enum1.previousRecordId();
- item=new PhoneItem(ind,phone.getRecord(ind));
- items.addElement(item);
- }
-
- }catch(Exception ex){ex.printStackTrace();}
- finally{
- try{
- enum1.destroy();
- }catch(Exception e){}
-
- this.closePhone();//关闭RecordStore
- }//end finally
-
- return items;
- }
-
- //按条件访问记录
- public Vector getContinutePhones(){
- Vector items=new Vector(10,3);
- this.openPhone();//打开RecordStore
- RecordEnumeration enum1=null;
- int ind=0;
- try{
- System.out.println("我开始搜索了");
- PhoneItem item=null;
- enum1=phone.enumerateRecords(new InnerFilter(),null,false);
- while(enum1.hasNextElement()){
- ind=enum1.nextRecordId();
- item=new PhoneItem(ind,phone.getRecord(ind));
- items.addElement(item);
- }
-
- }catch(Exception ex){ex.printStackTrace();}
- finally{
- try{
- enum1.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;
- }
- }
- //一个简单的过滤器
- private class InnerFilter implements RecordFilter{
- public boolean matches(byte[] candidate){
- System.out.println("我开始搜索了1"+PhoneFind.getKeyValue());
- String str = new String(candidate);
- System.out.println(str.substring(0));
- if(str.startsWith(PhoneFind.getKeyValue())){
- System.out.println("好像搜索到!");
- return true;
- }
- System.out.println("好像没有搜索到呀");
- return false;
- }
- }
- }