MIDP Database Programming Using RMS: a Persistent Storage for MIDlets

Chapter 6 - MIDP Persistent Storage

    MIDP Database Programming Using RMS: a Persistent Storage for MIDlets

  • Introducing the RMS

              The MIDP provides a mechanism for MIDlets to persistently store data and retrieve it later.It is a simple record-oriented database called the Record Management System (RMS).

              A MIDP database or a record store consists of a collection of records that persistent after the MIDlet exits.

              To use the RMS, import the javax.microedition.rms package.

    The Record Store

              Record stores (binary files) are platform-dependent because they are created in platform-dependent locations. MIDlets within a single application (a MIDlet suite).

              It can create multiple record stores (database files) with different names. The RMS APIs provide the following functionality:

                   •  Allow MIDlets to manipulate (add and remove) records within a record store.

                   •  Allow MIDlets in the same application to share records.

                   •  Do not provide a mechanism for sharing records between MIDlets in different applications.

    Record Store Names

              Record store names are case sensitive, and cannot be more than 32 characters long.A MIDlet cannot create two record stores with the same name in the same application, but it can create a record store with the same name as a MIDlet in another application.

    The RMS Package

              The RMS package consists of the following four interfaces, one class, and five exception classes:

    Interfaces

                   •  RecordComparator: Defines a comparator to compare two records.

                   •  RecordEnumeration: Represents a bidirectional record enumerator.

                   •  RecordFilter: Defines a filter to a record and checks if it matches based on a criteria defined by the application.

                    •  RecordListener: Receives records which were added, changed, or deleted from a record store. Classes

                    •  RecordStore: Represents a record store.

    Working with Threads

              The MIDP RMS implementation ensures that all individual record store operations are atomic, synchronous, and serialized, so no corruption occurs with multiple access.

              MIDlets use multiple threads to access a record store, it is synchronize this access, or some of your records might be overwritten.

    Exceptions

                   •  InvalidRecordIDException: Thrown to indicate the RecordID is invalid.

                   •  RecordStoreException: Thrown to indicate a general exception was thrown.

                   •  RecordStoreFullException: Thrown to indicate the record store file system is full.

                   •  RecordStoreNotFoundException: Thrown to indicate the record store could not be found.

                   •  RecordStoreNotOpenException: Thrown to indicate an operation on a closed record store.

    Record Store

                   •  A record store consists of a collection of records that are uniquely identified by their record ID, which is an integer value.

              The record ID is the primary key for the records. The first record has an ID of 1, and each additional record is assigned an ID that is the previous value plus 1.

    Opening a Record Store

                   •  To open a record store, use the openRecordStore() static method:

    RecordStore db = RecordStore.openRecordStore("myDBfile", true);
    
    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);
            }
        }

© 2015 by Learncertification All Rights Reserved. The certification names are the trademarks of their respective owners. Terms & Privacy Policy