RecordStore的三种操作用法

来源:互联网 发布:matlab求数组长度 编辑:程序博客网 时间:2024/05/18 02:50
//删除地址记录
 public  void deleData(String name){
  try{
   RecordStore rs=RecordStore.openRecordStore(dbname, false);
   RecordEnumeration re=rs.enumerateRecords(null, null, false);
   while(re.hasNextElement()){
    byte tmp[]=re.nextRecord();
    bis=new ByteArrayInputStream(tmp);
    dis=new DataInputStream(bis);
    String rName=dis.readUTF();
    String rTel=dis.readUTF();
    if(rName.equals(name)){
     //根据记录ID删除
     rs.deleteRecord(dis.readInt());
     //显示所有记录
     listAllForm();
    }
   }
   re.destroy();
   rs.closeRecordStore();
  }catch(Exception e){
   System.out.println("Delete Error"+e.getMessage());
  }
  
 }

//向记录存储集中添加记录
 public void addData(){
  String name = txtName.getString();
  String tel= txtTel.getString();
  try{
   RecordStore rs= RecordStore.openRecordStore(dbname, false);
   bos =new ByteArrayOutputStream();
   dos=new DataOutputStream(bos);
   dos.writeUTF(name);
   dos.writeUTF(tel);
   dos.writeInt(rs.getNextRecordID());
   dos.flush();
   byte tmp[]=bos.toByteArray();
   rs.addRecord(tmp, 0,tmp.length);
   rs.closeRecordStore();
   
  }catch(Exception e){
   System.out.println("Add data error:"+e.getMessage());
  }
 }
}
 
// 显示地址记录的详细信息
 public void searchData(String name) {
  Form f = new Form("详细信息");
  Command back = new Command("返回", Command.SCREEN, 1);
  f.addCommand(back);
  f.setCommandListener(this);
  try {
   RecordStore rs = RecordStore.openRecordStore(dbname, false);
   RecordEnumeration re = rs.enumerateRecords(null, null, false);
   while (re.hasNextElement()) {
    byte tmp[] = re.nextRecord();
    bis = new ByteArrayInputStream(tmp);
    dis = new DataInputStream(bis);
    String rName = dis.readUTF();
    String rTel = dis.readUTF();
    if (rName.equals(name)) {
     f.append("Name:/n");
     f.append(rName + "/n");
     f.append("Tel:/n");
     f.append(rTel + "/n");
     display.setCurrent(f);
     return;
    }
   }
   rs.closeRecordStore();
  } catch (Exception e) {
   System.out.println();
  }
 }