Handling MIDP RMS
1. RMS (Record Management System) ------ Record the storage of the data in a mobile device to make data lasting storage.
2. Record the storage ----- is a binary file consisting of each record.
3. Record (storage unit) ----- Each record is an array of bytes.
4. RMS Create a record store and add each record to the recording store. When the recording is added, each record is assigned a unique marker (record ID). RMS manages the records in the record store by the record ID. (RMS does not care about the actual content of the record). The record ID has always been saved to the record from the recording store.
5. Record the storage ---- Save under the x: / wtk104 / appdb directory, with a .db suffix.
6. The package that implements RMS is a javax.microedition.rms package.
7. There is only one RECORDStore class in the RMS package.
8. Several important methods for the RecordStore class:
1) OpenRecordStore () ----- Open Record Storage
2) ClosRecordStore () ----- Close Record Storage
3) deleteRecordStore () ----- Delete Record Storage
4) ENUMERATERECORDS () ----- Record the entire collection recorded in the storage
5) getName () ----- Get record storage name
6) getnumRecords () ----- Recording storage records
7) AddRecord () ----- Join Record
8) getRecord () ----- Retrieval record
9) DeleteRecord () ----- Delete Record
9. RMS instance
1) Task statement: SaveMymoney Bank Application requires data storage on your mobile device. After the mobile user has to add this loan record on your mobile device, you can save it as a memo. The records include loan numbers, repayment date, and number of credits.
2) Step 1: Open Record Storage
RecordStore RS = NULL;
// Open A Record Store with the given name (Open Record Storage)
Public RecordStore OpenRS (String FileName) {
Try
{
// Open FileName Record Storage, the second parameter TRUE represents Creating a new record storage if the record store does not exist //
/ / If the value is false value triggered a RecordStoreNotFoundException
RS = RecordStore.OpenRecordStore (filename, true);
}
Catch (RecordStoreException RSE)
{
Rse.printStackTrace ();
}
Return RS;
}
3) Step 2: Join the record to record storage
// Add a new record (loan) to the record store
// Each record is represented by an array of bytes, so adding a record means: add byte arrays to record storage
// Synchronized represents synchronization, the same time, guarantees that only one thread is operated on the RECORDSTORE
Public Synchronized Void AddNewloan (String Record)
{
BYTEARRAYOUTPUTSTREAM baos = new byteArrayoutputStream ();
Dataoutputstream daos = New DataOutputStream (baos);
Try {
DAOS.WRITEUTF (RECORD);
Catch (IOEXCEPTION IOE)
{
System.out.println (IOE);
IoE.PrintStackTrace ();
}
Byte [] bytearr = baos.tobytearray ();
Try
{
/ / Add to record storage, this method has three parameters, the first one is the array of bytes to be added
// Second is OFFSET (displacement) in the byte array, the number of bytes to join
Rs.addrecord (Bytearr, 0, Bytearr.Length);
}
Catch (RecordStoreException RSE)
{
System.out.println (RSE);
Rse.printStackTrace ();
}
}
4) Step 3: Delete Record Storage
// use the enumeration Object to delete Each Record in The Record
// Store Delete Record Storage
Public void deleters ()
{
Try
{
Recordenumeration re = enumerate ();
While (Re.hasNexTelement ())
{
INT ID = Re.nexTrecordID ();
Rs.deleteRecord (ID);
}
Showalert ();
}
Catch (Exception E) {}
}
5) Step 4: Close Record Storage
// Close The Record Store
Public void closers () THROWS RecordstoreNotopenexception, RecordStoreException
{
// Retrieve the total number of records in the record storage, delete this record storage if the record storage is empty
IF (rs.getnumRecords () == 0)
{
String filename = rs.getname ();
Rs.closecordstore ();
Rs.deleteRecordStore (filename);
}
Else
{
Rs.closecordstore ();
}
}
6) Step 5: Navigate records in records
// Get the Enumeration Object.
Public synchronized recordenumeration enumerate ()
Throws RecordStorenotopenexception
{
Return Rs.EnumerateRecords (Null, Null, False);
}
7) Important code is written above, below is all code:
Import javax.microedition.rms. *;
Import javax.microedition.lcdui. *;
Import javax.microedition.midlet. *;
Import javax.microedition.io. *;
Import java.util.enumeration;
Import java.util.date;
Import java.util.calendar;
Import java.io. *;
Public Class Loanmidlet Extends MIDlet ImmancelliStener
{
Display display = null;
List menu = null; // the initial menu
List choose = null;
Form form = new form ("repayment details");
// define the alert
Alert Alert = New Alert ("Message", "Cleared The Record Store Success ...", NULL, ALERTTYPE.INFO);
Static final command backcommand = new command ("back", command.back, 0);
Static Final Command MainmenuCommand = New Command ("Main", Command.Screen, 1);
Static Final Command SaveCommand = New Command ("Save", Command.ok, 2);
Static Final Command ExitCommand = New Command ("EXIT", Command.stop, 3);
String currentmenu = null;
// RecordStore Object
RecordStore RS = NULL;
// Form Components
Datefield LOANDATE = New Datefield ("Repay Date", Datefield.date;
Textfield Loannumber = New Textfield ("Loan Number", NULL, 50, TextField.Any);
Textfield Loanamount = New Textfield ("Repay Amount", NULL, 20, TEXTFIELD.NUMERIC);
Public loanmidlet () {} // constructor
// Open A Record Store with the given name (Open Record Storage)
Public RecordStore OpenRS (String FileName)
{
Try
{
// Open FileName Record Storage, the second parameter TRUE represents Creating a new record storage if the record store does not exist
/ / If the value is false value triggered a RecordStoreNotFoundException
RS = RecordStore.OpenRecordStore (filename, true);
}
Catch (RecordStoreException RSE)
{
Rse.printStackTrace ();
}
Return RS;
}
// Add a new record (loan) to the record store
// Each record is represented by an array of bytes, so adding a record means: add byte arrays to record storage
// Synchronized represents synchronization, the same time, guarantees that only one thread is operated on the RECORDSTORE
Public Synchronized Void AddNewloan (String Record)
{
BYTEARRAYOUTPUTSTREAM baos = new byteArrayoutputStream ();
Dataoutputstream daos = New DataOutputStream (baos);
Try {
DAOS.WRITEUTF (RECORD);
}
Catch (IOEXCEPTION IOE)
{
System.out.println (IOE);
IoE.PrintStackTrace ();
}
Byte [] bytearr = baos.tobytearray ();
Try
{
/ / Add to record storage, this method has three parameters, the first one is the number of bytes to be added // second is OFFSET (displacement) in the byte array, the number of bytes to join.
Rs.addrecord (Bytearr, 0, Bytearr.Length);
}
Catch (RecordStoreException RSE)
{
System.out.println (RSE);
Rse.printStackTrace ();
}
}
// use the enumeration Object to delete Each Record in The Record
// Store Delete Record Storage
Public void deleters ()
{
Try
{
Recordenumeration re = enumerate ();
While (Re.hasNexTelement ())
{
INT ID = Re.nexTrecordID ();
Rs.deleteRecord (ID);
}
Showalert ();
}
Catch (Exception E) {}
}
// Get the Enumeration Object.
Public synchronized recordenumeration enumerate ()
Throws RecordStorenotopenexception
{
Return Rs.EnumerateRecords (Null, Null, False);
}
// Close The Record Store
Public void closers ()
Throws RecordStorenotopenexception, RecordStoreException
{
// Retrieve the total number of records in the record storage, delete this record storage if the record storage is empty
IF (rs.getnumRecords () == 0)
{
String filename = rs.getname ();
Rs.closecordstore ();
Rs.deleteRecordStore (filename);
}
Else
{
Rs.closecordstore ();
}
}
// start the midlet
Public void startapp () throws MidletStateChangeException
{
Display = display.getdisplay (this);
// Open the record Source
Try
{
// Open Record Storage, create a new
OpenRS ("Myloan");
}
Catch (Exception E) {}
Menu = New List ("Loan Data", Choice.Implicit);
Menu.Append ("add", null);
Menu.Append ("View", NULL);
Menu.Append ("delete all entries", null;
Menu.addCommand (EXITCOMMAND);
Menu.setCommandListener (this);
Form.Append (LoanNumber);
Form.Append (LOAANDATE);
Form.Append (Loanamount);
Mainmenu ();
}
Public void pauseApp ()
{
Display = NULL;
choose = null;
Menu = NULL;
Form = null;
Try
{
Closers ();
}
Catch (Exception E) {}
}
Public void destroyApp (boolean unconditional) {
Try
{
Closers ();
}
Catch (Exception E) {}
NotifyDestroyed ();
}
Void mainmenu ()
{
Display.SetCurrent (MENU);
CurrentMenu = "main";
}
// Show the alert
Public void shodulert ()
{
Try
{
Alert.SetTimeout (Alert.Forever);
Display.Setcurrent (Alert);
}
Catch (Exception E) {}
}
// Convert Date to string
// the date format used is mm / dd / yyyy date conversion to string type
Public String DateTostr (Date Dateval)
{
Calendar Calendar = Calendar.getInstance ();
Calendar.SetTime (DateVal);
String strdate = integer.tostring (calendar.day_of_month);
String strMonth = integer.tostring (Calendar.Mont) 1); string stryear = integer.tostring (Calendar.get (Calendar.year);
Return strMonth "/" strdate "/" strsyear;
}
// Show the Form
Public void addloan ()
{
Form.Addcommand (SaveCommand);
form.addcommand (backcommand);
Form.setCommandListener (this);
Display.SetCurrent (Form);
CurrentMenu = "add";
}
// list the repayment details in the record store lists the repayment record information
Public void ListItems ()
{
Choose = New List ("Repayment List", choice.implicit;
Choose.addcommand (Backcommand);
Choose.setCommandlistener (this);
Try
{
Recordenumeration re = enumerate ();
While (Re.hasNexTelement ())
{
String thelist = new string (re.nexTrecord ());
Choose.Append (theList, NULL);
}
}
Catch (Exception E) {}
Display.setcurrent (choose);
CurrentMenu = "List";
}
// Handle Command Events
Public void CommandAction (Command C, Displayable D)
{
String label = c.getlabel ();
Label.equals ("exit"))
{
DESTROYAPP (TRUE);
}
Else IF (Label.equals ("Save")) {
CurrentMenu.equals ("add")))
{
// add it to record store
Try
{
String strnumber = loannumber.getstring ();
String stramount = loanamount.getstring ();
String strdate = DATETOSTR (Loandate.getdate ());
// Create the new record by contatenating the inputs
String strresult = strnumber ";" stramount ";" strdate;
// add it to the record store
AddNewloan (Strresult);
}
Catch (Exception E) {}
Mainmenu ();
}
}
ELSE IF (Label.equals ("back"))
{
CurrentMenu.equals ("List")))
{
// go back to menu
Mainmenu ();
}
Else IF (CurrentMenu.equals ("add")))
{
// go back to menu
Mainmenu ();
}
}
Else
{
List down = (list) display.getcurrent ();
Switch (Down.getSelectedIndex ())
{
Case 0: addloan ();
Case 1: ListItems (); Break;
Case 2: Deleters (); Break;
}
}
}
}
The test results are as follows:
Main interface
Add record
Add ADD, save
VIEW record
Delete all records