Java Reference
In-Depth Information
For the obsessively curious—or if you're tracking record IDs for some reason, such
as to link records in two different record stores—you can get the next record ID to be
assigned using the open store's getNextRecordID method. This can throw one of the
following exceptions:
RecordStoreNotOpenException : Indicates that the record store you want to add a
record to isn't open
RecordStoreException : Indicates a general failure related to the open record store
Of course, this isn't atomic: there's no guarantee that if you invoke this, do a bit of
stuff, and then insert a record that the ID of the inserted record actually will have this ID,
because the platform is multithreaded, and another thread within your application may
be using the store! So it's up to you to manage thread contention across IDs if you use this
interface.
Retrieving a Record
Fundamentally, there are two ways to retrieve a record: by its ID, or by enumerating
across all records in the store. If you know a record's ID, you can fetch it directly using the
open record store's getRecord method and passing the record ID. This method returns the
record as an array of bytes. Of course, this method may fail; when it does, it throws one of
the following exceptions:
RecordStoreNotOpenException : Indicates that the record store you want to add a
record to isn't open
InvalidRecordIDException : Indicates that the record ID is invalid
RecordStoreException : Indicates a general failure related to the open record store
Once you have the bytes that make up your record, it's up to you to deserialize your
record from those bytes; as I note in the opening to this section, you'll likely use the
ByteArrayInputStream and DataInputStream classes to do this.
Enumerating a Record
Often, you don't know the ID of a record when you want to fetch it. This may be because
you're showing a list of records (such as a list of locations for the weather application, or a
list of expenses in an expense report) or because your record structure doesn't require any
sort of coherent index. In this case, you need an enumerator, and the MIDP record store
has you covered with the record store's enumerateRecords method. The enumerateRecords
method takes three arguments:
 
Search WWH ::




Custom Search