简单的RMS操作

来源:互联网 发布:封闭区域图形填充算法 编辑:程序博客网 时间:2024/05/01 04:58


import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;
import javax.microedition.lcdui.*;
//import javax.microedition.rms.*;

public class Rcord extends MIDlet implements CommandListener{
 private Display mdisplay;
 private Form mform;
 private TextField mtextfiled;
 private Command start;
 private Command stop;
    //private mythread thp1;
 public Rcord() {
  // TODO Auto-generated constructor stub
  mform = new Form("Wellcome DB test");
  mtextfiled = new TextField("Money", null, 20, TextField.DECIMAL);
  mform.append(mtextfiled);
  start = new Command("start", Command.OK, 1);
  stop =  new Command("stop", Command.EXIT, 1);
  mform.addCommand(start);
  mform.addCommand(stop);
  mform.setCommandListener(this);
  
  
  
 }

 protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
  // TODO Auto-generated method stub
  //destroyApp(false);
  //notifyDestroyed();

 }

 protected void pauseApp() {
  // TODO Auto-generated method stub
  

 }

 protected void startApp() throws MIDletStateChangeException {
  // TODO Auto-generated method stub
  

  mdisplay = Display.getDisplay(this);
  
  mdisplay.setCurrent(mform);
  
 }
 public void commandAction(Command c,Displayable d) {
  
  
  if (c == start){
   //thp1 = new mythread(1);
   //thp1.start();
   DBtest mytest = new DBtest();
   String textf;
   try {
    textf = mytest.testDatabase(mtextfiled.getString(),mtextfiled.getMaxSize());
    mform.append(textf);  
    } catch (RecordStoreFullException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    } catch (RecordStoreNotFoundException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    } catch (RecordStoreException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }     
     
       
 
  }
  else if(c == stop)
   notifyDestroyed();
   
  
 }
 
 
}

 

以上Rcord.java

 

 


import javax.microedition.rms.*;

public class DBtest  {
 DBtest(){
  
 }

    public String testDatabase(String st,int size) throws RecordStoreFullException, RecordStoreNotFoundException, RecordStoreException  {

     
     
     
     RecordStore anRMS = RecordStore.openRecordStore("myDB" , true);
     
       byte[] buffer1 = st.getBytes();
       int id =anRMS.addRecord(buffer1, 0, buffer1.length);
      
       byte[] buffer2 = anRMS.getRecord(id);
       String restring =  new String(buffer2);
       System.out.println(new String(buffer2));
     
       anRMS.closeRecordStore();
       return restring;
      
      
    }
   

       
}
以上DBtest .java

 

 

写在最后本来想写个多线程的 后来不知道如何调试就没有写,在这里发个简单版

 

 


// TODO: Auto-generated Javadoc
/**
 * The Class mythread.
 */
public class mythread extends Thread  {

 
 private int id;

 /**
  * Instantiates a new mythread.
  */
 public mythread(int id){
      this.id = id;

  
 }

 /* (non-Javadoc)
  * @see java.lang.Thread#run()
  */
 public void run() {
  // TODO Auto-generated method stub
  
  System.out.println("线程"+ id + "执行完毕/n");
  
 }

}
以上就是今天的成果 英文真的很讨厌!!!!