Basic RMS Example

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import java.io.*;

public class RMSDemo extends MIDlet implements CommandListener {

private Display display;
private RecordStore rs=null;
private Command exit;
private RecordEnumeration re;
private int recordNO;
Form frm;
int index=0;
public RMSDemo() {
display = Display.getDisplay(this);

//Create a RMS
try {
rs= RecordStore.openRecordStore("myRecord",false);
rs.closeRecordStore();
} catch(Exception e) {
System.out.println(e);
}

}

public void startApp() {

frm=new Form("RMSDemo");

exit= new Command("Exit",Command.EXIT,1);
frm.addCommand(exit);

add= new Command("Add",Command.SCREN,1);
frm.addCommand(add);

delete= new Command("Delete",Command.SCREEN,2);
frm.addCommand(delete);

show= new Command("SHOW",Command.SCREEN ,3);
frm.addCommand(show);

frm.setCommandListener(this);
frm.append("#####");
display.setCurrent(frm);
}

public void pauseApp() {

}

public void destroyApp(boolean un) {
}

// Handling commands
public void commandAction(Command cmd,Displayable d) {
if(cmd==add) {
addRecord();
} else
if(cmd==delete) {
removeRecord();
} else
if(cmd==show) {
try {
byte b[]= rs.getRecord(recordNO);
String s= new String(b);
frm.append(s);
} catch(Exception e) {}
}
}


void addRecord() {
try {
rs= RecordStore.openRecordStore("myRecord",false);
index++;
byte b[]=("Record NO "+index).getBytes();
//Adding record to record store
rs.addRecord(b,0,b.length);
rs.closeRecordStore() ;
} catch(Exception e) {
System.out.println(e);
}

}

// Deleting a record
void removeRecord(int recordID) {
try {
rs= RecordStore.openRecordStore("myRecord",false);
rs.deleteRecord(recordID);
index--;
rs.closeRecordStore();
} catch(Exception e) {
System.out.println(e);
}
}


}

18 comments:

  1. how to apply it to an application. i mean how to capture information to b store from a form

    ReplyDelete
  2. How to Connect a database with J2ME

    ReplyDelete
    Replies
    1. which database r u using. is it online or offline

      Delete
  3. All programms coding wrong,y r u puuting coding in net

    ReplyDelete
  4. where does the location of "my record" ?

    ReplyDelete
  5. im using record store in my progra when i delete something from my record i am not able to save the

    ReplyDelete
  6. please provide some detail line above code for what that particular code id use.

    ReplyDelete
  7. simply purchase a Book and study it throughly... got it !!!

    ReplyDelete
  8. i have developed a form whic has a textfield of username n date of birth can istor it in mobile ???is it possible please help me out in this part of problem

    ReplyDelete
  9. Does exactly what it say's on the tin!
    Basic RMS example - you need to build the fence, then paint it!

    ReplyDelete
  10. simply purchase a Book and study it throughly... got it

    ReplyDelete
  11. Thats good!!!!!!!!!!!!!11

    ReplyDelete
  12. you've gotta correct this code

    ReplyDelete
  13. C:\Documents and Settings\Administrator\j2mewtk\2.5.2\apps\RMSDemo\src\RMSDemo.java:57: '.class' expected
    addRecord(int);
    ^
    1 error
    com.sun.kvem.ktools.ExecutionException
    Build failed

    ReplyDelete
  14. this code has a lot of errors, here is the corrected version:
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.rms.*;
    import java.io.*;

    public class RMSDemo extends MIDlet implements CommandListener {

    private Display display;
    private RecordStore rs=null;
    private Command exit,add,delete,show;
    private RecordEnumeration re;
    private int recordNO,recordID;
    Form frm;
    int index=0;
    public RMSDemo() {
    display = Display.getDisplay(this);

    //Create a RMS
    try {
    rs= RecordStore.openRecordStore("myRecord",false);
    rs.closeRecordStore();
    } catch(Exception e) {
    System.out.println(e);
    }

    }

    public void startApp() {

    frm=new Form("RMSDemo");

    exit= new Command("Exit",Command.EXIT,1);
    frm.addCommand(exit);

    add= new Command("Add",Command.SCREEN,1);
    frm.addCommand(add);

    delete= new Command("Delete",Command.SCREEN,2);
    frm.addCommand(delete);

    show= new Command("SHOW",Command.SCREEN ,3);
    frm.addCommand(show);

    frm.setCommandListener(this);
    frm.append("#####");
    display.setCurrent(frm);
    }

    public void pauseApp() {

    }

    public void destroyApp(boolean un) {
    }

    // Handling commands
    public void commandAction(Command cmd,Displayable d) {
    if(cmd==add) {
    addRecord();
    } else
    if(cmd==delete) {
    removeRecord();
    } else
    if(cmd==show) {
    try {
    byte b[]= rs.getRecord(recordNO);
    String s= new String(b);
    frm.append(s);
    } catch(Exception e) {}
    }
    }


    void addRecord() {
    try {
    rs= RecordStore.openRecordStore("myRecord",false);
    index++;
    byte b[]=("Record NO "+index).getBytes();
    //Adding record to record store
    rs.addRecord(b,0,b.length);
    rs.closeRecordStore() ;
    } catch(Exception e) {
    System.out.println(e);
    }

    }

    // Deleting a record
    public void removeRecord() {
    try {
    rs= RecordStore.openRecordStore("myRecord",false);
    rs.deleteRecord(recordID);
    index--;
    rs.closeRecordStore();
    } catch(Exception e) {
    System.out.println(e);
    }
    }


    }

    ReplyDelete
  15. how to retrieve datasets in j2me app from sql database server??

    ReplyDelete
  16. I made some minor changes to this example to get it working for my application. Here's the snips for getting a new RMS to load into your phone app:

    The integer parameter is for the record number that the addRecord would create:

    public String ReadRMSSettings(int r)
    {
    try
    {
    rs = RecordStore.openRecordStore("METARsRMS", true);
    // if there are no records in the RMS then lets ADD one
    if(rs.getNumRecords() == 0)
    {
    // SETTING_DEFAULT_STATION
    rs.addRecord(rsBytes, 0, rsBytes.length);
    }
    // now reading that record
    ret = rs.getRecord(r, rsBytes, 0);
    tempString = new String(rsBytes);
    rs.closeRecordStore();
    }
    catch(Exception ex)
    {
    Error(ex.toString());
    return "";
    }

    return tempString;
    }

    and some code to update the RMS record

    public void UpdateRMSSettings(String s, int r)
    {
    try
    {
    rs = RecordStore.openRecordStore("METARsRMS", true);
    rsBytes = s.getBytes();
    rs.setRecord(r, rsBytes, 0, rsBytes.length);
    rs.closeRecordStore();
    }
    catch(Exception ex)
    {
    Error(ex.toString());
    }
    }

    calling the methods

    public static final int SETTING_DEFAULT_STATION = 1;

    tempString = ReadRMSSettings(SETTING_DEFAULT_STATION);

    UpdateRMSSettings(station.getString(), SETTING_DEFAULT_STATION);

    ReplyDelete
    Replies
    1. btw: when the RMS is empty and you add the first record to it its record number is one and with each add it adds one to the record number. So, if you wanted to save 10 settings loop the add 10 times and then have constants for each of the 10 records ie. SETTING_DEFAULT_STATION = 1.

      Delete

 

Design by Blogger Buster | Distributed by Blogging Tips