PhoneRecordStore.java
上传用户:heyongping
上传日期:2022-07-01
资源大小:95k
文件大小:5k
源码类别:

J2ME

开发平台:

Java

  1. /*
  2.  * 创建于 2009-12-04
  3.  * 实现手机虚拟电话簿,添加记录,编辑记录,删除记录,查询记录等
  4.  * 
  5.  * 更改注释格式可以:Window - Preferences - Java - Code Style - Code Templates
  6.  */
  7. package net.garrey.util;
  8. import javax.microedition.rms.RecordComparator;
  9. import javax.microedition.rms.RecordFilter;
  10. import javax.microedition.rms.RecordStore;
  11. import javax.microedition.rms.RecordEnumeration;
  12. import javax.microedition.rms.RecordStoreException;
  13. import net.garrey.model.PhoneItem;
  14. import net.garrey.view.PhoneFind;
  15. import java.util.Vector;
  16. /**
  17.  * 作者: 张三
  18.  * 实现对RecordStore进行操作
  19.  */
  20. public class PhoneRecordStore {
  21. private static final String RECORDSTORE_NAME="PHONE_DB";
  22. private static RecordStore phone;
  23. private  UIController controller;
  24. /*
  25.  * 含参数的构造方法
  26.  */
  27. public PhoneRecordStore(UIController control){
  28. controller=control;
  29. }
  30. //打开RecordStore,没有则创建
  31.     public void openPhone() {
  32.         try {
  33.             phone = RecordStore.openRecordStore(RECORDSTORE_NAME, true);
  34.         }catch (RecordStoreException ex) {            
  35.             phone=null;
  36.         }
  37.     }
  38.     //关闭RecordStore
  39.     public void closePhone() {
  40.         if (phone!= null) {
  41.             try {
  42.                 phone.closeRecordStore();
  43.                 phone=null;
  44.             } catch (RecordStoreException ex) {}
  45.         }
  46.     }
  47.     //增加记录
  48.     public int addPhone(PhoneItem item) {
  49.                 try {
  50.                     this.openPhone();
  51.                     byte[] data=item.getBytes();
  52.                     int id=phone.getNextRecordID();
  53.                  int rec =phone.addRecord(data,0,data.length);
  54.                  this.closePhone();
  55.                  return id;
  56.                 } catch (RecordStoreException ex) { }
  57.         
  58.         return -1;
  59.     }
  60.     //更新记录
  61.     public void updatePhone(PhoneItem item){
  62.             try {
  63.                 this.openPhone();       
  64.                 byte[] data=item.getBytes();
  65.              phone.setRecord(item.getId(),data,0,data.length);
  66.              this.closePhone();
  67.             } catch (RecordStoreException ex) { }
  68.     }
  69.     //访问一条记录
  70.     public PhoneItem getPhone(int id){
  71.         PhoneItem item=null;
  72.             try {
  73.                 this.openPhone();
  74.              new PhoneItem(id,phone.getRecord(id));
  75.              this.closePhone();
  76.             } catch (RecordStoreException ex) { }
  77.             System.out.println("aaaaaaaaaaaaaaaa"+item);
  78.          return item;
  79.     }
  80.     //删除一条记录
  81.     public void deletePhone(int id){
  82.             try {
  83.                 this.openPhone();
  84.              phone.deleteRecord(id);
  85.              this.closePhone();
  86.             } catch (RecordStoreException ex) {}
  87.     }
  88.     //访问所有记录
  89.     public Vector getPhones(){
  90.         Vector items=new Vector(10,3);
  91.         this.openPhone();//打开RecordStore
  92.         RecordEnumeration enum1=null;
  93.         int ind=0;
  94.         try{
  95.             PhoneItem item=null;
  96.          enum1=phone.enumerateRecords(null,new InnerComparator(),false);
  97.             while(enum1.hasPreviousElement()){
  98.              ind=enum1.previousRecordId();
  99.              item=new PhoneItem(ind,phone.getRecord(ind));
  100.              items.addElement(item);
  101.             }
  102.             
  103.         }catch(Exception ex){ex.printStackTrace();}
  104.         finally{
  105.          try{
  106.          enum1.destroy();
  107.          }catch(Exception e){}
  108.         
  109.          this.closePhone();//关闭RecordStore
  110.         }//end finally
  111.     
  112.         return items;
  113.     }
  114.     
  115.     //按条件访问记录
  116.     public Vector getContinutePhones(){
  117.         Vector items=new Vector(10,3);
  118.         this.openPhone();//打开RecordStore
  119.         RecordEnumeration enum1=null;
  120.         int ind=0;
  121.         try{
  122.          System.out.println("我开始搜索了");
  123.             PhoneItem item=null;
  124.          enum1=phone.enumerateRecords(new InnerFilter(),null,false);
  125.             while(enum1.hasNextElement()){
  126.              ind=enum1.nextRecordId();
  127.              item=new PhoneItem(ind,phone.getRecord(ind));
  128.              items.addElement(item);
  129.             }
  130.             
  131.         }catch(Exception ex){ex.printStackTrace();}
  132.         finally{
  133.          try{
  134.          enum1.destroy();
  135.          }catch(Exception e){}
  136.         
  137.          this.closePhone();//关闭RecordStore
  138.         }//end finally
  139.     
  140.         return items;
  141.     }
  142.     //一个简单的比较器
  143.     private class InnerComparator implements RecordComparator{
  144.         public int compare(byte[] rec1, byte[] rec2){
  145.             if(rec1.length>rec2.length)
  146.                 return FOLLOWS;
  147.             else if(rec1.length<rec2.length)
  148.                 return PRECEDES;
  149.             else 
  150.                 return EQUIVALENT;
  151.         }
  152.     }
  153.     //一个简单的过滤器
  154.     private class InnerFilter implements RecordFilter{
  155.      public boolean matches(byte[] candidate){
  156.      System.out.println("我开始搜索了1"+PhoneFind.getKeyValue());
  157.      String  str = new String(candidate);
  158.      System.out.println(str.substring(0));
  159.      if(str.startsWith(PhoneFind.getKeyValue())){
  160.      System.out.println("好像搜索到!");
  161.      return true;
  162.      }
  163.      System.out.println("好像没有搜索到呀");
  164.      return false;
  165.      }
  166.     }
  167. }