TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
一个RMS的简单例子,让大家可以理解RMS的应用。
简单的记事本程序,可以将朋友的名字进行记录,也可以查询某个人的名字,从中我们可以了解怎样对记录进行操作,如增加、查询、删除等。
MIDlet为notebook.java,RF.java是生成recordFilter记录过滤器的类,用来形成记录集,起到相当于recordSet的作用。
一、RF.java - import javax.microedition.rms.*;
- public class RF implements RecordFilter{
- byte[] data4;
- RF(byte[] data4){
- this.data4=data4;
- }
- public boolean matches(byte[] candidate){
- String s1=new String(candidate);
- if(s1.equals(new String(data4))){
- return true;
- }else{
- return false;
- }}}
复制代码
二、notebook.java- import javax.microedition.midlet.*;
- import javax.microedition.lcdui.*;
- import javax.microedition.rms.*;
- public class notebook extends MIDlet implements CommandListener{
- private Display display;
- private Form form;
- private Command cmdExit,cmdOk,cmdQuery;
- private TextField textfield;
- private RecordStore rs=null;
- static final String rsName="NOTEBOOK";
- private byte[] data;
- int lastID;
- int recordID;
- RecordFilter rf;
- public notebook(){
- display=Display.getDisplay(this);
- form=new Form("记事本");
- cmdExit=new Command("退出",Command.EXIT,1);
- cmdOk=new Command("确定",Command.SCREEN,1);
- cmdQuery=new Command("查询",Command.SCREEN,1);
- textfield=new TextField("请输入名字:","",20,0);
- form.addCommand(cmdExit);
- form.addCommand(cmdOk);
- form.addCommand(cmdQuery);
- form.append(textfield);
- form.setCommandListener(this);
- try{
- rs=RecordStore.openRecordStore(rsName,true);
- }catch(RecordStoreException e){
- }
- }
- public void startApp(){
- display.setCurrent(form);
- }
- public void pauseApp(){}
- public void destroyApp(boolean u){}
-
- public void commandAction(Command c,Displayable d){
- if(c==cmdExit){
- try{
- rs.closeRecordStore();
- }catch(RecordStoreException e){
- }
- destroyApp(true);
- notifyDestroyed();
- }
- if(c==cmdOk){
- data=(textfield.getString()).getBytes();
- try{
- recordID=rs.addRecord(data,0,data.length);
- System.out.println("recordID "+recordID+" is added!Total num is "+rs.getNumRecords());
- }catch(RecordStoreException e){
- System.out.println("Program error!exit");
- }
- textfield.setString("");
- }
- if(c==cmdQuery){
- try{
- int lastID=rs.getNextRecordID();
- for(int i=1;i< lastID;i++){
- try{
- byte[] data1=rs.getRecord(i);
- if(data1==null){
- System.out.println("null");
- rs.deleteRecord(i);
- }else{
- System.out.println(new String(data1));
- }
- }catch(InvalidRecordIDException e){
- continue;
- }
- }
- }catch(RecordStoreException e){}
- rf=new RF((textfield.getString()).getBytes());
- try{
- RecordEnumeration enu=rs.enumerateRecords(rf,null,false);
- System.out.println("查询结果如下:");
- while(enu.hasNextElement()){
- byte[] data2=enu.nextRecord();
- if(data2==null){System.out.println("null");}else{
- System.out.println(new String(data2));}
- }
- }catch(Exception e){
- System.out.println("program error"+e.getMessage());
- }
- }
- }
- }
- 运行图:
- [img]http://img.javaxxz.com/2014/10/11/081013140.gif[/img]
- [img]http://img.javaxxz.com/2014/10/11/081013265.gif[/img]
复制代码
源码下载:http://203.93.208.26/kj/cwb/dir7/notebook1.zip |
|