Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Record Store (2009.12.25) Frank Ducrest. The Record Store 2 The Record Store provides persistence of data between MIDlet runs not quite a DBMS API.

Similar presentations


Presentation on theme: "The Record Store (2009.12.25) Frank Ducrest. The Record Store 2 The Record Store provides persistence of data between MIDlet runs not quite a DBMS API."— Presentation transcript:

1 The Record Store (2009.12.25) Frank Ducrest

2 The Record Store 2 The Record Store provides persistence of data between MIDlet runs not quite a DBMS API is Package javax.microedition.rms store – data collection that ➢ contains records ➢ may be private to the MIDlet ➢ may be shared with other MIDlets ➢ persists between runs of the MIDlet ➢ schema must be known to be read from or written to ➢ named as part of MIDlet suite http://java.sun.com/javame/reference/apis/jsr118/javax/microedition/rms/package-summary.html

3 The Record Store 3 Record each record in a store has a unique index (ID) starting at 1 removing a record does not recycle the index (ID), just sets data to null

4 The Record Store 4 Opening / Creating a Record Store static RecordStore openRecordStore(String recordStoreName, boolean createIfNecessary) creating if it doesn't exist RecordStore rs = RecordStore.openRecordStore(“MyStore”, true); failing if it doesn't exist RecordStore rs = RecordStore.openRecordStore(“MyStore”, false); http://java.sun.com/javame/reference/apis/jsr118/javax/microedition/rms/RecordStore.html

5 The Record Store 5 Closing a Record Store void closeRecordStore() example: rs.closeRecordStore(); Deleting a Record Store static void deleteRecordStore(String recordStoreName) example: RecordStore.deleteRecordStore(“MyStore”);

6 The Record Store 6 Adding a Record to a Store int addRecord(byte[ ] data, int offset, int numBytes) Reading a Record from a Store byte[ ] getRecord(int recordId)

7 The Record Store 7 Example Using a Record Store import javax.microedition.midlet.*; import javax.microedition.rms.*; public class RecordStoreExampleMidlet extends MIDlet { private void putNameInStore(String name) { try { RecordStore.deleteRecordStore("MyStore"); }catch (Exception e) { } try { byte[] b = name.getBytes(); RecordStore rs = RecordStore.openRecordStore("MyStore", true); rs.addRecord(b, 0, b.length); rs.closeRecordStore(); } catch (Exception e) {} }

8 The Record Store 8 Example Using a Record Store (continued) private String getNameFromStore() { String name = null; try { RecordStore rs = RecordStore.openRecordStore("MyStore", false); byte[] b = rs.getRecord(1); name = new String(b); rs.closeRecordStore(); } catch (Exception e) {} return name; }

9 The Record Store 9 Example Using a Record Store (continued) public void startApp() { String name = getNameFromStore(); System.out.println("Last Name in Store: " + name); if (name == null) putNameInStore("Sam"); else if (name.equalsIgnoreCase("Sam")) putNameInStore("Sue"); else if (name.equalsIgnoreCase("Sue")) putNameInStore("Tom"); else if (name.equalsIgnoreCase("Tom")) putNameInStore("George"); destroyApp(true); notifyDestroyed(); } public void pauseApp() {} public void destroyApp(boolean unconditional) {} }

10 The Record Store 10 Example Using a Record Store (continued) output from 6 consecutive executions: Last Name in Store: null Last Name in Store: Sam Last Name in Store: Sue Last Name in Store: Tom Last Name in Store: George


Download ppt "The Record Store (2009.12.25) Frank Ducrest. The Record Store 2 The Record Store provides persistence of data between MIDlet runs not quite a DBMS API."

Similar presentations


Ads by Google