Slide 4.3: Record management systems
  Slide 4.5: Programming with the RMS (cont.)
  Home


Programming with the RMS


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:
import  javax.microedition.rms.*;
RecordStore  rs = null;
try {
  rs = RecordStore.openRecordStore( "myDBfile", true );
}
catch( RecordStoreNotFoundException e ) {
  // doesn't exist
}
catch( RecordStoreException e ) {
  // some other error
}
It creates a new database file named myDBfile. The second parameter, which is set to true, says that if the record store does not exist, create it.

Creating a New Record
A record is an array of bytes. You can use the DataInputStream, DataOutputStream, ByteArrayInputStream, and ByteArrayOutputStream classes to pack and unpack data types into and out of the byte arrays. To add a record to the record store, use the addRecord( ) method as follows:
byte[ ]  data = new byte[2];
data[0] = 0;        data[1] = 1;
try {
  int  id = rs.addRecord( data, 0, data.length );
}
catch( RecordStoreFullException e ) {
  // no room left for more data
}
catch( RecordStoreNotOpenException e ) {
  // store has been closed
}
catch( RecordStoreException e ) {
  // general error
}