Record Management System in J2ME
- Every J2ME application that we develop requires persistence.
- Persistence is the retention of information during operation of the MIDlet and when it is not running.
- MIDP provides a mechanism for MIDlets to persist data so it can be used in later executions of the MIDlet, or to be shared among MIDlets. This mechanism is known as a Record Management System.
- Many small computing devices lack disk drives and access to a network database server or file server, which are the typical resources used for persistence in J2SE and J2EE applications.
- J2ME applications store information in nonvolatile memory using the Record Management System (RMS).
- RMS is an application programming interface that is used to store and manipulate data in a small computing device using a J2ME application.
Need for Record Management System:
- Not all small computing devices have a file system and therefore are unable to store information in the manner that you are accustomed to when working with a PC, server, and other traditional computing devices.
- The Record Management System provides a file system–like environment that is used to store and maintain persistence in a small computing device
- RMS is a combination file system and database management system that enables you to store data in columns and rows similar to the organization of data in a table of a database.
- Although RMS provides database functionality, RMS is not a relational database, and therefore we cannot use SQL to interact with the data.
The Record Store and Its Scope
- RMS stores information in a record store. A record store compares to a flat file used for data storage in a traditional file system and to a table of a database.
- A record store is a collection of records organized as rows (records) and columns (fields).
- Columns contain like data such as first name. Rows contain related data such as a first name, middle name, last name, street, city, state, and postal code.
- RMS automatically assigns to each row a unique integer that identifies the row in the record store, which is called the record ID.
Organizing Data in Rows and Columns:
- The record ID is in its own column within the record store. The record ID is considered the primary key of the record store.
- A primary key of the record store serves the same purpose as a primary key in a table of a database, which is to uniquely identify each record in a table.
- Although conceptually you can envision a record store as rows and columns, technically there are two columns.
- The first column is the record ID, and the other column is an array of bytes that contains the persistent data
Scope of Record Store:
- We can create multiple record stores as required by your MIDlet as long as the name of each record store is unique.
- The name of a record store must be a minimum of one character and not more than 32 characters. Characters are Unicode, and the name is case sensitive.
- Record stores can be shared among MIDlets that are within the same MIDlet suite as shown in the below figure:

- Record stores must be uniquely named within a MIDlet suite, although duplicate names can be used for record stores in other MIDlet suites.
Setting up a Record Store
- The openRecordStore() method is called to create a new record store and to open an existing record store.
- It either creates or opens a record store depending on whether the record store already exists within the MIDlet suite.
- The openRecordStore() method requires two parameters:
- The first parameter is a string containing the name of the record store.
- The second parameter is a boolean value indicating whether the record store should be created if the record store doesn’t exist.
- A true value causes the record store to be created if the record store isn’t in the MIDlet suite and also opens the record store. A false value does not create the record store if the record store isn’t located.
- The second version of the openRecordStore() method useful whenever we MIDlet tries to open an existing record store.
- We may don't want our MIDlet to create a new record store if for some reason the record store is unavailable when you tried to open it because another MIDlet creates and maintains the record store
Closing a Record Store:
- Internal resources are utilized to make an open record store available to MIDlets within a MIDlet suite.
- Thus, always close any record store that is not in use so that resources utilized by the record store can be reused by other processes
- We can close a record store by calling the closeRecordStore() method. The closeRecordStore() method does not require any parameters.
- A record store remains in nonvolatile memory even after the small computing device is powered down.
Removing a Record Store from Memory:
- Nonvolatile memory is a scarce resource that needs to be properly managed to ensure that sufficient memory is available when required to store information collected by a MIDlet.
- We can help manage nonvolatile memory by removing all record stores that are no longer being used by MIDlets running on the device.
- A record store can be deleted by calling the deleteRecordStore() method. This method requires one parameter, which is a string containing the name of the record store that is to be removed from the device.
MIDlet to Check if a Record Store Already Exists or Not:
import javax.microedition.rms.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
class RecordStoreMidlet extends MIDlet implements CommandListener
{
private Display display;
private Alert alert;
private Form form;
private Command exit;
private Command openStore;
private Command newStore;
private Command clsStore;
private Command delStore;
private RecordStore recordstore = null;
private RecordEnumeration recordenumeration = null;
private StringItem st;
private TextField txtStoreName;
public RecordStoreMidlet ()
{
display = Display.getDisplay(this);
st = new StringItem("","");
txtStoreName = new TextField("Name of the Store", "", 31, 0);
openStore = new Command("Open Store", Command.SCREEN, 1);
newStore = new Command("Create Store", Command.SCREEN, 2);
exit = new Command("Exit App", Command.SCREEN, 5);
form = new Form("Record Store");
form.append(txtStoreName);
form.addCommand(openStore);
form.addCommand(newStore);
form.addCommand(exit);
form.setCommandListener(this);
}
public void startApp()
{
display.setCurrent(form);
}
public void pauseApp()
{
}
public void destroyApp( boolean unconditional )
{
}
public void commandAction(Command command, Displayable displayable)
{
if (command == exit)
{
destroyApp(true);
notifyDestroyed();
}
else if (command == newStore)
{
try
{
recordstore = RecordStore.openRecordStore(txtStoreName.getString(), true );
st.setText("The Record Store Exists and Open for Storage");
form.append(st);
}
catch (Exception error)
{
alert = new Alert("File Status!", error.toString(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
}
else if (command == openStore)
{
try
{
recordstore = RecordStore.openRecordStore(txtStoreName.getString(), false );
st.setText("The Record Store Exists and Open for Storage and Retrieval");
form.append(st);
}
catch (Exception error)
{
st.setText("The Record Store Does not Exist. Create a New One!");
alert = new Alert("File Status!", st.getText(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
form.append(st);
}
}
}
}