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

📄 reportdb.java

📁 我参与的一个J2ME项目的部分代码
💻 JAVA
字号:
package com.sepco.apps.trouble.database;import java.util.*;import java.io.*;import javax.microedition.rms.*;import com.sepco.apps.trouble.businessobject.*;public class reportDb{    RecordStore recordStore = null;    RecordEnumeration enum = null;    String recordStoreName = null;    public reportDb() {    }    public reportDb(String storeName) {        //构造器:打开或者创建一个RMS数据库999        try {            recordStoreName = storeName;            recordStore = RecordStore.openRecordStore(storeName,true);        } catch(RecordStoreException rse) {            rse.printStackTrace();        }    }    public void close() throws RecordStoreNotOpenException, RecordStoreException {        //关闭或者删除数据存储        if (recordStore.getNumRecords() == 0) {            String storeName = recordStore.getName();            recordStore.closeRecordStore();            recordStore.deleteRecordStore(recordStoreName);        } else {            recordStore.closeRecordStore();        }        if (enum != null) {            enum.destroy();        }    }  //根据任务报告ID得到一条任务记录  public synchronized reportRecord getreport(String JLID) {        String record;        MyFilter filter = new MyFilter();        filter.setFilterCondition(JLID, 1, MyFilter.FILTER_EXACT);        try {            enum = recordStore.enumerateRecords(filter, null, false);            if (enum.hasNextElement()) {                ByteArrayInputStream bin = new ByteArrayInputStream(enum.nextRecord());                DataInputStream      din = new DataInputStream(bin);                record = din.readUTF();                           if (record.length() != 0) {                    return (parseRecIntoBizObj(record));                } else {                    return (null);                }            } else {                return (null);            }        } catch (Exception e) {            return (null);        }    }   //设置一条任务报告记录      public synchronized void setreport(reportRecord theRecord) {        try {            int recId = getRecordIdOfTicket(theRecord.getJLID());            ByteArrayOutputStream bout = new ByteArrayOutputStream();            DataOutputStream dout = new DataOutputStream(bout);            dout.writeUTF(theRecord.toDelimitedString());            byte[] b = bout.toByteArray();            if (recId != -1) { //do an Update                recordStore.setRecord(recId, b, 0, b.length);            } else { //do an Insert                recordStore.addRecord(b, 0, b.length);            }        } catch (Exception e) {        }    }   public synchronized boolean deletereport(reportRecord theRecord) {        try {                        int recId = getRecordIdOfTicket(theRecord.getJLID());            if (recId != -1) {                recordStore.deleteRecord(recId);                return (true);            } else {                return (false);            }        } catch (Exception e) {            return (false);        }    }    public synchronized boolean deletereport(String JLID) {        try {            int recId = getRecordIdOfTicket(JLID);            if (recId != -1) {                recordStore.deleteRecord(recId);                return (true);            } else {                return (false);            }        } catch (Exception e) {            return (false);        }    }     public void refreshRMSWithVector(Vector in) {        try {            recordStore.closeRecordStore();            RecordStore.deleteRecordStore(recordStoreName);            recordStore = RecordStore.openRecordStore(recordStoreName, true);            Enumeration enum = in.elements();            while (enum.hasMoreElements()) {                reportRecord reportRec = (reportRecord)enum.nextElement();                ByteArrayOutputStream bout = new ByteArrayOutputStream();                DataOutputStream dout = new DataOutputStream(bout);                dout.writeUTF(reportRec.toDelimitedString());                byte[] b = bout.toByteArray();                recordStore.addRecord(b, 0, b.length);            }        } catch (RecordStoreException ex1) {        } catch (IOException ex2) {        }    }    public synchronized Vector rmsToVectorOfBizObj() {        String record;        Vector returnVector = new Vector();        MyComparator comparator = new MyComparator();        comparator.setSortColumn(1);               try {                     enum = recordStore.enumerateRecords(null, comparator, false);            while (enum.hasNextElement()) {                              ByteArrayInputStream bin = new ByteArrayInputStream(enum.nextRecord());                DataInputStream      din = new DataInputStream(bin);                record = din.readUTF();                if (record.length() != 0) {                                    returnVector.addElement((parseRecIntoBizObj(record)));                }            }            if (returnVector.isEmpty()) {                return (null);            } else {                return (returnVector);            }        } catch (Exception e) {            return (null);        }    }    private int getRecordIdOfTicket(String JLID) {        MyFilter filter = new MyFilter();        filter.setFilterCondition(JLID, 1, MyFilter.FILTER_EXACT);        try {            enum = recordStore.enumerateRecords(filter, null, false);            if (enum.hasNextElement()) {                return (enum.nextRecordId());            } else {                return (-1);            }        } catch (Exception e) {            return (-1);        }    }    private reportRecord parseRecIntoBizObj(String record) {        int column = 0;        int index = 0;        int prevIndex = 0;        String field;        reportRecord reportRec = new reportRecord();               //Process swCase        while (prevIndex < record.length()) {            index = record.indexOf('|', prevIndex);            if (index < 0) {                field = record.substring(prevIndex, record.length()-1);                prevIndex = record.length()+1;            } else {                field = record.substring(prevIndex, index);                prevIndex = index+1;            }            switch (column) {                case 0:                    reportRec.setJLID(field);                    break;                case 1:                    reportRec.setContent(field);                    break;            }   column++;                   }        return (reportRec);    }    private class MyFilter implements RecordFilter {        // Define the filtering modes        private static final int FILTER_NONE = 0;        private static final int FILTER_STARTSWITH = 1;        private static final int FILTER_CONTAINS = 2;        private static final int FILTER_EXACT = 3;        private String filterText;        private int filterColumn;        private int filterBy;        public MyFilter() {            super();            filterColumn = 0;            filterBy = FILTER_NONE;        }        public void setFilterCondition(String searchText, int recColumn, int by) {            filterText = searchText;            filterColumn = recColumn;            filterBy = by;        }        public boolean matches(byte[] rec) {            int i;            int index = 0;            int prevIndex = 0;            String record;            String field = new String();            try {                ByteArrayInputStream bin = new ByteArrayInputStream(rec);                DataInputStream      din = new DataInputStream(bin);                record = din.readUTF();                for (i=0; i<filterColumn && prevIndex < record.length(); i++) {                    index = record.indexOf('|', prevIndex);                    if (index < 0) {                        field = record.substring(prevIndex, record.length()-1);                        prevIndex = record.length()+1;                    } else {                        field = record.substring(prevIndex, index);                        prevIndex = index+1;                    }                }                if( filterBy == FILTER_STARTSWITH ){                    return (field.startsWith(filterText));                } else if( filterBy == FILTER_CONTAINS ){                    return (field.indexOf(filterText) >= 0);                } else if( filterBy == FILTER_EXACT ){                    return (field.equals(filterText));                }            } catch (Exception e) {                return false;            }            return false;        }    }    private class MyComparator implements RecordComparator {        private int sortColumn;        public void setSortColumn(int column) {            sortColumn = column;        }        public int compare(byte[] rec1, byte[] rec2){            int i;            int index = 0;            int prevIndex = 0;            String record1;            String record2;            String field1 = new String();            String field2 = new String();            try {                ByteArrayInputStream bin1 = new ByteArrayInputStream(rec1);                DataInputStream      din1 = new DataInputStream(bin1);                ByteArrayInputStream bin2 = new ByteArrayInputStream(rec2);                DataInputStream      din2 = new DataInputStream(bin2);                record1 = din1.readUTF();                record2 = din2.readUTF();                for (i=0; i<sortColumn && prevIndex < record1.length(); i++) {                    index = record1.indexOf('|', prevIndex);                    if (index < 0) {                        field1 = record1.substring(prevIndex, record1.length()-1);                        prevIndex = record1.length()+1;                    } else {                        field1 = record1.substring(prevIndex, index);                        prevIndex = index+1;                    }                }                                for (i=0, index = 0, prevIndex = 0; i<sortColumn && prevIndex < record2.length(); i++) {                    index = record2.indexOf('|', prevIndex);                    if (index < 0) {                        field2 = record2.substring(prevIndex, record2.length()-1);                        prevIndex = record2.length()+1;                    } else {                        field2 = record2.substring(prevIndex, index);                        prevIndex = index+1;                    }                }                i = field1.compareTo(field2);                if (i == 0) {                    return EQUIVALENT;                } else if (i<0) {                    return PRECEDES;                } else {                    return FOLLOWS;                }            } catch(Exception e) {                return EQUIVALENT;            }        }    }}

⌨️ 快捷键说明

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