日期:2014-05-16 浏览次数:20453 次
/** * <p>Title: </p> * * <p>Description: </p> * * <p>Copyright: Copyright (c) 2008</p> * * <p>Company: WorthTech</p> * * @author bruce * @version 1.0 */ import javax.microedition.rms.*; //import java.io.*; //import java.util.Hashtable; //import com.mpay.io.SerializerInputStream; //import com.mpay.io.SerializerOutputStream; //import com.mpay.io.SerializationException; //import com.mpay.util.commlib.info.MenuInfo; //import com.mpay.util.commlib.ByteArray; //import com.mpay.util.commlib.info.BillInfo; //import com.mpay.util.commlib.TypeConvert; public class Database { private RecordStore store; public static Database instance; public Database() { } public static Database getDatabase() { if (instance == null) { instance = new Database(); } return instance; } public void open(String tableName) { try { this.store = RecordStore.openRecordStore(tableName, true); } catch (RecordStoreException ex) { ex.printStackTrace(); //System.out.println("打开数据库出错:" + ex.getMessage()); try { store.closeRecordStore(); store = null; } catch (Exception e) { e.printStackTrace(); } } } public int getNumRecords() { try { return this.store.getNumRecords(); } catch (RecordStoreNotOpenException ex) { ex.printStackTrace(); //System.out.println("未找到记录" + ex.getMessage()); return 0; } } public void close() { try { this.store.closeRecordStore(); } catch (RecordStoreNotOpenException ex) { ex.printStackTrace(); //System.out.println("数据库尚未打开:" + ex.getMessage()); } catch (RecordStoreException ex) { ex.printStackTrace(); //System.out.println("数据库关闭出错:" + ex.getMessage()); } } public int addRecord(String content) { byte[] data = content.getBytes(); try { return this.store.addRecord(data, 0, data.length); } catch (RecordStoreNotOpenException ex) { ex.printStackTrace(); //System.out.println("数据库尚未打开:" + ex.getMessage()); } catch (RecordStoreException ex) { ex.printStackTrace(); //System.out.println("写入数据出错:" + ex.getMessage()); } return -1; } public int addRecord(byte[] data) { try { return this.store.addRecord(data, 0, data.length); } catch (RecordStoreNotOpenException ex) { ex.printStackTrace(); //System.out.println("数据库尚未打开:" + ex.getMessage()); } catch (RecordStoreException ex) { ex.printStackTrace(); //System.out.println("写入数据出错:" + ex.getMessage()); } return -1; } // public int addRecord(Object obj) { // ByteArrayOutputStream baos = new ByteArrayOutputStream(); // SerializerOutputStream out = null; // try { // out = new SerializerOutputStream(baos); // if (obj instanceof MessageInfo) { // ((MessageInfo) obj).serialize(out); // } else if (obj instanceof MenuInfo) { // ((MenuInfo) obj).serialize(out); // } else if (obj instanceof Constants) { // ((Constants) obj).serialize(out); // } else if (obj instanceof BillInfo) { // ((BillInfo) obj).serialize(out); // } // out.flush(); // byte[] data = baos.toByteArray(); // return this.addRecord(data); // } catch (SerializationException ex) { // ex.printStackTrace(); // //System.out.println("序列化对象时异常:" + ex.getMessage()); // } catch (IOException e) { // e.printStackTrace(); // //System.out.println("序列化对象存储时异常:" + e.getMessage()); // } finally { // try { // if (out != null) { // out.close(); // } // if (baos != null) { // baos.close(); // } // } catch (IOException ex1) { // ex1.printStackTrace(); // } // } // return -1; // } public byte[] getRecord(int recordId) { try { return this.store.getRecord(recordId); } catch (InvalidRecordIDException ex) { ex.printStackTrace(); //System.out.println("不正确的记录ID索引" + ex.getMessage()); return null; } catch (RecordStoreNotOpenException ex) { ex.printStackTrace(); //System.out.println("数据库尚未打开:" + ex.getMessa