RecordStore with examples - I

14 May 2008

I will use several examples to show how to use RecordStrore in J2ME applications.

Creating record store is simple. We use a static method to create or open an existing record store.

public static RecordStore openRecordStore(String recordStoreName,
                                          boolean createIfNecessary)
                                   throws RecordStoreException,
                                          RecordStoreFullException,
                                          RecordStoreNotFoundException

Exception handling is very important. Note the exceptions thrown by the openRecordStore(…) method. Use the catch blocks to define the alternative flow.

Example:

try {
System.out.println("creating data base");
recordStore = RecordStore.openRecordStore(RECORD_STORE_NAME,
true);
} catch (RecordStoreException rse) {
System.err.println(rse.toString());
}

After creating/opening the record store, we can add elements in it. RecordStore provides the following method to add records. Thing to note is that we have to provide the data to be added in form of byte array.

int addRecord(byte[] data, int offset, int numBytes)

Example:

// dataBuf is a byte array with the required data
try {
recordID = recordStore.addRecord(dataBuf, 0, dataBuf.length);
 
} catch (RecordStoreException rse) {
System.err.println(rse.toString());
try {
recordStore.deleteRecord(recordID);
} catch(Exception ex) {
System.err.println(ex.toString());
}
}

continued …

del.icio.us:RecordStore with examples - I  digg:RecordStore with examples - I  spurl:RecordStore with examples - I  wists:RecordStore with examples - I  simpy:RecordStore with examples - I  newsvine:RecordStore with examples - I  blinklist:RecordStore with examples - I  furl:RecordStore with examples - I  reddit:RecordStore with examples - I  fark:RecordStore with examples - I  blogmarks:RecordStore with examples - I  Y!:RecordStore with examples - I  smarking:RecordStore with examples - I  magnolia:RecordStore with examples - I  segnalo:RecordStore with examples - I  gifttagging:RecordStore with examples - I

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: RecordStore with examples - I

Leave a Reply