




































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Description about Java ME – Storing data on the device, Optional versus core APIs, Three ways of storing data , Using Record Stores,File and PIM, Deleting a RecordStore.
Typology: Study notes
1 / 44
This page cannot be seen from the preview
Don't miss anything!





































1 Application Development for Mobile Devices
Contents Optional versus core APIs Three ways of storing data Using Record Stores Introduction Opening, closing and writing to a RecordStore Reading data from a RecordStore Deleting a RecordStore Things we aren't studying and limitations of RecordStores File and PIM
collect data offline then upload download data then disconnect
javax.microedition.rms.RecordStore
MIDlet Suite MIDlet MIDlet MIDlet MIDlet Suite MIDlet RecordStore RecordStore RecordStore Persistent Storage on the Device
Opening a RecordStore Before you can do anything with a RecordStore you must open it RecordStore rs = RecordStore.openRecordStore("Details", true); name of the RecordStore true means that if the RecordStore doesn't already exist it'll be created If anything goes wrong when you're accessing a RecordStore the code may "throw an exception"
Writing a Record Records are written as arrays of bytes It's easy to convert from a String to an array of bytes String detailsToAdd = strName + ";" + strAge + ";" + strEmail; byte [] detailsBuffer = detailsToAdd.getBytes(); rs.addRecord(detailsBuffer, 0, detailsBuffer.length); make up a String to hold the details to be written convert to an array of bytes write the data (^) amount to write name of the array start of data in the array
What's in our RecordStore? Details 4 mickey ; 60 ; [email protected] 5 gill ; 23 ; [email protected] 7 sati ; 24 ; [email protected]
Closing a RecordStore Closing a RecordStore will release the resources, including memory, associated with it There's a trade-off between keeping a RecordStore open for the life of your MIDlet - saves the overhead of repeatedly opening and closing it opening and closing a RecordStore in your MIDlet each time you use it - minimises use of memory To close a RecordStore rs.closeRecordStore();
Finding the number of Records ....... rs.addRecord(detailsBuffer, 0, detailsBuffer.length); // find out the number of records int recordCount = rs.getNumRecords(); rs.closeRecordStore(); // display confirmation to the user mAlertConfirmDetailsSaved.setString("** Details saved ** \n" + recordCount + " records have been stored" ); ........ Persist2.java
Persist2.java import javax.microedition.midlet.; import javax.microedition.lcdui.; import javax.microedition.rms.RecordStore; public class Persist2 extends MIDlet implements CommandListener { private Display mDisplay; private Alert mAlertConfirmDetails, mAlertConfirmDetailsSaved; private Form mDataEntryForm; private TextField txtName, txtAge, txtEmail; private String strName, strAge, strEmail; public Persist2() { // create the Form mDataEntryForm = new Form("Enter your details"); txtName = new TextField("Name:", null, 15, TextField.ANY); txtAge = new TextField("Age:", null, 3, TextField.NUMERIC); txtAge.setLayout(Item.LAYOUT_2 | Item.LAYOUT_CENTER); txtEmail = new TextField("Email:", null, 20, TextField.EMAILADDR);
protected void startApp() { mDisplay = Display.getDisplay(this); mDisplay.setCurrent(mDataEntryForm); } public void commandAction(Command c, Displayable d) { if (c.getLabel().equals("Exit")) { notifyDestroyed(); } if (c.getLabel().equals("Back")) { mDisplay.setCurrent(mDataEntryForm); } if (c.getLabel().equals("Next") && d == mDataEntryForm) { // get the details entered strName = txtName.getString(); strAge = txtAge.getString(); strEmail = txtEmail.getString(); // display the details to the user mAlertConfirmDetails.setString("Details entered: \n " + strName + "\n " + strAge + "\n " + strEmail); mDisplay.setCurrent(mAlertConfirmDetails); }
if (c.getLabel().equals("Save")) { // write the details to the database try { RecordStore rs = RecordStore.openRecordStore("Details", true); String detailsToAdd = strName + ";" + strAge + ";" + strEmail; byte [] detailsBuffer = detailsToAdd.getBytes(); rs.addRecord(detailsBuffer, 0, detailsBuffer.length); // find out the number of records int recordCount = rs.getNumRecords(); rs.closeRecordStore(); // display confirmation to the user and clear the form mAlertConfirmDetailsSaved.setString(" ** Details saved **\n " + recordCount + " records have been stored"); // clear the form ready for redisplay txtName.setString(""); txtAge.setString(""); txtEmail.setString(""); } catch (Exception e) { // an error occurred saving mAlertConfirmDetailsSaved.setString("Couldn't save details"); System.err.println("Error saving to database"); } mDisplay.setCurrent(mAlertConfirmDetailsSaved, mDataEntryForm); } }