×
Namespaces

Variants
Actions

RMS complete reference

From Nokia Developer Wiki
Jump to: navigation, search

This article explains how to create, open, read, write and delete records on RMS.

Article Metadata
Code ExampleCompatibility
Platform(s):
Series 40
Symbian
Article
Created: adarsha_saraff (09 Jul 2012)
Last edited: hamishwillee (30 Jul 2013)

Note.pngNote: This article extends some elements of chapter 8 of http://www.21gradi.it/Public/Java_J2ME_TheCompleteReference_McGrawHill_Osborne.pdf

Contents

The Record Store

RMS stores the information in a record store. A record store compares to a flat file used for data storage in a file system and to a table of a database. RMS automatically assigns to each row a unique integer that identifies the row in record store, called record ID.

Creating, Opening, Closing and Removing a Record Store

  1. /* Create a record store */
  2.  RecordStore recordstore = null;
  3.  
  4. /* Opening a record store */
  5. try {
  6.               recordstore = RecordStore.openRecordStore("myRecordStore", true);
  7.               /* Argument 2 specifies, weather to create a new record store if it does not exists */
  8. }catch(Exception ex){}
  9.  
  10. /* Closing a record store */
  11. try{
  12.            recordstore.closeRecordStore();
  13. } catch (Exception ex) {} 
  14.  
  15. /* Removing a record store */
  16. try{
  17.           if(RecordStore.listRecordStores() != null)
  18.           {
  19.                          RecordStore.deleteRecordStore("myRecordStore");
  20.            }
  21. }catch(Exception ex){}

Writing and Reading a record store

Writing and Reading String-Based record

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordStore;
 
public class readwriteString extends MIDlet implements CommandListener {
 
RecordStore recordstore = null;
Form main;
Command stratCommand, readCommand;
 
public void startApp() {
main = new Form("writeString");
 
stratCommand = new Command("Add", Command.OK, 1);
readCommand = new Command("Read", Command.OK, 2);
main.addCommand(stratCommand);
main.setCommandListener(this);
Display.getDisplay(this).setCurrent(main);
}
 
public void pauseApp() {
}
 
public void destroyApp(boolean unconditional) {
 
}
 
public void commandAction(Command c, Displayable d) {
if (c == stratCommand) {
main.removeCommand(c);
String[] data = {"FirstRecord", "SecondRecord", "ThirdRecord"};
try {
/*
* Open record store
*/

recordstore = RecordStore.openRecordStore("myRecordStore", true);
/*
* Write to record store
*/

byte[] b;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(bout);
for (int i = 0; i < 3; i++) {
out.writeUTF(data[i]);
out.flush();
b = bout.toByteArray();
recordstore.addRecord(b, 0, b.length);
bout.reset();
}
bout.close();
out.close();
main.addCommand(readCommand);
System.out.println(recordstore.getNumRecords());
/*
* Close record store
*/

recordstore.closeRecordStore();
} catch (Exception ex) {
}
} else if (c == readCommand) {
/*
* Open record store
*/

try {
recordstore = RecordStore.openRecordStore("myRecordStore", true);
/*
* Read from record store
*/

byte[] input = new byte[7000];
ByteArrayInputStream iStream = new ByteArrayInputStream(input);
DataInputStream dinp = new DataInputStream(iStream);
for (int i = 1; i <= recordstore.getNumRecords(); i++) {
recordstore.getRecord(i, input, 0);
String str = dinp.readUTF();
main.append(str+"\n");
System.out.println(str);
dinp.reset();
}
dinp.close();
iStream.close();
/*
* Close record store
*/

recordstore.closeRecordStore();
} catch (Exception ex) {
}
}
}
}

StringRMS.png StringRMS1.png

Writing and Reading Mixed Data type record

Consider the table given below for an fruit merchant. ID - contains record ID, Column 1 - name of fruit, Column 2 - demand in market, Column 3 - stock available to fulfill demand.

ID Column 1 Column 2 Column 3
1 Apple 700 true
2 Orange 192 false
3 Mango 187 true
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordStore;
 
 
public class fruitmerchant extends MIDlet implements CommandListener {
 
Form add, read;
RecordStore merchantrecordstore = null;
TextField fruitname, demand;
ChoiceGroup available;
Command addCommand, readCommand, backCommand;
 
public void startApp() {
 
addCommand = new Command("Add", Command.OK, 1);
backCommand = new Command("Back", Command.BACK, 0);
readCommand = new Command("Read", Command.OK, 1);
add = new Form("add");
read = new Form("read");
add.addCommand(addCommand);
read.addCommand(backCommand);
add.setCommandListener(this);
read.setCommandListener(this);
 
fruitname = new TextField("Fruit Name", null, 32, TextField.INITIAL_CAPS_WORD);
demand = new TextField("Demand", null, 9, TextField.DECIMAL);
 
available = new ChoiceGroup("Stock", ChoiceGroup.POPUP);
available.append("true", null);
available.append("false", null);
 
add.append(fruitname);
add.append(demand);
add.append(available);
Display.getDisplay(this).setCurrent(add);
}
 
public void pauseApp() {
}
 
public void destroyApp(boolean unconditional) {
}
 
public void commandAction(Command c, Displayable d) {
if (c == addCommand) {
try {
merchantrecordstore = RecordStore.openRecordStore("recordstore", true);
String fname = fruitname.getString();
int dem = Integer.parseInt(demand.getString());
String f;
if (available.isSelected(0)) {
f = "true";
} else {
f = "false";
}
byte[] outByte;
ByteArrayOutputStream oStream = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(oStream);
dout.writeUTF(fname);
dout.writeInt(dem);
dout.writeUTF(f);
dout.flush();
outByte = oStream.toByteArray();
merchantrecordstore.addRecord(outByte, 0, outByte.length);
oStream.close();
dout.close();
merchantrecordstore.closeRecordStore();
add.addCommand(readCommand);
} catch (Exception ex) {
}
}
else if(c == backCommand){
Display.getDisplay(this).setCurrent(add);
}
else if(c == readCommand){
read.deleteAll();
read.append("FName|Demand|Stock\n");
try {
merchantrecordstore = RecordStore.openRecordStore("recordstore", true);
byte[] input = new byte[700];
ByteArrayInputStream iStream = new ByteArrayInputStream(input);
DataInputStream dinp = new DataInputStream(iStream);
for (int i = 1; i <= merchantrecordstore.getNumRecords(); i++) {
merchantrecordstore.getRecord(i, input, 0);
String fname = dinp.readUTF();
int dema = dinp.readInt();
String f = dinp.readUTF();
dinp.reset();
read.append(fname+"|"+dema+"|"+f+"\n");
}
dinp.close();
iStream.close();
merchantrecordstore.closeRecordStore();
Display.getDisplay(this).setCurrent(read);
} catch (Exception ex) {
}
}
}
}

MerchantRMS.png MerchantRMS1.png

Updating a existing record

To update an record use recordstore.setRecord(recordID,byte[],offset,length);

private void update(int no){
try {
recordstore = RecordStore.openRecordStore("myRS", true);
byte[] b;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(bout);
out.writeUTF("UpdatedString");
out.flush();
b = bout.toByteArray();
recordstore.setRecord(no,b,0,b.length);
out.close();
bout.close();
recordstore.closeRecordStore();
}catch(Exception ex){}
}

Removing a record from record store

Even though we have recordstore.deleteRecordStore(recordID) method. The record ID of deleted record is not reusable. If you want to delete a record in the middle, this will make the records in store to be noncontinuous. However we have an alternative:

  private void remove(int no) {
try {
recordstore = RecordStore.openRecordStore("myRS", true);
byte[] byt = new byte[700];
RecordStore rec = RecordStore.openRecordStore("abc", true);
for (int i = 1; i <= recordstore.getNumRecords(); i++) {
if (i != no) {
recordstore.getRecord(i, byt, 0);
rec.addRecord(byt, 0, byt.length);
}
}
recordstore.closeRecordStore();
RecordStore.deleteRecordStore("myRMS");
recordstore = RecordStore.openRecordStore("myRMS", true);
for (int j = 1; j <= rec.getNumRecords(); j++) {
rec.getRecord(j, byt, 0);
recordstore.addRecord(byt, 0, byt.length);
}
rec.closeRecordStore();
RecordStore.deleteRecordStore("abc");
recordstore.closeRecordStore();
} catch (Exception ex) {
}
}

Record Enumeration

Creating record enumeration

RecordEnumeration recordenumeration = recordstore.enumerateRecords(null, null, false);

You can retrieve a record from the record enumeration using one of two technique

while ( recordEnumeration.hasNextElement())
{
/* do something */
}
String string = new String(recordEnumeration.nextRecord());

Closing record enumeration

recordenumeration.destroy();

Creating RecordEnumeration

import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordStore;
 
public class readwriteString extends MIDlet implements CommandListener {
 
RecordStore recordstore = null;
RecordEnumeration recordenumeration = null;
Form main;
Command stratCommand, readCommand;
 
public void startApp() {
main = new Form("writeString");
 
stratCommand = new Command("Add", Command.OK, 1);
readCommand = new Command("Read", Command.OK, 2);
main.addCommand(stratCommand);
main.setCommandListener(this);
Display.getDisplay(this).setCurrent(main);
}
 
public void pauseApp() {
}
 
public void destroyApp(boolean unconditional) {
 
}
 
public void commandAction(Command c, Displayable d) {
if (c == stratCommand) {
main.removeCommand(c);
String[] data = {"FirstRecord", "SecondRecord", "ThirdRecord"};
try {
recordstore = RecordStore.openRecordStore("myRecordStore", true);
for (int i = 0; i < 3; i++) {
byte[] b = data[i].getBytes();
recordstore.addRecord(b, 0, b.length);
}
main.addCommand(readCommand);
recordstore.closeRecordStore();
} catch (Exception ex) {
}
} else if (c == readCommand) {
try {
recordstore = RecordStore.openRecordStore("myRecordStore", true);
StringBuffer buffer = new StringBuffer();
recordenumeration = recordstore.enumerateRecords(null,null,false);
while(recordenumeration.hasNextElement())
{
buffer.append(new String(recordenumeration.nextRecord()));
buffer.append("\n");
}
recordstore.closeRecordStore();
recordenumeration.destroy();
main.append(buffer.toString());
} catch (Exception ex) {
}
}
}
}

Sorting

import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordStore;
 
public class readwriteString extends MIDlet implements CommandListener {
 
RecordStore recordstore = null;
RecordEnumeration recordenumeration = null;
Comparator comparator = null;
Form main;
Command stratCommand, readCommand;
 
public void startApp() {
main = new Form("writeString");
 
stratCommand = new Command("Add", Command.OK, 1);
readCommand = new Command("Read", Command.OK, 2);
main.addCommand(stratCommand);
main.setCommandListener(this);
Display.getDisplay(this).setCurrent(main);
}
 
public void pauseApp() {
}
 
public void destroyApp(boolean unconditional) {
 
}
 
public void commandAction(Command c, Displayable d) {
if (c == stratCommand) {
main.removeCommand(c);
String[] data = {"FirstRecord", "SecondRecord", "ThirdRecord"};
try {
recordstore = RecordStore.openRecordStore("myRecordStore", true);
for (int i = 0; i < 3; i++) {
byte[] b = data[i].getBytes();
recordstore.addRecord(b, 0, b.length);
}
main.addCommand(readCommand);
recordstore.closeRecordStore();
} catch (Exception ex) {
}
} else if (c == readCommand) {
try {
recordstore = RecordStore.openRecordStore("myRecordStore", true);
StringBuffer buffer = new StringBuffer();
comparator = new Comparator();
recordenumeration = recordstore.enumerateRecords(null,comparator,false);
while(recordenumeration.hasNextElement())
{
buffer.append(new String(recordenumeration.nextRecord()));
buffer.append("\n");
}
recordstore.closeRecordStore();
recordenumeration.destroy();
main.append(buffer.toString());
} catch (Exception ex) {
}
}
}
}
 
class Comparator implements RecordComparator
{
public int compare(byte[] record1, byte[] record2)
{
String s1 = new String(record1), s2 = new String(record2);
int comparison = s1.compareTo(s2);
if(comparison == 0) return RecordComparator.EQUIVALENT;
else if(comparison < 0) return RecordComparator.PRECEDES;
else return RecordComparator.FOLLOES;
}
}

Searching

import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordStore;
 
public class readwriteString extends MIDlet implements CommandListener {
 
RecordStore recordstore = null;
RecordEnumeration recordenumeration = null;
Filter filter = null;
Form main;
Command stratCommand, readCommand;
 
public void startApp() {
main = new Form("writeString");
 
stratCommand = new Command("Add", Command.OK, 1);
readCommand = new Command("Read", Command.OK, 2);
main.addCommand(stratCommand);
main.setCommandListener(this);
Display.getDisplay(this).setCurrent(main);
}
 
public void pauseApp() {
}
 
public void destroyApp(boolean unconditional) {
 
}
 
public void commandAction(Command c, Displayable d) {
if (c == stratCommand) {
main.removeCommand(c);
String[] data = {"FirstRecord", "SecondRecord", "ThirdRecord"};
try {
recordstore = RecordStore.openRecordStore("myRecordStore", true);
for (int i = 0; i < 3; i++) {
byte[] b = data[i].getBytes();
recordstore.addRecord(b, 0, b.length);
}
main.addCommand(readCommand);
recordstore.closeRecordStore();
} catch (Exception ex) {
}
} else if (c == readCommand) {
try {
recordstore = RecordStore.openRecordStore("myRecordStore", true);
filter = new Filter("SecondRecord");
recordenumeration = recordstore.enumerateRecords(filter,null,false);
while(recordenumeration.hasNextElement())
{
String str = new String(recordenumeration.nextRecord());
main.append(str);
}
recordstore.closeRecordStore();
recordenumeration.destroy();
filter.filterClose();
} catch (Exception ex) {
}
}
}
}
 
class Filter implements RecordFilter
{
private String search = null;
private ByteArrayInputStream istream = null;
private DataInputStream dinp = null;
 
public Filter(String search)
{
this.search = search;
}
 
public boolean matches(byte[] b)
{
String s = new String(suspect).toLowerCase();
if(s != null && s.indexOf(search) != -1) return true;
else return false;
}
 
public void filterClose()
{
try{
if(istream != null) istream.close();
if(dinp != null) dinp.close();
}catch(Exception ex){}
}
}

RecordListener

Your definition of RecordListener interface must define three methods: recordAdded(), recordChanged() and recordDeleted(). To add RecordListener use:

recordstore.addRecordListener(new myRecordListener());

RecordListener:

class myRecordListener implements RecordListener
{
public void recordAdded(RecordStore recordstore, int recordid)
{
try{
/* do something */
}catch(Exception ex){
/* do something */
}
}
 
public void recordDeleted(RecordStore recordstore, int recordid)
{
try{
/* do something */
}catch(Exception ex){
/* do something */
}
}
 
public void recordChanged(RecordStore recordstore, int recordid)
{
try{
/* do something */
}catch(Exception ex){
/* do something */
}
}
}

RMS example programs

Saving High Score

Create a Record Store to store scores.

/* Create a record Store */
RecordStore RS = null;

When the app starts, copy all scores to an array and work on the array.

public void startApp()
{
try {
RS = RecordStore.openRecordStore("GameScore", true);
byte[] input = new byte[7000];
ByteArrayInputStream iStream = new ByteArrayInputStream(input);
DataInputStream dinp = new DataInputStream(iStream);
for (int i = 1; i <= RS.getNumRecords(); i++) {
RS.getRecord(i, input, 0);
scores[i - 1] = dinp.readLong();
dinp.reset();
}
dinp.close();
iStream.close();
RS.closeRecordStore();
} catch (Exception ex) {
}
}

Store the scores back to record store before exiting of game.

 public void destroyApp(boolean unconditional) {
try {
RecordStore.deleteRecordStore("GameScore");
RS = RecordStore.openRecordStore("GameScore", true);
byte[] b;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(bout);
for (int i = 0; i < 5; i++) {
out.writeLong(scores[i]);
b = bout.toByteArray();
RS.addRecord(b, 0, b.length);
out.flush();
bout.reset();
}
RS.closeRecordStore();
} catch (Exception ex) {
}
notifyDestroyed();
}

Application Setting/ User preference Storage

Create an Record Store.

private RecordStore Appsettings = null;

Check for previous password and update the record store.

/* Code sample form the application QuoteBook */
if (_AppPassword.length() > 0) {
if (_opass.getString().equals(_AppPassword)) {
if (_authchg.isSelected(0)) {
if (_npass.getString().length() > 0 && _chgpswcon.isSelected(0)) {
try {
Appsettings = RecordStore.openRecordStore("QuoteBookSettings", true);
byte[] outByte;
ByteArrayOutputStream oStream = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(oStream);
dout.writeUTF(_npass.getString());
dout.writeBoolean(true);
dout.writeUTF(_nphint.getString());
dout.writeBoolean(_askatchng.isSelected(0));
dout.writeBoolean(_askatchng.isSelected(1));
dout.writeBoolean(_askatchng.isSelected(2));
dout.writeBoolean(_askatchng.isSelected(3));
dout.flush();
outByte = oStream.toByteArray();
Appsettings.setRecord(1, outByte, 0, outByte.length);
oStream.close();
dout.close();
Appsettings.closeRecordStore();
} catch (Exception ex) {
}
_AppPassword = _npass.getString();
_AppPassHint = _nphint.getString();
_addAtt = _askatchng.isSelected(0);
_shareAtt = _askatchng.isSelected(2);
_manageAtt = _askatchng.isSelected(1);
_startupAtt = _askatchng.isSelected(3);
switchDisplayable(null, getMenu());
} else if (_npass.getString().length() == 0 && _chgpswcon.isSelected(0)) {
switchDisplayable(getPasswrd_alt(), getSettings());
} else {
try {
Appsettings = RecordStore.openRecordStore("QuoteBookSettings", true);
byte[] outByte;
ByteArrayOutputStream oStream = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(oStream);
dout.writeUTF(_AppPassword);
dout.writeBoolean(true);
dout.writeUTF(_AppPassHint);
dout.writeBoolean(_askatchng.isSelected(0));
dout.writeBoolean(_askatchng.isSelected(1));
dout.writeBoolean(_askatchng.isSelected(2));
dout.writeBoolean(_askatchng.isSelected(3));
dout.flush();
outByte = oStream.toByteArray();
Appsettings.setRecord(1, outByte, 0, outByte.length);
oStream.close();
dout.close();
Appsettings.closeRecordStore();
} catch (Exception ex) {
}
_addAtt = _askatchng.isSelected(0);
_shareAtt = _askatchng.isSelected(2);
_manageAtt = _askatchng.isSelected(1);
_startupAtt = _askatchng.isSelected(3);
switchDisplayable(null, getMenu());
}
} else {
try {
Appsettings = RecordStore.openRecordStore("QuoteBookSettings", true);
byte[] outByte;
ByteArrayOutputStream oStream = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(oStream);
dout.writeUTF("");
dout.writeBoolean(false);
dout.writeUTF("");
dout.writeBoolean(false);
dout.writeBoolean(false);
dout.writeBoolean(false);
dout.writeBoolean(false);
dout.flush();
outByte = oStream.toByteArray();
Appsettings.setRecord(1, outByte, 0, outByte.length);
oStream.close();
dout.close();
Appsettings.closeRecordStore();
} catch (Exception ex) {
}
_AppPassword = "";
_AppPassHint = "";
_addAtt = false;
_shareAtt = false;
_manageAtt = false;
_startupAtt = false;
_EAppAtt = false;
switchDisplayable(null, getMenu());
}
 
} else {
switchDisplayable(getAuthentication_error(), getSettings());
}
} else {
if (_authchg.isSelected(0)) {
if (_npass.getString().length() > 0 && _chgpswcon.isSelected(0)) {
try {
Appsettings = RecordStore.openRecordStore("QuoteBookSettings", true);
byte[] outByte;
ByteArrayOutputStream oStream = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(oStream);
dout.writeUTF(_npass.getString());
dout.writeBoolean(true);
dout.writeUTF(_nphint.getString());
dout.writeBoolean(_askatchng.isSelected(0));
dout.writeBoolean(_askatchng.isSelected(1));
dout.writeBoolean(_askatchng.isSelected(2));
dout.writeBoolean(_askatchng.isSelected(3));
dout.flush();
outByte = oStream.toByteArray();
Appsettings.setRecord(1, outByte, 0, outByte.length);
oStream.close();
dout.close();
Appsettings.closeRecordStore();
} catch (Exception ex) {
}
_AppPassword = _npass.getString();
_EAppAtt = true;
_AppPassHint = _nphint.getString();
_addAtt = _askatchng.isSelected(0);
_shareAtt = _askatchng.isSelected(2);
_manageAtt = _askatchng.isSelected(1);
_startupAtt = _askatchng.isSelected(3);
switchDisplayable(null, getMenu());
} else {
switchDisplayable(getPasswrd_alt(), getSettings());
}
}
}

UsersettingRMS.png

Multiple profile

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.security.DigestException;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordStore;
 
 
public class multipleprofile extends MIDlet implements CommandListener {
 
RecordStore rs = null;
Form welcome, addprofile, editprofile;
List profile;
TextField name, age, name1, age1;
ChoiceGroup gender, gender1;
Command addprfCommand, deleteprfCommand, editprfCommand, backCommand;
 
public void startApp() {
name = new TextField("Name", null, 32, TextField.INITIAL_CAPS_WORD);
age = new TextField("Age", null, 3, TextField.DECIMAL);
gender = new ChoiceGroup("Gender", ChoiceGroup.POPUP);
gender.append("M", null);
gender.append("F", null);
name1 = new TextField("Name", null, 32, TextField.INITIAL_CAPS_WORD);
age1 = new TextField("Age", null, 3, TextField.DECIMAL);
gender1 = new ChoiceGroup("Gender", ChoiceGroup.POPUP);
gender1.append("M", null);
gender1.append("F", null);
 
addprfCommand = new Command("Add", Command.OK, 0);
deleteprfCommand = new Command("Delete", Command.OK, 0);
editprfCommand = new Command("Edit", Command.OK, 1);
backCommand = new Command("Back", Command.BACK, 2);
 
profile = new List("Select", List.IMPLICIT);
profile.addCommand(addprfCommand);
profile.setCommandListener(this);
 
welcome = new Form("Welcome");
welcome.addCommand(backCommand);
welcome.addCommand(deleteprfCommand);
welcome.addCommand(editprfCommand);
welcome.setCommandListener(this);
 
addprofile = new Form("Add Profile");
addprofile.append(name);
addprofile.append(age);
addprofile.append(gender);
addprofile.addCommand(addprfCommand);
addprofile.addCommand(backCommand);
addprofile.setCommandListener(this);
 
editprofile = new Form("Edit");
editprofile.append(name1);
editprofile.append(age1);
editprofile.append(gender1);
editprofile.addCommand(backCommand);
editprofile.addCommand(editprfCommand);
editprofile.setCommandListener(this);
 
update();
 
}
 
public void pauseApp() {
}
 
public void destroyApp(boolean unconditional) {
}
 
public void commandAction(Command c, Displayable d) {
if (c == backCommand) {
Display.getDisplay(this).setCurrent(profile);
} else if (c == addprfCommand) {
if (Display.getDisplay(this).getCurrent() == profile) {
Display.getDisplay(this).setCurrent(addprofile);
} else {
try {
rs = RecordStore.openRecordStore("profileRMS", true);
byte[] outByte;
ByteArrayOutputStream oStream = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(oStream);
String n = name.getString();
int a = Integer.parseInt(age.getString());
String g = gender.getString(gender.getSelectedIndex());
dout.writeUTF(n);
dout.writeInt(a);
dout.writeUTF(g);
dout.flush();
outByte = oStream.toByteArray();
rs.addRecord(outByte, 0, outByte.length);
dout.close();
oStream.close();
rs.closeRecordStore();
} catch (Exception ex) {
}
update();
}
} else if (c == deleteprfCommand) {
Display.getDisplay(this).setCurrent(profile);
int no = profile.getSelectedIndex() + 1;
try {
rs = RecordStore.openRecordStore("profileRMS", true);
byte[] byt = new byte[700];
RecordStore rec = RecordStore.openRecordStore("abc", true);
for (int i = 1; i <= rs.getNumRecords(); i++) {
if (i != no) {
rs.getRecord(i, byt, 0);
rec.addRecord(byt, 0, byt.length);
}
}
rs.closeRecordStore();
RecordStore.deleteRecordStore("profileRMS");
rs = RecordStore.openRecordStore("profileRMS", true);
for (int j = 1; j <= rec.getNumRecords(); j++) {
rec.getRecord(j, byt, 0);
rs.addRecord(byt, 0, byt.length);
}
rec.closeRecordStore();
RecordStore.deleteRecordStore("abc");
rs.closeRecordStore();
} catch (Exception ex) {
}
update();
} else if (c == editprfCommand) {
if (Display.getDisplay(this).getCurrent() == welcome) {
Display.getDisplay(this).setCurrent(editprofile);
} else {
int no = profile.getSelectedIndex() + 1;
try {
rs = RecordStore.openRecordStore("profileRMS", true);
byte[] outByte;
ByteArrayOutputStream oStream = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(oStream);
String n = name1.getString();
int a = Integer.parseInt(age1.getString());
String g = gender1.getString(gender1.getSelectedIndex());
dout.writeUTF(n);
dout.writeInt(a);
dout.writeUTF(g);
dout.flush();
outByte = oStream.toByteArray();
rs.setRecord(no, outByte, 0, outByte.length);
dout.close();
oStream.close();
rs.closeRecordStore();
} catch (Exception ex) {
}
}
} else if (c == List.SELECT_COMMAND) {
int no = profile.getSelectedIndex() + 1;
try {
rs = RecordStore.openRecordStore("profileRMS", true);
byte[] input = new byte[7000];
ByteArrayInputStream iStream = new ByteArrayInputStream(input);
DataInputStream dinp = new DataInputStream(iStream);
rs.getRecord(no, input, 0);
welcome.deleteAll();
welcome.append("Welcome, " + dinp.readUTF() + "\nYour age is " + dinp.readInt() + "\nYour gender " + dinp.readUTF());
dinp.reset();
dinp.close();
iStream.close();
rs.closeRecordStore();
} catch (Exception ex) {
}
Display.getDisplay(this).setCurrent(welcome);
}
}
 
public void update() {
try {
profile.deleteAll();
rs = RecordStore.openRecordStore("profileRMS", true);
byte[] input = new byte[7000];
ByteArrayInputStream iStream = new ByteArrayInputStream(input);
DataInputStream dinp = new DataInputStream(iStream);
for (int i = 1; i <= rs.getNumRecords(); i++) {
rs.getRecord(i, input, 0);
profile.append(dinp.readUTF(), null);
dinp.reset();
}
dinp.close();
iStream.close();
rs.closeRecordStore();
} catch (Exception ex) {
}
Display.getDisplay(this).setCurrent(profile);
}
}

RMSexample1.png RMSexample2.png RMSexample3.png

Bibliography

J2ME: The Complete Reference by James Keogh - McGraw-Hill (February 27, 2003) - ISBN-10: 0072227109 | ISBN-13: 978-0072227109

Source Code

File:RMSExample.zip File:Fruitmerchant.zip File:Multipleprofile.zip

This page was last modified on 30 July 2013, at 08:17.
105 page views in the last 30 days.
×