?? reportdb.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 + -