J2me--RMS学习笔记

(一)  记录存储

a)         记录管理系统:可以插入记录,读记录,查询特定记录,排序记录管理系统中存储的数据

b)        记录管理系统并不是一个关系数据库,不能在数据之间使用结构化查询语言(SQL,相反,可以使用记录管理系统的应用程序界面和枚举应用程序接口进行排序,查询,以及对其他存储信息的管理

c)         记录管理系统第一列是记录的ID,第二列是包含了持续性数据的字节数组

d)        记录存储器的名字必须是字母的最小集,不能超过32个字母,字母符号Unicode并且区分大小写

e)         一套MIDlet集合中的记录存储器必须命名唯一,记录存储器可以在同一套MIDlet集合的MIDlet之间共享

f)         记录存储必须掌握的几项使用:

                        i.              创建一条新记录,并且读取一条已存在的记录

                      ii.              读写复杂数据类型的记录  Te

                    iii.              把简单数据类型的记录读入RecordEnumeration中  Test_3

                     iv.              把复杂数据类型的记录读入RecordEnumeration中  Test_4

                       v.              排序RecordEnumeration中的单数据类型的记录  Test_5

1.         RecordComparator

a)         compare

                     vi.              排序RecordEnumeration中的复杂数据类型的记录 Test_6

                   vii.              查询单数据类型的记录 Test_7

1.         RecordFilter

a)         matches

                 viii.              查询复杂数据类型的记录  Test_8

                     ix.              RecordListener

1.         把传给enumerationRecords()方法的第三个参数设为true使RecordEnumeration和记录存储器同步

2.         当记录存储器发生变化时,还希望引起其他事件,例如你可能希望在表单上改变信息,反映记录存储器的最新数据

3.         recordAdded , recordChanged, recordDeleted

 学习代码:

package mvc.mobile.test;

 

import javax.microedition.midlet.*;

import javax.microedition.lcdui.Display;

import javax.microedition.lcdui.Alert;

import javax.microedition.lcdui.Form;

import javax.microedition.lcdui.Command;

import javax.microedition.lcdui.CommandListener;

import javax.microedition.lcdui.Displayable;

import javax.microedition.rms.RecordStore;

import javax.microedition.rms.RecordEnumeration;

import javax.microedition.lcdui.AlertType;

import java.io.ByteArrayOutputStream;

import java.io.DataOutputStream;

import java.io.ByteArrayInputStream;

import java.io.DataInputStream;

 

/**

 *@(#)RMSTest.java

 * 版权声明 , 版权所有 违者必究

 * 版本号  1.0

 * 程序说明:关于记录存储器的使用

 * 1.声明类的引用

 * 2.创建类的实例,并赋值给这个类的引用

 * 3.打开记录存储器,如果这个记录存储器不存在就创建一个新的记录存储器

 * 4.显示打开或者创建存储器时发生的错误

 * 5.关闭记录存储器

 * 6.显示关闭记录存储区时发生的错误

 * 7.删除记录存储器

 * 8.显示删除记录存储器时发生的错误

 *修订记录:

 *1)更改者:not attributable

 *  时 间:2009--

 *  描 述:创建

 */

 

public class RMSTest

    extends MIDlet implements CommandListener {

  private Display iDisplay;

  private Alert iAlert;

  private Form iForm;

  private Comparator sComparator;

  private Command iExit;

  private Command iStart;

  private RecordStore iRecordStore = null;

  private RecordEnumeration iRecordEnumeratioin = null;

 

  public RMSTest() {

    iDisplay = Display.getDisplay(this);

    iExit = new Command("Exit", Command.EXIT, 1);

    iStart = new Command("Start", Command.SCREEN, 1);

    iForm = new Form("Record Store");

    iForm.addCommand(iExit);

    iForm.addCommand(iStart);

    iForm.setCommandListener(this);

  }

 

  /**

   * destroyApp

   *

   * @param _boolean boolean

   * @throws MIDletStateChangeException

   * @todo Implement this javax.microedition.midlet.MIDlet method

   */

  protected void destroyApp(boolean _boolean) throws MIDletStateChangeException {

  }

 

  /**

   * pauseApp

   *

   * @todo Implement this javax.microedition.midlet.MIDlet method

   */

  protected void pauseApp() {

  }

 

  /**

   * startApp

   *

   * @throws MIDletStateChangeException

   * @todo Implement this javax.microedition.midlet.MIDlet method

   */

  protected void startApp() throws MIDletStateChangeException {

    iDisplay.setCurrent(iForm);

  }

 

  public static void main(String[] args) {

    RMSTest rmstest = new RMSTest();

  }

  /**

   * 读写基于字符串的记录的步骤

   * 1.声明类的引用

   * 2.创建类的实例,并赋值给这些类的引用

   * 3.打开记录存储器,如果这个记录存储器不存在,就创建一个新的记录存储器

   * 4.显示打开或创建记录存储器时候发生的错误

   * 5.创建字符串形式的数据

   * 6.把这个数据转换成字节数据

   * 7.把字节数组写到记录存储器中

   * 8.从记录存储器中读取数据之前,创建一个字节数组

   * 9.获得字节存储器中的每条记录

   * 10.循环遍历记录存储器中的每条记录

   * 11.判断当前记录大小是否超过字节数组的长度,如果超过,则创建一个新的大小足够存储这个记录的字节数组

   * 12.从记录存储器将当前记录复制到这个字节数组中

   * 13.把这个字节数组转换成字符串,并且在屏幕上显示这个字符串

   * 14.关闭记录存储器,并且显示关闭记录存储器时发生的错误

   * 15.删除记录存储器,并且显示删除记录存储器时发生的错误

   *

   * 读写复杂数据类型的记录

   * 1.声明类的引用

   * 2.创建类的实例,并付给这些类的引用

   * 3.打开记录存储器,如果不存在,则创建一个新的记录存储器

   * 4.显示打开或者创建记录存储器时发生的错误

   * 5.创建合适的数据类型的数据

   * 6.把数据转换成字节数组输出流

   * 7.使用字节数组输出流创建数据输出流

   * 8.把记录的每一列写到数据输出流中

   * 9.把数据输出流转换成一个字节数组

   * 10.把这条记录写到记录存储器中

   * 11.关闭字节数组输出流和数据输出流

   * 12.显示些记录存储器时发生的任何错误

   * 13.创建能保存一条记录的字节缓冲区

   * 14.创建字节数组输入流和数据输入流

   * 15.循环遍历记录存储器,从记录存储器中将每一列复制到一个变量中

   * 16.在对话框中显示数据

   * 17.显示从记录存储器中读取记录时发生的错误

   * 18.关闭和删除记录存储器

   * @param command Command

   * @param displayable Displayable

   */

  public void commandAction(Command command, Displayable displayable) {

    if (command == iExit) {

      try {

        destroyApp(true);

      }

      catch (MIDletStateChangeException ex1) {

      }

      notifyDestroyed();

    }

    else if (command == iStart) {

      try {

        iRecordStore = RecordStore.openRecordStore("myRecordStore", true);

 

      }

      catch (Exception ex) {

        iAlert = new Alert("Error creating", ex.toString(), null,

                           AlertType.WARNING);

        iAlert.setTimeout(Alert.FOREVER);

        iDisplay.setCurrent(iAlert);

      }

      /**

       * Test_1

       */

      //      try {

//        String sOutputData = "First Record" ;

//        byte[] sByteOutputData = sOutputData.getBytes();

//        iRecordStore.addRecord(sByteOutputData,0,sByteOutputData.length);

//

//      }

//      catch (Exception ex) {

//        iAlert = new Alert("Error writing", ex.toString(), null,

//                         AlertType.WARNING);

//      iAlert.setTimeout(Alert.FOREVER);

//      iDisplay.setCurrent(iAlert);

//

//      }

//

//      try {

//        byte[] sByteInputData = new byte[1];

//        int sLength= 0;

//        for(int x = 1 ; x <= iRecordStore.getNumRecords(); x++){

//         if(iRecordStore.getRecordSize(x)> sByteInputData.length)

//           sByteInputData= new byte[iRecordStore.getRecordSize(x)];

//         sLength = iRecordStore.getRecord(x,sByteInputData,0) ;

//       }

//       iAlert = new Alert("Reading", new String(sByteInputData,0,sByteInputData.length), null,

//                         AlertType.WARNING);

//      iAlert.setTimeout(Alert.FOREVER);

//      iDisplay.setCurrent(iAlert);

//

//

//      }

//      catch (Exception ex) {

//        iAlert = new Alert("Error reading", ex.toString(), null,

//                         AlertType.WARNING);

//      iAlert.setTimeout(Alert.FOREVER);

//      iDisplay.setCurrent(iAlert);

//

//      }

      /**

       * Test_2

       */

//      try {

//        byte[] sOutputRecord ;

//        String sOutputString= "First Record";

//        int sOutputInteger = 15;

//        boolean sOutputBoolean = true;

//        ByteArrayOutputStream sOutputStream = new ByteArrayOutputStream();

//        DataOutputStream sDataOutputStream = new DataOutputStream(sOutputStream);

//        sDataOutputStream.writeUTF(sOutputString);

//        sDataOutputStream.writeInt(sOutputInteger);

//        sDataOutputStream.writeBoolean(sOutputBoolean);

//        sDataOutputStream.flush();

//        sOutputRecord = sOutputStream.toByteArray();

//        iRecordStore.addRecord(sOutputRecord,0,sOutputRecord.length);

//        sOutputStream.reset();

//        sOutputStream.close();

//        sDataOutputStream.close();

//

//      }

//      catch (Exception ex) {

//        iAlert = new Alert("Error writing", ex.toString(), null,

//                         AlertType.WARNING);

//      iAlert.setTimeout(Alert.FOREVER);

//      iDisplay.setCurrent(iAlert);

//

//      }

//      try {

//        String sInputString = null;

//        int sInputInteger=0 ;

//        boolean sInputBoolean = false ;

//        byte[] sByteInputData = new byte[100];

//        ByteArrayInputStream sByteArrayInputStream = new ByteArrayInputStream(sByteInputData);

//        DataInputStream sDataInputStream = new DataInputStream(sByteArrayInputStream);

//        for( int x = 1 ; x <= iRecordStore.getNumRecords(); x++){

//          iRecordStore.getRecord(x,sByteInputData,0);

//          sInputString = sDataInputStream.readUTF();

//          sInputInteger = sDataInputStream.readInt();

//          sInputBoolean = sDataInputStream.readBoolean();

//

//        }

//        sByteArrayInputStream.close();

//        sDataInputStream.close();

//        iAlert = new Alert("Reading", sInputString +"  "+  sInputInteger+ "   " + sInputBoolean, null,

//                         AlertType.WARNING);

//      iAlert.setTimeout(Alert.FOREVER);

//      iDisplay.setCurrent(iAlert);

//

//

//      }

//      catch (Exception ex) {

//        iAlert = new Alert("Error Reading", ex.toString(), null,

//                         AlertType.WARNING);

//      iAlert.setTimeout(Alert.FOREVER);

//      iDisplay.setCurrent(iAlert);

//

//      }

 

      /**

       * Test_3

       */

//      try {

//        String sOutputData[] = {"First Record","Second Record","Third Record"};

//        for(int x = 0 ; x < 3; x++){

//         byte[] sByteOutputData= sOutputData[x].getBytes();

//         iRecordStore.addRecord(sByteOutputData,0,sByteOutputData.length);

//

//        }

//      }

//      catch (Exception ex) {

//                iAlert = new Alert("Error Writing", ex.toString(), null,

//                         AlertType.WARNING);

//      iAlert.setTimeout(Alert.FOREVER);

//     iDisplay.setCurrent(iAlert);

//

//      }

//      try {

//        StringBuffer sBuffer = new StringBuffer();

//        RecordEnumeration sRecordEnumeration = iRecordStore.enumerateRecords(null,null,false);

//        while( sRecordEnumeration.hasNextElement()){

//         sBuffer.append( new String(sRecordEnumeration.nextRecord()));

//         sBuffer.append("/n");

//       }

//       iAlert = new Alert("Reading", sBuffer.toString(), null,

//                        AlertType.WARNING);

//     iAlert.setTimeout(Alert.FOREVER);

//    iDisplay.setCurrent(iAlert);

//

//       }

//      catch (Exception ex) {

//        iAlert = new Alert("Error Reading", ex.toString(), null,

//                        AlertType.WARNING);

//     iAlert.setTimeout(Alert.FOREVER);

//    iDisplay.setCurrent(iAlert);

//

//      }

      /**

       * Test_4

       */

//      try {

//        byte[] sOutputRecord;

//        String sOutputString [] = {"First Record","Second Record","Third Record"};

//        int[] sOutputInteger ={15,10,25};

//        boolean[] sOutputBoolean ={true,false,true};

//        ByteArrayOutputStream sByteArrayOutputStream = new ByteArrayOutputStream();

//        DataOutputStream sDataOutputStream = new DataOutputStream(sByteArrayOutputStream);

//        for(int x = 0; x < 3 ; x++){

//           sDataOutputStream.writeUTF(sOutputString[x]);

//           sDataOutputStream.writeInt(sOutputInteger[x]);

//           sDataOutputStream.writeBoolean(sOutputBoolean[x]);

//           sDataOutputStream.flush();

//           sOutputRecord = sByteArrayOutputStream.toByteArray();

//           iRecordStore.addRecord(sOutputRecord,0,sOutputRecord.length);

//        }

//        sByteArrayOutputStream.reset();

//        sByteArrayOutputStream.close();

//        sDataOutputStream.close();

//      }

//      catch (Exception ex) {

//        iAlert = new Alert("Error Writing", ex.toString(), null,

//                        AlertType.WARNING);

//     iAlert.setTimeout(Alert.FOREVER);

//    iDisplay.setCurrent(iAlert);

//

//      }

//

//      try {

//        StringBuffer sBuffer= new StringBuffer();

//        byte[] sByteInputData = new byte[300];

//        ByteArrayInputStream sByteArrayInputStream = new ByteArrayInputStream(sByteInputData);

//        DataInputStream sDataInputStream = new DataInputStream(sByteArrayInputStream);

//        RecordEnumeration sRecordEnumeration = iRecordStore.enumerateRecords(null,null,false);

//        while(sRecordEnumeration.hasNextElement()){

//         iRecordStore.getRecord(sRecordEnumeration.nextRecordId(),sByteInputData,0);

//         sBuffer.append(sDataInputStream.readUTF());

//         sBuffer.append("/n");

//         sBuffer.append(sDataInputStream.readInt());

//         sBuffer.append("/n");

//         sBuffer.append(sDataInputStream.readBoolean());

//         sBuffer.append("/n");

//         iAlert = new Alert("Reading", sBuffer.toString(), null,

//                        AlertType.WARNING);

//     iAlert.setTimeout(Alert.FOREVER);

//    iDisplay.setCurrent(iAlert);

//

//        }

//      }

//      catch (Exception ex) {

//        iAlert = new Alert("Error Reading", ex.toString(), null,

//                        AlertType.WARNING);

//     iAlert.setTimeout(Alert.FOREVER);

//    iDisplay.setCurrent(iAlert);

//

//      }

//

      /**

       * Test_5

       */

      try {

//        String[] sOutputData = {"Mary","Bob","Adm"};

          /**

           * Test_9 网址排序及前缀匹配查询

           */

          String [] sOutputData ={ "www.baidu.com","www.google.cn","www.ucweb.cn","www.bidu.com"};

       // Test_6 && Test_8

//        int[] sOutputInteger = {15,10,5};

//        byte[] sOutputRecord ;

//        ByteArrayOutputStream sByteOutputStream   = new ByteArrayOutputStream();

//        DataOutputStream sDataOutputStream = new DataOutputStream(sByteOutputStream);

 

        for(int x =0 ; x< sOutputData.length; x++){

            /**

             * Test_5 && Test_7 && Test_9

             */

                     byte[] sByteOutputData = sOutputData[x].getBytes();

            iRecordStore.addRecord(sByteOutputData,0,sByteOutputData.length);

            /**

             * Test_6 && Test_8

             */

//           sDataOutputStream.writeUTF(sOutputData[x]);

//           sDataOutputStream.writeInt(sOutputInteger[x]);

//           sDataOutputStream.flush();

//           sOutputRecord = sByteOutputStream.toByteArray();

//           iRecordStore.addRecord(sOutputRecord,0,sOutputRecord.length);

//           sByteOutputStream.reset();

 

            }

            /**

             * Test_6 && Test_8

             */

//                        sByteOutputStream.close();

//           sDataOutputStream.close();

      }

      catch (Exception ex) {

               iAlert = new Alert("Error writing", ex.toString(), null,

                     AlertType.WARNING);

    iAlert.setTimeout(Alert.FOREVER);

   iDisplay.setCurrent(iAlert);

 

      }

      try {

      StringBuffer sBuffer= new StringBuffer();

      /**

       * Test_9

       */

      sComparator = new Comparator();

         /**

          * Test_5 && Test_6

          */

         //        RecordEnumeration sRecordEnumeration = iRecordStore.enumerateRecords(null,sComparator,false);

         /**

          * Test_7 && Test_8

          */

 

          Filter sfilter = new Filter("www.b");

 

         //Test_6 && Test_8

//        String[] sInputString = new String[3];

//        int z =0;

//        byte[] sbyteInputData= new byte[300];

//        ByteArrayInputStream sInputStream = new ByteArrayInputStream(sbyteInputData);

//        DataInputStream sDataInputStream = new DataInputStream(sInputStream);

        RecordEnumeration sRecordEnumeration =null;

     if(iRecordStore.getNumRecords()>0){

 

         sRecordEnumeration = iRecordStore.enumerateRecords(sfilter,sComparator,false);

 

        while(sRecordEnumeration.hasNextElement()){

 

//            /**

//             * Test_5

//             */

         sBuffer.append(new String(sRecordEnumeration.nextRecord()));

         sBuffer.append("/n");

//            /**

//             * Test_6 && Test_8

//             */

//            iRecordStore.getRecord(sRecordEnumeration.nextRecordId(),sbyteInputData,0);

//            sBuffer.append(sDataInputStream.readUTF());

//            sBuffer.append("/n");

//            sBuffer.append(sDataInputStream.readInt());

//            sBuffer.append("/n");

//            sDataInputStream.reset();

//

 

 

        }

            }

            iAlert = new Alert("Reading", sBuffer.toString(), null,

                     AlertType.WARNING);

  iAlert.setTimeout(Alert.FOREVER);

 iDisplay.setCurrent(iAlert);

 

 

         /**

          * Test_7

          */

//         if ( sRecordEnumeration.numRecords() > 0 ){

//          String sString = new String( sRecordEnumeration.nextRecord());

//

//          iAlert = new Alert("Reading",sString, null , AlertType.WARNING);

//          iAlert.setTimeout(Alert.FOREVER);

//          iDisplay.setCurrent(iAlert);

//         }

         /**

          * 资源回收处

          */

         /**

          * Test_6 && Test_9

          */

         //         sInputStream.close();

//         sDataInputStream.close();

         sRecordEnumeration.destroy();

         sfilter.filterClose();

      }

      catch (Exception ex) {

            iAlert = new Alert("Error Reading", ex.toString(), null,

                        AlertType.WARNING);

     iAlert.setTimeout(Alert.FOREVER);

    iDisplay.setCurrent(iAlert);

 

      }

      try {

        iRecordStore.closeRecordStore();

        //Test_9

        sComparator.compareClose();

      }

      catch (Exception ex) {

 

        iAlert = new Alert("Error closing", ex.toString(), null,

                           AlertType.WARNING);

        iAlert.setTimeout(Alert.FOREVER);

        iDisplay.setCurrent(iAlert);

 

      }

      if (RecordStore.listRecordStores() != null) {

        try {

          RecordStore.deleteRecordStore("myRecordStore");

 

 

        }

        catch (Exception ex) {

          iAlert = new Alert("Error deleting", ex.toString(), null,

                             AlertType.WARNING);

          iAlert.setTimeout(Alert.FOREVER);

          iDisplay.setCurrent(iAlert);

 

        }

      }

    }

  }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值