View RSS Feed

My Java Tips

RecordStore with examples

Rate this Entry
by , 11-07-2011 at 06:15 PM (1566 Views)
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.

Java Code:
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:

Java Code:
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.

Java Code:
int addRecord(byte[] data, int offset, int numBytes)
Example:

Java Code:
// 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());
}
}


As shown above you can only add data in form of byte array into RecordStore. This means you have to write a helper method which inserts a record containing a String into the given recordStore. The following methods does exactly that. It takes 2 parameters: str is the String to be added into the RecordStore and rStore is the RecordStore. In which data has to be added.

Java Code:
    private int insertString(String str, RecordStore rStore){
        byte[] buff = new byte[64];
        int rID = 0;

        /*
         * Set the first element of the record to 'S' to identify
         * this record as a String.
         */
        buff[0] = (byte)'S';
        int length = asBytes(str, buff, 1);

        /*
         * Insert the string into the database.
         */
        try {
            System.out.println( "writing S " + str);
            rID = rStore.addRecord(buff, 0, buff.length);

        } catch (RecordStoreException rse) {
            System.err.println(rse.toString());
            try {
                rStore.deleteRecord(rID);
            } catch(Exception ex) {
                System.err.println(ex.toString());
            }
        }
        return rID;
    }
Deleting a record from RecordStore is very easy. You have to provide the record unique id for that.

Java Code:
        try {
            System.out.println("deleting ID #" + String.valueOf(recordID));
            recordStore.deleteRecord(recordID);
        } catch (RecordStoreException rse) {
            System.err.println(rse.toString());
        }
You need record id to delete it from the RecordStore. So if you don't have the id, you have to iterate through all the items of the RecordStore to find the required record id. For that, you may use the following method:

Java Code:
 RecordEnumeration 	enumerateRecords(RecordFilter filter, RecordComparator comparator, boolean keepUpdated)


Let me show how to useenumerateRecords(...) method with example:

Java Code:
        /*
         * Build an enumeration to index through all records.
         */
        RecordEnumeration re = null;
        try {
            re = recordStore.enumerateRecords((RecordFilter)null,
                    (RecordComparator)null, false);
        } catch (RecordStoreNotOpenException rsnoe) {
            System.err.println(rsnoe.toString());
        }

        while(re.hasNextElement()) {
            try {
                System.out.println("Next Record ID = " +
                        re.nextRecordId());
            } catch(InvalidRecordIDException iride) {
                System.err.println(iride.toString());
Now lets see how to build a new RecordEnumeration with a filter to include only integer records. Notice how it will only report the integer records.

Java Code:
        IntegerFilter iFilt = new IntegerFilter();
        try {
            re = recordStore.enumerateRecords((RecordFilter)iFilt,
                    null, false);
        } catch (RecordStoreNotOpenException rsnoe) {
            System.err.println(rsnoe.toString());
        }

        /*
         * And walk through it backwards for kicks...
         */
        while(re.hasPreviousElement()) {
            try {
                System.out.println("Previous Record ID = " +
                        re.previousRecordId());
            } catch(InvalidRecordIDException iride) {
                System.err.println(iride.toString());
            }
        }
It is always preferred to clean the RecordEnumeration by calling its destory method.

I hope that you have learned to use RecordStore and will use it from now on.

Submit "RecordStore with examples" to Facebook Submit "RecordStore with examples" to Digg Submit "RecordStore with examples" to del.icio.us Submit "RecordStore with examples" to StumbleUpon Submit "RecordStore with examples" to Google

Tags: None Add / Edit Tags
Categories
Java ME

Comments