RecordStore with examples - II
14 May 2008Let continue exploring RecordStore.
As shown in the previous post, 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.
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 recird unique id for that.
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 dont 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:
RecordEnumeration enumerateRecords(RecordFilter filter, RecordComparator comparator, boolean keepUpdated)
continued …
Related Posts:
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 - II