【问题描述】JavaMe不支持关系型数据库的操作,如何实现永久存储呢?
JavaMe中,之所以没有不支持数据库,是因为功能性手机一般存储空间较小。所以采用一种成为“记录”的概念代替,即采用一种成为RMS的机制进行永久存储。
【实例】
UserDataItem.java
package com.token.model; import com.token.util.StringDealMethod; public class UserDataItem { private int id; public String name = null; public String passwd = null; public UserDataItem(String name,String passwd) { this.name = name; this.passwd = passwd; } public UserDataItem(int id,byte[] data){ this.id=id; String temp=new String(data); String temp_sub[] = StringDealMethod.split(temp, ","); this.name = temp_sub[0]; this.passwd = temp_sub[1]; } public int getId(){ return id; } public void setId(int id){ this.id=id; } public String getName(){ return name; } public void setName(String name){ this.name = name; } public String getPasswd(){ return passwd; } public void setPasswd(String passwd){ this.passwd = passwd; } public byte[] getBytes(){ String temp=null; if(name==null||passwd==null){ return null; }else{ temp=name+","+passwd; } return temp.getBytes(); } }
UserDataRecord.java
package com.token.util; import java.util.Vector; import javax.microedition.rms.RecordComparator; import javax.microedition.rms.RecordEnumeration; import javax.microedition.rms.RecordStore; import javax.microedition.rms.RecordStoreException; import com.token.model.*; public class UserDataRecord { private static final String RECORDSTORE_NAME="USER_DB"; private static RecordStore info; public UserDataRecord(){ } //打开RecordStore,没有则创建 public void openDataBase() { try { info = RecordStore.openRecordStore(RECORDSTORE_NAME, true); }catch (RecordStoreException ex) { info =null; } } //关闭RecordStore public void closeDataBase() { if (info!= null) { try { info.closeRecordStore(); info=null; } catch (RecordStoreException ex) {} } } //增加记录 public int db_addRecord(UserDataItem item) { try { this.openDataBase(); byte[] data=item.getBytes(); int id=info.getNextRecordID(); info.addRecord(data,0,data.length); this.closeDataBase(); return id; } catch (RecordStoreException ex) { } return -1; } //更新记录 public void db_updateRecord(UserDataItem item){ try { this.openDataBase(); byte[] data=item.getBytes(); info.setRecord(item.getId(),data,0,data.length); this.closeDataBase(); } catch (RecordStoreException ex) { } } //访问一条记录 public UserDataItem db_getRecord(int id){ UserDataItem item=null; try { this.openDataBase(); item = new UserDataItem(id,info.getRecord(id)); this.closeDataBase(); } catch (RecordStoreException ex) { } return item; } //删除一条记录 public void db_deleteRecord(int id){ try { this.openDataBase(); info.deleteRecord(id); this.closeDataBase(); } catch (RecordStoreException ex) {} } //删除所有记录 public void db_deleteAllRecord(){ try { RecordStore.deleteRecordStore(RECORDSTORE_NAME); } catch (RecordStoreException ex) {} } //访问所有记录 public Vector db_getRecords(){ Vector items=new Vector(10,3); this.openDataBase();//打开RecordStore RecordEnumeration enum1=null; int ind=0; try{ UserDataItem item=null; enum1=info.enumerateRecords(null,new InnerComparator(),false); while(enum1.hasPreviousElement()){ ind=enum1.previousRecordId(); item=new UserDataItem(ind,info.getRecord(ind)); items.addElement(item); } }catch(Exception ex){ex.printStackTrace();} finally{ try{ enum1.destroy(); }catch(Exception e){} this.closeDataBase();//关闭RecordStore }//end finally return items; } //一个简单的比较器 private class InnerComparator implements RecordComparator{ public int compare(byte[] rec1, byte[] rec2){ if(rec1.length>rec2.length) return FOLLOWS; else if(rec1.length<rec2.length) return PRECEDES; else return EQUIVALENT; } } }
【分析】
我们模拟了一种数据库,下以“数据库”称之,实际上还是对记录进行操作。
1 打开数据库
public void openDataBase() { try { info = RecordStore.openRecordStore(RECORDSTORE_NAME, true); }catch (RecordStoreException ex) { info =null; } }
2 关闭数据库
public void closeDataBase() { if (info!= null) { try { info.closeRecordStore(); info=null; } catch (RecordStoreException ex) {} } }
3 增加记录(增)
public int db_addRecord(UserDataItem item) { try { this.openDataBase(); byte[] data=item.getBytes(); int id=info.getNextRecordID(); info.addRecord(data,0,data.length); this.closeDataBase(); return id; } catch (RecordStoreException ex) { } return -1; }
4 更新记录(改)
//更新记录 public void db_updateRecord(UserDataItem item){ try { this.openDataBase(); byte[] data=item.getBytes(); info.setRecord(item.getId(),data,0,data.length); this.closeDataBase(); } catch (RecordStoreException ex) { } }
5 查看记录(查)
//访问一条记录 public UserDataItem db_getRecord(int id){ UserDataItem item=null; try { this.openDataBase(); item = new UserDataItem(id,info.getRecord(id)); this.closeDataBase(); } catch (RecordStoreException ex) { } return item; }
6 删除记录
public void db_deleteRecord(int id){ try { this.openDataBase(); info.deleteRecord(id); this.closeDataBase(); } catch (RecordStoreException ex) {} }
7 删除所有记录
public void db_deleteAllRecord(){ try { RecordStore.deleteRecordStore(RECORDSTORE_NAME); } catch (RecordStoreException ex) {} }
8 如何使用?
(1) 新建对象
private UserDataRecord userRecord;
userRecord = new UserDataRecord();
(2) 添加记录
UserDataItem userItem = new UserDataItem(1,(username+","+passwd).getBytes()); userRecord.db_addRecord(userItem);
(3) 查看记录
userRecord.db_getRecord(1);
*1为记录id,在添加记录步骤中,id为1。
(4) 更新记录
UserDataItem userItem = new UserDataItem(1,(username+","+passwd).getBytes()); userRecord.db_updateRecord(userItem);
(5) 综合实例
userRecord.db_deleteAllRecord(); if(userRecord.db_getRecord(1)==null) { UserDataItem userItem = new UserDataItem(1,(username+","+passwd).getBytes()); userRecord.db_addRecord(userItem); userItem = null; }
*在添加记录前,要检测当前记录ID是否被使用过,否则“增”操作会失败。如果不指定数值,默认情况下,id会递增1。所以在多次调用时,而又只需要一条记录的情况下,调用了userRecord.db_deleteAllRecord();
*将UserDataRecord.java中的UserDataItem改为Object对象,稍作修改,可作为一个通用的数据库,有兴趣的朋友可以试一试。
转载请标明出处,仅供学习交流,勿用于商业目的
Copyright @ http://blog.csdn.net/tandesir