Miniware logo
Articles / Using the Record Management System

Using the Record Management System

There is alway the case where a midlet wants to store data among various executions. This can be for example some simple settings, a user name or a password. The solution for this problem is the Record Management System. The Record Management System is a mechanism that allows a midlet to store data permanently.

The Record Management System is organized in Recordsets. Each Recordset contains records. A recordset is identified by its name. Each record is identified by an integer which is assigned automatically by the implementation. The record is actually the data we store in the Record Management System and it is sotred is the form of an array of bytes (byte[])

In order to to access a record in a recordset the recordset has to be opened. The recordset is beeing opened by invoking the public static RecordStore openRecordStore(String name, boolean create) method where the name is the name of the recodrset and the create variable if set to true creates the recordset, if it does not exist or if it set to false it throws an exception. The following line is an example of this method invocation

RecordStore scores = RecordStore.openRecordStore("mystore", true);

After finishing with manipulating with the RecordStore it has to be closed by using the closeRecordStore( ) method. The records can be added, deleted or updated by using the public int addRecord(byte[] data, int offset, int size), public byte[] getRecord(int recordId) and public void setRecord(int recordId, byte[] data, int offset, int size) methods respectively. The byte[] data is the array which we want to add, the offset is the element of the array from which the implemetation will start adding the data, size is the size of the data recordID is the identification number of the record.

In order to insert in a recordset data types other than byte[] the DataOutputStream and the ByteArrayOutputStream can be used. The following piece of code demonstrates an example of inserting a String into a recordset

ByteArrayOutputStream bos = new ByteArrayOutputStream( );
DataOutputStream os = new DataOutputStream(baos);

os.writeUTF("hello world");
os.close( );

byte[] data = bos.toByteArray( );

int id = recordStore.addRecord(data, 0, data.length);


The same technique can be used in order to retrieve a record. The pice of code demonstrates how to read a string from a record with identification number 1:

byte[] data = recordStore.getRecord(1);
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data));
String result = dis.readUTF( );
dis.close( );


The content of this page can be reproduced as long as the author and the source are mentioned. For questions please use the forum. Nikos Fotiou
Comments
(Post new comment)