⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 model.java

📁 fishgame手机游戏 运用工具netbean可以运行起来
💻 JAVA
字号:
package cn.zucc.mmf.harpoon.util.dbbase;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.util.*;import javax.microedition.rms.InvalidRecordIDException;import javax.microedition.rms.RecordComparator;import javax.microedition.rms.RecordEnumeration;import javax.microedition.rms.RecordFilter;import javax.microedition.rms.RecordStore;import javax.microedition.rms.RecordStoreException;import javax.microedition.rms.RecordStoreNotOpenException;public class Model {    //这里用Vector模拟持久性存储,下一章会介绍RMS    //private Vector entries = new Vector();    private RecordStore rs=null;    //添加联系人    public Model(){      openDatabase();    }    public void addEntry(Entry entry) {        try{        entry.setId(rs.getNextRecordID());        ByteArrayOutputStream baos=new ByteArrayOutputStream();        DataOutputStream dos=new DataOutputStream(baos);        entry.serialize(dos);        byte[] data=baos.toByteArray();        int id=rs.addRecord(data,0, data.length);        System.out.println("-------------"+id+"---nextID"+rs.getNextRecordID());        dos.close();        }catch(RecordStoreException e){        }catch(IOException ex){        }    }    //读取联系人信息    public Entry getEntry(int recordID) {        //return (Entry)entries.elementAt(index);        try{            if(rs.getNumRecords()>0){               byte[] data=rs.getRecord(recordID);               ByteArrayInputStream bais=new ByteArrayInputStream(data);               DataInputStream dis=new DataInputStream(bais);               Entry entry=Entry.deserialize(dis);               //entry.setId(rs.getNextRecordID()-1);               dis.close();                              return entry;            }        }catch(RecordStoreException e){                    }catch(IOException ex){                    }        return null;    }    //更新联系人信息    public void updateEntry(Entry entry) {        /*for(int i=0; i<entries.size(); i++) {            Entry e = (Entry)entries.elementAt(i);            if(e.getId()==entry.getId()) {                e.setName(entry.getName());                e.setMobile(entry.getMobile());                e.setPhone(entry.getPhone());                e.setEmail(entry.getEmail());                e.setImage(entry.getImage());                e.setGroup(entry.getGroup());                String[] gr=entry.getGroup();            }        }*/         try{        ByteArrayOutputStream baos=new ByteArrayOutputStream();        DataOutputStream dos=new DataOutputStream(baos);        entry.serialize(dos);        byte[] data=baos.toByteArray();        rs.setRecord(entry.getId(), data, 0, data.length);         }catch(RecordStoreException e){        }catch(IOException ex){        }    }    //读取所有联系人    public Entry[] getAll() {        try{        RecordEnumeration re = rs.enumerateRecords(null, null, false);        if(re.numRecords() <= 0){                Entry[] ret = new Entry[0];                return ret;         }else{        Entry[] ret = new Entry[re.numRecords()];        int i=0;        while(re.hasNextElement()){            byte[] data=re.nextRecord();            ByteArrayInputStream bais=new ByteArrayInputStream(data);            DataInputStream dis=new DataInputStream(bais);            Entry entry=Entry.deserialize(dis);            //entry.setId(re.nextRecordId());为什么会报错误!            //System.out.println(re.nextRecordId());            dis.close();            ret[i++]=entry;        }        return ret;        }        } catch (IOException ex) {            ex.printStackTrace();        }catch(RecordStoreException e){            e.printStackTrace();        }catch(NullPointerException e){            e.printStackTrace();        }        return null;    }    public int size() {        try {            return rs.getNumRecords();        } catch (RecordStoreNotOpenException ex) {            ex.printStackTrace();        }        return -1;    }    //删除联系人    public void removeEntry(Entry entry) {        try {                       rs.deleteRecord(entry.getId());        } catch (RecordStoreNotOpenException ex) {            ex.printStackTrace();        } catch (InvalidRecordIDException ex) {            ex.printStackTrace();        } catch (RecordStoreException ex) {            ex.printStackTrace();        }    }    private void openDatabase() {        try {            //RecordStore.deleteRecordStore("phonebook");            rs = RecordStore.openRecordStore("harpoon", true);        } catch (RecordStoreException ex) {            ex.printStackTrace();        }     }        //内部类的方式实现SearchFilter,通过比较记录的开始字母决定返回的子集    private class SearchFilter implements RecordFilter{        private String query = null;        public SearchFilter(String query){            this.query = query;        }                public boolean matches(byte[] data){            try {                ByteArrayInputStream bais = new ByteArrayInputStream(data);                DataInputStream dis = new DataInputStream(bais);                boolean flag=Entry.deserialize(dis).getName().startsWith(query);                dis.close();                return flag;            } catch (IOException ex) {                ex.printStackTrace();            }            return false;        }    }    //内部类的方式实现SearchComparator,决定返回子集的排列顺序    private class SearchComparator implements RecordComparator{                public int compare(byte[] data1,byte[] data2){            int i = new String(data1).compareTo(new String(data2));            if(i < 0){                return RecordComparator.FOLLOWS;            }else if(i == 0){                return RecordComparator.EQUIVALENT;            }else if(i > 0){                return RecordComparator.PRECEDES;            }            return i;        }       }    //使用RecordEnumeration来访问记录。    public Entry[] getResult(String query){        try{            //这里返回的记录是符合SearchComparator的排列顺序并经过SearchFilter筛选的记录            RecordEnumeration re = rs.enumerateRecords(new SearchFilter(query), null, false);            if(re.numRecords() <= 0){                Entry[] result = new Entry[0];                return result;            }else{                Entry[] result = new Entry[re.numRecords()];                int i = 0;                //循环记录集合放入result数组中                while(re.hasNextElement()){                    byte[] data=re.nextRecord();                    ByteArrayInputStream bais=new ByteArrayInputStream(data);                    DataInputStream dis=new DataInputStream(bais);                    Entry entry=Entry.deserialize(dis);                    dis.close();                    result[i++]=entry;                }                return result;            }                        } catch (IOException ex) {            ex.printStackTrace();            System.out.println("------"+ex.getMessage());        }catch(RecordStoreException e){            e.printStackTrace();            System.out.println("------"+e.getMessage());        }catch(NullPointerException e){            e.printStackTrace();            System.out.println("------"+e.getMessage());        }        return null;    }  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -