一个记录存储管理系统的例子
在J2ME开发过程中,其中RMS很重要,可以存储手机本地的数据,我国现在的无线网络如GPRS、CDMA等还不稳定,而且覆盖面还不广泛,因而如果采用无线移动方式去开发移动应用系统,如交通、保险、烟草等行业的应用系统,在很多地方并不能在线进行数据交互,为了解决这一问题,采用C/S结构,而不采用B/S结构,在手机端开发客户端程序,通过网络传递数据,在信号不稳定的地方可将数据存储在本地,到信号好的地方再和服务端进行通信,完成数据的交互。
在这个过程中,RMS系统起着很重要的作用,用来存储数据(包括本地的数据及从服务端传来的数据),以下是一个RMS的简单例子,让大家可以理解RMS的应用。
这是一个简单的记事本程序,可以将朋友的名字进行记录,也可以查询某个人的名字,从中我们可以了解怎样对记录进行操作,如增加、查询、删除等。
MIDlet为notebook.java,RF.java是生成recordFilter记录过滤器的类,用来形成记录集,起到相当于recordSet的作用。
notebook.java:
importjavax.microedition.midlet.*;
importjavax.microedition.lcdui.*;
importjavax.microedition.rms.*;
publicclassnotebookextendsMIDletimplementsCommandListener{
privateDisplaydisplay;
privateFormform;
privateCommandcmdExit,cmdOk,cmdQuery;
privateTextFieldtextfield;
privateRecordStorers=null;
staticfinalStringrsName="NOTEBOOK";
privatebyte[]data;
intlastID;
intrecordID;
RecordFilterrf;
publicnotebook(){
display=Display.getDisplay(this);
form=newForm("记事本");
cmdExit=newCommand("退出",Command.EXIT,1);
cmdOk=newCommand("确定",Command.SCREEN,1);
cmdQuery=newCommand("查询",Command.SCREEN,1);
textfield=newTextField("请输入名字:","",20,0);
form.addCommand(cmdExit);
form.addCommand(cmdOk);
form.addCommand(cmdQuery);
form.append(textfield);
form.setCommandListener(this);
try{
rs=RecordStore.openRecordStore(rsName,true);
}catch(RecordStoreExceptione){
}
}
publicvoidstartApp(){
display.setCurrent(form);
}
publicvoidpauseApp(){}
publicvoiddestroyApp(booleanu){}
publicvoidcommandAction(Commandc,Displayabled){
if(c==cmdExit){
try{
rs.closeRecordStore();
}catch(RecordStoreExceptione){
}
destroyApp(true);
notifyDestroyed();
}
if(c==cmdOk){
data=(textfield.getString()).getBytes();
try{
recordID=rs.addRecord(data,0,data.length);
System.out.println("recordID"+recordID+"isadded!Totalnumis"+rs.getNumRecords());
}catch(RecordStoreExceptione){
System.out.println("Programerror!exit");
}
textfield.setString("");
}
if(c==cmdQuery){
try{
intlastID=rs.getNextRecordID();
for(inti=1;i
try{
byte[]data1=rs.getRecord(i);
if(data1==null){
System.out.println("null");
rs.deleteRecord(i);
}else{
System.out.println(newString(data1));
}
}catch(InvalidRecordIDExceptione){
continue;}
}
}catch(RecordStoreExceptione){}
rf=newRF((textfield.getString()).getBytes());
try{
RecordEnumerationenum=rs.enumerateRecords(rf,null,false);
System.out.println("查询结果如下:");
while(enum.hasNextElement()){
byte[]data2=enum.nextRecord();
if(data2==null){System.out.println("null");}else{
System.out.println(newString(data2));}
}
}catch(Exceptione){
System.out.println("programerror"+e.getMessage());
}
}
}
}
RF.java:
importjavax.microedition.rms.*;
publicclassRFimplementsRecordFilter{
byte[]data4;
RF(byte[]data4){
this.data4=data4;
}
publicbooleanmatches(byte[]candidate){
Strings1=newString(candidate);
if(s1.equals(newString(data4))){
returntrue;
}else{
returnfalse;
}
}
}
以上程序在WTK中运行通过,请大家参考,提出宝贵意见。
在这个过程中,RMS系统起着很重要的作用,用来存储数据(包括本地的数据及从服务端传来的数据),以下是一个RMS的简单例子,让大家可以理解RMS的应用。
这是一个简单的记事本程序,可以将朋友的名字进行记录,也可以查询某个人的名字,从中我们可以了解怎样对记录进行操作,如增加、查询、删除等。
MIDlet为notebook.java,RF.java是生成recordFilter记录过滤器的类,用来形成记录集,起到相当于recordSet的作用。
notebook.java:
importjavax.microedition.midlet.*;
importjavax.microedition.lcdui.*;
importjavax.microedition.rms.*;
publicclassnotebookextendsMIDletimplementsCommandListener{
privateDisplaydisplay;
privateFormform;
privateCommandcmdExit,cmdOk,cmdQuery;
privateTextFieldtextfield;
privateRecordStorers=null;
staticfinalStringrsName="NOTEBOOK";
privatebyte[]data;
intlastID;
intrecordID;
RecordFilterrf;
publicnotebook(){
display=Display.getDisplay(this);
form=newForm("记事本");
cmdExit=newCommand("退出",Command.EXIT,1);
cmdOk=newCommand("确定",Command.SCREEN,1);
cmdQuery=newCommand("查询",Command.SCREEN,1);
textfield=newTextField("请输入名字:","",20,0);
form.addCommand(cmdExit);
form.addCommand(cmdOk);
form.addCommand(cmdQuery);
form.append(textfield);
form.setCommandListener(this);
try{
rs=RecordStore.openRecordStore(rsName,true);
}catch(RecordStoreExceptione){
}
}
publicvoidstartApp(){
display.setCurrent(form);
}
publicvoidpauseApp(){}
publicvoiddestroyApp(booleanu){}
publicvoidcommandAction(Commandc,Displayabled){
if(c==cmdExit){
try{
rs.closeRecordStore();
}catch(RecordStoreExceptione){
}
destroyApp(true);
notifyDestroyed();
}
if(c==cmdOk){
data=(textfield.getString()).getBytes();
try{
recordID=rs.addRecord(data,0,data.length);
System.out.println("recordID"+recordID+"isadded!Totalnumis"+rs.getNumRecords());
}catch(RecordStoreExceptione){
System.out.println("Programerror!exit");
}
textfield.setString("");
}
if(c==cmdQuery){
try{
intlastID=rs.getNextRecordID();
for(inti=1;i
try{
byte[]data1=rs.getRecord(i);
if(data1==null){
System.out.println("null");
rs.deleteRecord(i);
}else{
System.out.println(newString(data1));
}
}catch(InvalidRecordIDExceptione){
continue;}
}
}catch(RecordStoreExceptione){}
rf=newRF((textfield.getString()).getBytes());
try{
RecordEnumerationenum=rs.enumerateRecords(rf,null,false);
System.out.println("查询结果如下:");
while(enum.hasNextElement()){
byte[]data2=enum.nextRecord();
if(data2==null){System.out.println("null");}else{
System.out.println(newString(data2));}
}
}catch(Exceptione){
System.out.println("programerror"+e.getMessage());
}
}
}
}
RF.java:
importjavax.microedition.rms.*;
publicclassRFimplementsRecordFilter{
byte[]data4;
RF(byte[]data4){
this.data4=data4;
}
publicbooleanmatches(byte[]candidate){
Strings1=newString(candidate);
if(s1.equals(newString(data4))){
returntrue;
}else{
returnfalse;
}
}
}
以上程序在WTK中运行通过,请大家参考,提出宝贵意见。