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

📄 worklogdb.java

📁 我参与的一个J2ME项目的部分代码
💻 JAVA
字号:
package com.sepco.apps.trouble.database;import java.util.*;import java.io.*;import javax.microedition.lcdui.*;import javax.microedition.midlet.*;import javax.microedition.rms.*;import com.sepco.apps.trouble.businessobject.*;public class workLogDb{    RecordStore recordStore = null;    RecordEnumeration enum = null;    String recordStoreName = null;    public workLogDb() {    }    public workLogDb(String storeName) {        //构造器:打开或者创建一个RMS数据库        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 workLogRecord getworkLog(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 setworkLog(workLogRecord 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 deleteworkLog(workLogRecord theRecord) {        try {            int recId = getRecordIdOfTicket(theRecord.getJLID());            if (recId != -1) {                recordStore.deleteRecord(recId);                return (true);            } else {                return (false);            }        } catch (Exception e) {            return (false);        }    }   //参数workLogID也即为工作单的第一个字段    public synchronized boolean deleteworkLog(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()) {                workLogRecord workLogRec = (workLogRecord)enum.nextElement();                ByteArrayOutputStream bout = new ByteArrayOutputStream();                DataOutputStream dout = new DataOutputStream(bout);                dout.writeUTF(workLogRec.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);        }    }      public void vectorOfBizObjIntoRMS(Vector in) {        if (in != null) {            Enumeration enum = in.elements();            while (enum.hasMoreElements()) {                workLogRecord workLogRec = (workLogRecord)enum.nextElement();                int recId = getRecordIdOfTicket(workLogRec.getJLID());                if (recId == -1) { // Not found                    setworkLog(workLogRec);                }            }        }    }        private workLogRecord parseRecIntoBizObj(String record) {        int column = 0;        int index = 0;        int prevIndex = 0;        String field;        workLogRecord workLogRec = new workLogRecord();              //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:                    workLogRec.setJLID(field);                    break;                case 1:                    workLogRec.setVoltageGrade(field);                    break;                case 2:                    workLogRec.setLineName(field);                    break;                case 3:                    workLogRec.setTeam(field);                    break;                case 4:                    workLogRec.setInCharge(field);                    break;                case 5:                    workLogRec.setStartTime(field);                    break;                case 6:                    workLogRec.setEndTime(field);                    break;                          case 7:                    workLogRec.setMethod(field);                    break;                case 8:                    workLogRec.setContent(field);                    break;                case 9:                    workLogRec.setSyncStatus(field);                    break;                   case 10:                    workLogRec.setToBeSync(field);                    break;                   default:                    break;            }           column++;        }      return (workLogRec);    }            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 + -