Get record from RecordStore : RecordStore « J2ME « Java Tutorial






Get record from RecordStore
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreNotFoundException;

public class getRecordMIDlet extends MIDlet implements CommandListener {
  private Command exitCommand = new Command("exit", Command.STOP, 1);

  private Display display;

  public getRecordMIDlet() {
    display = Display.getDisplay(this);
  }

  public void startApp() {
    TextBox aTextBox = new TextBox("Main", null, 256, TextField.ANY);
    RecordStore rs = null;
    boolean existingOrNot = false;
    existingOrNot = existing("aRS");
    if (existingOrNot) {
      try {
        rs = RecordStore.openRecordStore("aRS", false);
      } catch (Exception e) {
      }
    } else {
      try {
        rs = RecordStore.openRecordStore("aRS", true);
      } catch (Exception e) {
      } finally {
      }
    }

    try {
      String record = "";
      for (int i = 1; i < rs.getNextRecordID(); i++) {
        record = new String(rs.getRecord(i));
      }
      aTextBox.setString(record);
    } catch (Exception e) {
      aTextBox.setString("Failed");
      try {
        rs.closeRecordStore();
        RecordStore.deleteRecordStore("aRS");
      } catch (Exception x) {
      }
    }

    try {
      rs.closeRecordStore();
    } catch (Exception e) {
    }

    aTextBox.addCommand(exitCommand);
    aTextBox.setCommandListener(this);
    display.setCurrent(aTextBox);
  }

  public void pauseApp() {
  }

  public void destroyApp(boolean unconditional) {
  }

  public boolean existing(String recordStoreName) {
    boolean existingOrNot = false;
    RecordStore rs = null;
    if (recordStoreName.length() > 32)
      return false;
    try {
      rs = RecordStore.openRecordStore(recordStoreName, false);
    } catch (RecordStoreNotFoundException e) {
      existingOrNot = false;
    } catch (Exception e) {
    } finally {
      try {
        rs.closeRecordStore();
      } catch (Exception e) {
      }
    }
    return existingOrNot;
  }

  public void commandAction(Command c, Displayable s) {
    destroyApp(false);
    notifyDestroyed();
  }
}








31.47.RecordStore
31.47.1.Load yahoo stock quote
31.47.2.Add records to RecordStore
31.47.3.delete RecordStore
31.47.4.Filter FieldsFilter Fields
31.47.5.Get record from RecordStoreGet record from RecordStore
31.47.6.Save data to RecordStoreSave data to RecordStore
31.47.7.Record MonitorRecord Monitor
31.47.8.Retrieve AllRetrieve All
31.47.9.Sort FieldsSort Fields
31.47.10.Record Enumeration
31.47.11.RecordStore read and write
31.47.12.Read and write mixed data types with RecordStore
31.47.13.Mixed Record Enumeration
31.47.14.Sort records in RecordStore
31.47.15.Sort Mixed Record DataType
31.47.16.Search record in RecordStore
31.47.17.Search Mixed Record Data Type