移动数据库的开发

1、javax.microedition.rms包含内容:
·类
RecordStore
·接口
RecordComparator
RecordEnumeration
RecordFilter
RecordListener
·异常
InvalidRecordIDException
RecordStoreException
RecordStoreFullException
RecordStoreNotFoundException
RecordStoreNotOpenException

2、记录仓库(RMS)的开启、关闭与删除
·RecordStore.openRecordStore()开启记录仓储,有三种方式
  〓 第一种:四个参数
   !第一个参数:String ,记录仓储的名称
   !第二个参数:boolean ,true表示开启的记录仓储不存在,创建一个新的;false则抛出RecordStoreNofFoundException异常
   !第三个参数:认证模式,读取权限;Record.AUTHMODE_PRIVATE(只有建立此仓储的MIDlet Suite才能存取这个仓储);Record.AUTHMODE_ANY(设备上任何MIDlet都可以读取记录)
   !第四个参数:boolean ,写入权限
  〓 第二种:三个参数(专门用来开启属于其它MIDlet Suite的记录仓储)
   !第一个参数:记录仓储的名称
   !第二个参数:MIDlet Suite供货商名称
  〓 第三种:两个参数
   !第一个参数:String ,记录仓储的名称
   !第二个参数:boolean ,true表示开启的记录仓储不存在,创建一个新的;false则抛出RecordStoreNofFoundException异常
·RecordStore提供的setMode()方法用来改变记录仓储的认证模式及写入权(只有属于建立此仓库的NIDlet才能调用该方法)
·RecordStore提供了listRecordStores()函数,取得目前MIDlet所存在的MIDlet Suite之中所有记录仓储的名称,这些名称会以字符串数组的方式返回
·当记录仓储使用完毕后,必须使用RecordStore类的closeRecordStore()函数来关闭记录仓储,以释放资源
·如果不需要该记录仓储,使用RecordStore类的deleteRecordStroe()方法来删除该记录仓库;如果该记录仓库未处于关闭状态,则抛出RecordStoreException
·getLastModified()取得记录仓储的时间戳记
·getNextRecordID()取得Next Record ID的值
·getNumRecords()取得记录仓储之中记录的笔数
·getSize()取得目前记录仓储所占据的记忆空间
·getSizeAvailable()目前还有多少可用空间
·getVersion()取得版本号

3、数据的增加、修改及删除
·int addRecord(byte[] data , int offset , int numBytes)
将byte数组存放到数据仓储之中,并传回其Record ID
·int getRecord(int recordid , byte[] data , int offset)
取出特定Record ID的那笔数据
·byte[] getRecord(int recordid)
取出特定Record ID的那笔数据
·void setRecord(int recordid , byte[] newdata , int offset , int numBytes)
设定特定ID的数据
·void deleteRecord(int recordid)
删除特定ID的数据
!RecordStore类只提供写入byte数组的服务,写入非byte的数据类型时就比较麻烦,如下例:
---------------------------------------------------------------------------------------------
类:RMSUtil
import javax.microedition.rms.*;

public class RMSUtil {
 public RMSUtil() {
 }
 //打开一个记录仓库,没有时创建
 public static RecordStore openRMSAnyway(String rmsname) {
  try {
   RecordStore rs = RecordStore.openRecordStore(rmsname , true);
   return rs;
  }catch(Exception e) {}
  return null;  
 }
 //打开现有的记录仓库,没有时不创建
 public static RecordStore openRMSExisted(String rmsname) {
  try {
   RecordStore rs = RecordStore.openRecordStore(rmsname , false);
   return rs;
  }catch(Exception e) {}
  return null;
 }
 //删除现有的记录仓库
 public static boolean deleteRMS(String rmsname) {
  if(rmsname.length() > 32)
   return false;
  try {
   RecordStore.deleteRecordStore(rmsname);
  }catch(Exception e) {
   return false;
  }
  return true;
 }
 //向记录仓库写入字符
 public static int writeChar2RMS(RecordStore rs,char data) {
  byte []tmp = new byte[2] ;
  tmp[0] = (byte)(0xff&( data >>  8)) ;
  tmp[1] = (byte)(0xff&( data >>  0)) ;
  try {
   return rs.addRecord(tmp,0,tmp.length) ;
  }catch(Exception e){}
  return -1 ;
 }
 //从记录仓库读取字符
 public static char readChar4RMS(RecordStore rs,int recordid) {
  byte []tmp = new byte[2] ;
  try {
   tmp = rs.getRecord(recordid) ;
  }catch(Exception e) {}
  char result = (char)(tmp[0]&0x00ff);
  result = (char)((result << 8) + (char)(tmp[1]&0x00ff)) ;
  return result ;
 }
 //向记录仓库写入整数型
 public static int writeInt2RMS(RecordStore rs,int data) {
  byte []tmp = new byte[4] ;
  tmp[0] = (byte)(0xff&( data >> 24)) ;
  tmp[1] = (byte)(0xff&( data >> 16)) ;
  tmp[2] = (byte)(0xff&( data >>  8)) ;
  tmp[3] = (byte)(0xff&( data >>  0)) ;
  try {
   return rs.addRecord(tmp,0,tmp.length) ;
  }catch(Exception e) {}
  return -1 ;
 }
 //从记录仓库读取整数型
 public static int readInt4RMS(RecordStore rs,int recordid) {
  byte []tmp = new byte[4] ;
  try {
   tmp = rs.getRecord(recordid) ;
  }catch(Exception e) {}
  int result = (tmp[0]&0x0000ff) ;
  result = (result << 8) + (tmp[1]&0x000000ff) ;
  result = (result << 8) + (tmp[2]&0x000000ff) ;
  result = (result << 8) + (tmp[3]&0x000000ff) ;
  return result ;
 }
}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
类:MyMIDlet
........................
String dbname = "testdb";
Form f = new Form("testing");
RecordStore rs = RMSUtil.openRMSAnyway(dbname);
if(rs == null) {
 f.append("Table open fail ...");
}else {
 try {
  int r1 = RMSUtil.writeChar2RMS(rs , 'H');
  int r2 = RMSUtil.writeChar2RMS(rs , 'M');
  int r3 = RMSUtil.writeInt2RMS(rs , 10000);
  int r4 = RMSUtil.writeInt2RMS(rs , -9999);
  f.append("" + RMSUtil.readInt4RMS(rs , r3));
  f.append("" + RMSUtil.readChar4RMS(rs , r1));
  f.append("" + RMSUtil.readChar4RMS(rs , r2));
  f.append("" + RMSUtil.readInt4RMS(rs , r4));
  rs.closeRecordStore();
  RMSUtil.deleteRMS(dbname);
 }catch(Exception e) {}
}
display.setCurrent(f);







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值