RMS Basics in J2ME

By: Daniel Malcolm Emailed: 1677 times Printed: 2157 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Each MIDP-compliant device maintains a dedicated area of device memory for persistent application data storage. MIDlet data stored here persists across multiple invocations of the applications that use it. Both the physical location and the size of the data store are device dependent.

The RMS API abstracts the device-dependent details of the storage area and access to those details, and it provides a uniform mechanism to create, destroy, and modify data. This ensures portability of MIDlets to different devices.

RMS Data Storage Model

The RMS supports the creation and management of multiple record stores, shown in Figure below. A record store is a database whose central abstraction is the record. Each record store consists of zero or more records. A record store name is case sensitive and can consist of a maximum of 32 Unicode characters. A record store is created by a MIDlet.


MIDlets within the same MIDlet suite can share one another's record stores. A MIDlet suite defines a name space for record stores; a record store must have a unique name within a MIDlet suite. The same name can be used in different MIDlet suites, however.

MIDlets can list the names of all the record stores available to them. They can also determine the amount of free space available for storing data. Incidentally, you should be aware that when all MIDlets in a MIDlet suite are removed from a device, the device AMS removes all record stores in the MIDlet suite namespace. All persistent data will be lost. For this reason, you should consider designing applications to include a warning or confirmation that requires users to acknowledge that they understand the potential loss of data when they remove applications. Applications might also include a mechanism to back up the records in a data store to another location. This might require server side support.

The RMS defines the following conceptual operations on an individual record store:

• Add a record.
• Delete a record.
• Change a record.
• Look up (retrieve) a record.
• Enumerate all records.

Records are uniquely identified by a record ID, which is the only primary key type supported. The type of all record ids is the Java built-in type int. The RMS has no support for features—such as tables, rows, columns, data types, and so forth—that are present in relational databases.
 

Records

A record is a byte array of type byte []. The RMS doesn't support the definition or formatting of fields within a record. Your application must define the data elements within a record and their format.

The reader of a record, therefore, must be aware of the format that was used to write the record. Because a record is simply a byte array, applications must convert data from arbitrary types to bytes when writing records, and they must convert from bytes to those types upon reading the data.
 

Here is a sample code that shows simple read and write of RMS in J2ME

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

public class ReadWriteRMS extends MIDlet{
  private RecordStore rs = null;
  static final String REC_STORE = "ReadWriteRMS";

  public void startApp(){
    openRecStore();
    writeRecord("Core J2ME Technology");
    writeRecord("J2ME Wireless Toolkit");    
    readRecords();
    closeRecStore();
    deleteRecStore(); 
  }

  public void pauseApp(){}

  public void destroyApp(boolean unconditional){
    notifyDestroyed();
  }

  public void openRecStore(){
    try{
      rs = RecordStore.openRecordStore(REC_STORE, true );
    }catch (Exception e){}
  }    

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

  public void deleteRecStore(){
    if (RecordStore.listRecordStores() != null){
      try{
        RecordStore.deleteRecordStore(REC_STORE);
      }catch (Exception e){}
    }      
  }

  public void writeRecord(String str){
    byte[] rec = str.getBytes();
    try{
      rs.addRecord(rec, 0, rec.length);
    }catch (Exception e){}
  }

  public void readRecords(){
    try{
      byte[] recData = new byte[5]; 
      int len;
      
      for(int i = 1; i <= rs.getNumRecords(); i++){
        if(rs.getRecordSize(i) > recData.length){
          recData = new byte[rs.getRecordSize(i)];
        }
        len = rs.getRecord(i, recData, 0);
        System.out.println("------------------------------");
        System.out.println("Record " + i + " : " + new String(recData, 0, len));
        System.out.println("------------------------------");                        
      }
    }catch (Exception e){}
  }
}

J2ME Home | All J2ME Tutorials | Latest J2ME Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(16)


1. View Comment

Just what i was looking for. tku.

View Tutorial          By: Chow Lim at 2008-03-21 01:46:52
2. View Comment

rms programe

View Tutorial          By: hardik at 2008-03-28 23:06:58
3. View Comment

Hi hardik,

RMS sample program is available at <a href="http://www.java-samples.com/showtutorial.php?tutorialid=303">RMS Sample Program</a>


View Tutorial          By: Daniel Malcolm at 2008-03-29 19:46:18
4. View Comment

If i delete a record, will that recId be replaced by next one.
more clearly, if the user deletes 3rd record from RMS, will the fourth one come & occupy position 3?


View Tutorial          By: faisal at 2009-06-08 21:36:29
5. View Comment

I will try

View Tutorial          By: bridges at 2009-09-06 22:09:46
6. View Comment

can you give me tutorial how to copy phonebook to RMS???please :)

View Tutorial          By: rico at 2010-04-04 21:07:57
7. View Comment

I want to enter data from a textbox to recordstore and print it again on the emulator screen Please help me.Can anybody provide the revelant code

View Tutorial          By: Rahul Maurya at 2010-04-10 01:04:51
8. View Comment

hie
can u plz tell how i maintain my rms for storing my incoming message in milet suite???itz urgent..........reply me on diyagupta66@gmail.com


View Tutorial          By: diya at 2010-04-30 01:38:06
9. View Comment

i will make to try for rms :)

View Tutorial          By: sebo at 2010-05-20 07:38:28
10. View Comment

very brief and easy to understand description of RMS

View Tutorial          By: moona at 2010-05-26 22:49:26
11. View Comment

can anybody hp me how to create a rms database in j2me and how it worls

View Tutorial          By: contractornimesh at 2011-01-04 00:12:08
12. View Comment

How can i update status 1 and 0 in RMS ?
and then update this status in mysql database also ?
help needed plz


View Tutorial          By: maaz at 2011-06-16 02:54:28
13. View Comment

this is really helpful example for Learning RMS in J2ME

View Tutorial          By: Ahmed Nawaz Mari at 2011-06-30 05:29:18
14. View Comment

very nice example i really like it

View Tutorial          By: radha at 2012-03-15 17:50:52
15. View Comment

what is the output

View Tutorial          By: Raja at 2012-12-28 11:47:48
16. View Comment

Easy to understand... thank u....

View Tutorial          By: saranya at 2013-02-11 10:37:07

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Daniel Malcolm
javac options in Java
Operator Precedence in Java
Calling Multiple Listeners in JSF
Using free() Function in C
ForwardAction in Struts
Listing Files and Directories sample program in C
Binary Tree - (Self-referential Structures) example program in C
A simple program using EL in JSP
Command-line Arguments in C
Example Calculator program in C - describing use of External Variables in C
Assignment Operators and Expressions in C
The for statement in C
JSF Basics
assert() Versus Exceptions in C++
RMS Basics in J2ME

More Tutorials in J2ME
Code sample to Send SMS from a J2ME application.
Adding your own Application icon for your J2ME application (jar file)
Play a multimedia file in J2ME Program (Audio/Video) using MMAPI
Datagrams in J2ME (UDP Programming sample)
Client Server in J2ME (Socket Programming sample)
Using HttpConnection in J2ME (Retrieve web content from a website to a phone)
Using HTTP vs UDP vs Socket in J2ME
RMSCookieConnector - Using Cookies in J2ME
POST UTF-8 encoded data to the server in J2ME
lists, forms, choices, gauges, text fields, text boxes in J2ME
Using List to create a Menu and Menu items in J2ME
Using alerts and tickers in J2ME
J2ME Canvas sample to show games programming in J2ME
Timer and TimerTask example in J2ME
List of GPRS Access points for all countries

More Latest News
Most Viewed Articles (in J2ME )
Client Server in J2ME (Socket Programming sample)
TextBox sample program in J2ME
RMS Basics in J2ME
Download a file over a network in J2ME midlet
Sample Java program shows how to Read a file over a network using J2ME midlet
Types of Profiles in J2ME
A sample J2ME Auto-resume program.
Play a multimedia file in J2ME Program (Audio/Video) using MMAPI
Getting Started with J2ME
Code sample to Send SMS from a J2ME application.
How to load J2ME applications to the IDEN handsets
'LINK.EXE' is not recognized as an internal or ext
Sample J2ME code that shows various functionality of RMS.
Sample program to draw a rectangle in J2ME
Sample program to draw a arc in J2ME
Most Emailed Articles (in J2ME)
Gauge sample program in J2ME
How to load J2ME applications to the IDEN handsets
Ticker sample program in J2ME
StringItem sample program in J2ME
Sample program to draw a arc in J2ME
Timer and TimerTask example in J2ME
What is J2ME?
'LINK.EXE' is not recognized as an internal or ext
A sample J2ME midlet that shows an example of using a Timer and a TimerTask.
GUI components and menu based J2ME Applications.
How to get the CLDC and MIDP version from a J2ME program
paint() sample program to draw a line in J2ME
Sample program to draw a rectangle in J2ME
RMS Basics in J2ME
Types of Profiles in J2ME