26
Class RecordStore http:// improvejava.blogspot.in

Record store

Embed Size (px)

Citation preview

Page 1: Record store

http://improvejava.blogspot.in

Class RecordStore

Page 2: Record store

http://improvejava.blogspot.in

• This RecordStore class provides methods for creating, writing, and reading data from recordstores.

• A class representing a record store. A record store consists of a collection of records which will remain persistent across multiple invocations of the MIDlet. The platform is responsible for making its best effort to maintain the integrity of the MIDlet's record stores throughout the normal use of the platform, including reboots, battery changes, etc.

Page 3: Record store

http://improvejava.blogspot.in

syntax

• javax.microedition.rms.RecordStore• public class RecordStore extends Object

Page 4: Record store

http://improvejava.blogspot.in

RecordStore methods

• (1) openRecordStore()• (2)closeRecordStore()• (3)deleteRecordStore• (4)addRecord()• (5)deleteRecord()• (6)getRecord()• (7)setMode()

Page 5: Record store

http://improvejava.blogspot.in

openRecordStore()

• Open (and possibly create) or create a record store associated with the given MIDlet suite. And returns a RecordStore reference.

• If this method is called by a MIDlet when the record store is already open by a MIDlet in the MIDlet suite, this method returns a reference to the same RecordStore object.

Page 6: Record store

http://improvejava.blogspot.in

• The openRecordStore() method requires two parameters. The first parameter is a string containing the name of the record store.

• The second parameter is a boolean value indicating whether the record store should be created if the record store doesn’t exist. A true value causes the record store to be created

• if the record store isn’t in the MIDlet suite and also opens the record store.

• A false value does not create the record store if the record store isn’t located.

Page 7: Record store

http://improvejava.blogspot.in

openRecordStore() syntax

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

Page 8: Record store

http://improvejava.blogspot.in

• Parameters:recordStoreName - the MIDlet suite unique name for the record store, consisting of between one and 32 Unicode characters inclusive.createIfNecessary - if true, the record store will be created if necessary

• Returns:RecordStore object for the record storeThrows:

• RecordStoreException - if a record store-related exception occurred• RecordStoreNotFoundException - if the record store could not be

found• RecordStoreFullException - if the operation cannot be completed

because the record store is full• IllegalArgumentException - if recordStoreName is invalid

Page 9: Record store

http://improvejava.blogspot.in

Ex: openRecordStore

• recordstore = RecordStore.openRecordStore("EmployRecord", true );

Page 10: Record store

http://improvejava.blogspot.in

addRecord()

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

• Adds a new record to the record store. It returns int value, which is Record Id , this id is useful to access the particular record

• Example:• rec.addRecord(byteOutputData,

0,byteOutputData.length);

Page 11: Record store

http://improvejava.blogspot.in

getRecord()• This method reads a record from the record store. Record Id must be in the

method as a parameter(actual parameter)• The getRecord() method copies the record from the record store and into the

byteInputData byte array and returns an integer representing the length of the record.

• Call the getRecord() method of the RecordStore class for each iteration of the forloop. • The getRecord() method returns bytes from the RecordStore, which are stored in

a byte array that you create. • The getRecord() method requires three parameters. The first parameter is the

record ID, • The second parameter is the byte array that you create for storing the record. • The third parameter is an integer representing the position in the record from

which to begin copying into the byte array.

Page 12: Record store

http://improvejava.blogspot.in

• For example, the following code segment reads the second record from the record store and copies that record, beginning with the first byte of the record, from the record store into the byte array. Typically, the first parameter of the getRecord() method is the integer of the for loop, and the third parameter is zero, indicating the entire record is to be copied into the byte array.

• Example: recordstore.getRecord(2, myByteArray, 0)

Page 13: Record store

http://improvejava.blogspot.in

recordstore.getRecord(2, myByteArray, 0)

• three parameters. • The first is the record ID that is being read,

which is the current value of the for loop variable.

• The second is the name of the byte array into which the record is copied.

• The third is the index position of the first byte that is to be copied into the byte array.

Page 14: Record store

http://improvejava.blogspot.in

getRecordSize(int recordId)

• intgetRecordSize(int recordId) Returns the size (in bytes) of the MIDlet data available in the given record.

Page 15: Record store

http://improvejava.blogspot.in

closeRecordStore()

• This method is called when the MIDlet requests to have the record store closed. Note that the record store will not actually be closed until closeRecordStore() is called as many times as openRecordStore() was called. In other words, the MIDlet needs to make a balanced number of close calls as open calls before the record store is closed.When the record store is closed, all listeners are removed and all RecordEnumerations associated with it become invalid. If the MIDlet attempts to perform operations on the RecordStore object after it has been closed, the methods will throw a RecordStoreNotOpenException.

Page 16: Record store

http://improvejava.blogspot.in

Syntax of closeRecordStore()

• public void closeRecordStore() throws RecordStoreNotOpenException, RecordStoreException

Page 17: Record store

http://improvejava.blogspot.in

listRecordStores()

• Returns an array of the names of record stores owned by the MIDlet suite. Note that if the MIDlet suite does not have any record stores, this function will return null. The order of RecordStore names returned is implementation dependent.

• Returns:array of the names of record stores owned by the MIDlet suite. Note that if the MIDlet suite does not have any record stores, this function will return null.

Page 18: Record store

http://improvejava.blogspot.in

Syntax of listRecordStores()

• public static String[] listRecordStores()

Page 19: Record store

http://improvejava.blogspot.in

deleteRecordStore()

• Deletes the named record store. MIDlet suites are only allowed to delete their own record stores. If the named record store is open (by a MIDlet in this suite or a MIDlet in a different MIDlet suite) when this method is called, a RecordStoreException will be thrown. If the named record store does not exist a RecordStoreNotFoundException will be thrown. Calling this method does NOT result in recordDeleted calls to any registered listeners of this RecordStore.

Page 20: Record store

http://improvejava.blogspot.in

• public static void deleteRecordStore(String recordStoreName) throws RecordStoreException, RecordStoreNotFoundException

Page 21: Record store

http://improvejava.blogspot.in

• Parameters:recordStoreName - the MIDlet suite unique record store to delete

Page 22: Record store

SortRec.java

enumerateRecords()

RecordEnumeration is constructed by calling enumerateRecords() method.

• RecordEnumeration is a method for retrieving record from recordStore. It also provides for sorting, searching of records.

• If u want to sort the records we have to write null as a 1st parameter in enumerateRecords()

• Ex: rec.enumerateRecords(null, com,false);• If you want to search the records we have to write null as the

2nd parameter in enumerateRecords() method. Ie: rec.enumerateRecords(filter,null,false);

• This method is used to call the compare() method

Page 23: Record store

SortRec.java

r = rec.enumerateRecords(null, com, false);

Boolean false: • Means specifies whether or not enumeration

is updated with change in record store

Page 24: Record store

http://improvejava.blogspot.in

• public RecordEnumeration enumerateRecords(RecordFilter filter, RecordComparator comparator, boolean keepUpdated) throws RecordStoreNotOpenException

• r =rec.enumerateRecords(null, null, false);• RecordFilter: it specifies which records should be included in enumeration.• RecordComparator: it helps in comparing two records. It is mainly useful in

finding the sort order.• Boolean: Specifies whether or not enumeration is updated with change in

record store

Page 25: Record store

http://improvejava.blogspot.in

• Parameters:filter - if non-null, will be used to determine what subset of the record store records will be used

• comparator - if non-null, will be used to determine the order in which the records are returned

• keepUpdated - if true, the enumerator will keep its enumeration current with any changes in the records of the record store. Use with caution as there are possible performance consequences. If false the enumeration will not be kept current and may return record

• Ids for records that have been deleted or miss records that are added later. It may also return records out of order that have been modified after the enumeration was built. Note that any changes to records in the record store are accurately reflected when the record is later retrieved, either directly or through the enumeration. The thing that is risked by setting this parameter false is the filtering and sorting order of the enumeration when records are modified, added, or deleted.

• Returns:an enumeration for traversing a set of records in the record store in an optionally specified order

Page 26: Record store

http://improvejava.blogspot.in

setMode()

• This method is used to set or change the authorization mode. By setting this mode to AUTHMODE-ANY, the record store can be made sharable.

• Syntax:• Void setMode(int authmode, boolean

writable);