Page 1 of 15 Dr.
Mwakondo PhD (Computer Science) UoN
Topic 6: Persistence Storage
-Introduction to Small Device Database
-Managing Record Stores
-Manipulating Records
-MIDlet Example1
-MIDlet Example2
-Exercise
Page 2 of 15 Dr. Mwakondo PhD (Computer Science) UoN
6.1 Introduction to Small Device Database
–Persistent storage means capability to store data beyond
life of running application i.e. fancy term for database
–In MIDP, persistent storage is centered around record
store
–A record store is a small database structure containing
small pieces of data called records.
–A record store is identified by a name and must be
unique within a MIDlet suite.
–A MIDlet suite is a collection of MIDlets
–A record store may be limited to a single MIDlet suite or
shared between MIDlet suites.
Page 3 of 15 Dr. Mwakondo PhD (Computer Science) UoN
6.2 Managing Record Stores
Page 4 of 15 Dr. Mwakondo PhD (Computer Science) UoN
6.2 Managing Record Store
Record store object is implemented by RecordStore
class that represents each record store included in the
MIDlet.
RecordStore class defines API that serves two
purposes:
oAPI for manipulating individual records
oAPI for managing record stores
a. Opening Record store
To open a record store two things must be supplied:
Name -label that identifies record store
CreateIfNecessary -flag which determines whether
record store will be created or not, if the record
Page 5 of 15 Dr. Mwakondo PhD (Computer Science) UoN
store does not exist. (when true create if does not
exist and when false don’t create)
Format:
RecordStore rsVariable = RecordStore.openRecordStore
(“Name”, CreateFlag);
e.g.
RecordStore rs = RecordStore.openRecordStore
(“Address”,true);
b. Closing Record store
Its good idea to close record stores when done with
them:
Format:
rsVariable.closeRecordStore();
e.g.
rs.closeRecordStore ();
Page 6 of 15 Dr. Mwakondo PhD (Computer Science) UoN
c. Removing Record store
To remove record store with its associated records:
Format:
rsVariable.deleteRecordStore();
e.g.
rs.deleteRecordStore ();
d. Sharing Record store
To share record store to many MIDlet suites two
things must be supplied:
Authorization mode –three modes of access are
oAUTHMODE_PRIVATE – default
oAUTHMODE_ANY – Shared
Writable mode -two modes:
oTrue – writable
Page 7 of 15 Dr. Mwakondo PhD (Computer Science) UoN
oFalse – not writable
Record store can be shared during opening when it
none-existed or after:
Format:
RecordStore rsVariable = RecordStore.openRecordStore
(“Name”, true, AUTHMODE, writableFlag);
e.g.
RecordStore rs = RecordStore.openRecordStore
(“Address”,true,RecordStore.AUTHMODE_ANY,true);
OR
Format:
rsVariable.setMode(AUTHMODE, writableFlag);
e.g. rs.setMode(RecordStore.AUTHMODE_ANY,true);
Note:
Page 8 of 15 Dr. Mwakondo PhD (Computer Science) UoN
Most of these methods throw exceptions and need to be
used within trycatchexception block.
To access a shared record store, you need to know its
name, the name of the MIDlet suite that created it, and
the name of the MIDlet suite’s vendor. These names
must be the MIDlet-Name and MIDlet-Vendor attributes
in the MIDlet suite JAR manifest or the
applicationdescriptor.
Other Member Methods:
a) rs.getSize();//returns records size in byte
b) rs.getSizeAvailable();//returns total space
c) rs.getVersion();//returns version number
Page 9 of 15 Dr. Mwakondo PhD (Computer Science) UoN
6.3 Manipulating Records
–A record is a uniquely identified collection of data stored
as an array of bytes.
–A record has two components:
integer identifier
byte array of data
–A record store is populated with specific number of
records which can be manipulated through:
adding records
retrieving records
deleting records
replacing records
Page 10 of 15 Dr. Mwakondo PhD (Computer Science) UoN
a. Adding Record
To add a record in an existing recordstore two things
must be done:
Create the data
Convert the data into byte array
Page 11 of 15 Dr. Mwakondo PhD (Computer Science) UoN
Invoke addRecord method of the recordstore that
adds the record and returns the record id.
Format:
Datatype Variable = dataValue;
Byte[] data = Variable.getBytes();
int RecordID = rsVariable.addRecordS (data,
startindex,endindex);
e.g.
String myname = “Fullgence Mwakondo”;
Byte[] recordData = myname.getBytes();
Int RecordID = rs.addRecord(recordData,0,
recordData.length);
b. Retrieving Record
To retrieve a record in an open recordstore supply the
record id to the getRecord method of the recordstore.
Page 12 of 15 Dr. Mwakondo PhD (Computer Science) UoN
This returns the record in bytes array.
Format:
Bytes[] recordData = rsVariable.getRecord(RecordID);
e.g.
Bytes[] record=rs.getRecord(2);
c. Deleting Record
To remove record from an open recordstore by
supplying record id to the deleteRecord method of the
recordstore:
Format:
rsVariable.deleteRecord(RecordID);
e.g.
rs.deleteRecords(3);
Other Member Methods:
a)rs.getNextRecordID();//returns next record id
b)rs.getNumRecords();//returns total no. records
Page 13 of 15 Dr. Mwakondo PhD (Computer Science) UoN
6.4 Listening to Record Changes
–Interested objects can listen to a recordstore changes by
registering themselves as listeners to those changes.
–To register as recordstore listener objects must add
recordlisteners using the method below:
addRecordListener(RecordListener Listener)
–To deregister as recordstore listener objects must remove
Recordlisteners using the method below:
removeRecordListener(RecordListener Listener)
–Recordlistener listens to three types using three methods:
addRecord() – listens to add in record
changedRecord() – listens to change in record
deleteRecord() – listens to delete in record
Page 14 of 15 Dr. Mwakondo PhD (Computer Science) UoN
6.5 Performing Recordstore Queries
–Writing instructions to pull out just records that you
want from the recordstore: use enumerateRecords()
method:
–enumerateRecords() method returns a sorted subset of
the records in the recordstore:
–enumerateRecords() method has three components
namely:
RecordFilter – determines which records to include in
the subset
RecordComparator – used to sort them out
RecordEnumeration – allows you to navigate through
the returned records
a. RecordFilter
Page 15 of 15 Dr. Mwakondo PhD (Computer Science) UoN
b. RecordComparator
c. RecordEnumertion