July 27, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Building a J2ME Application in NetBeans 4.1

  • August 11, 2005
  • By Dick Wall
  • Send Email »
  • More Articles »
Adding Persistence

The persistence mechanism you will be using is based around a RecordStore. The record store has a name assigned to it—something you choose for your application that hopefully will not clash with another one. You will call yours MileageCalcStore. Records within the record store have a numeric index, and are simply an array of bytes.

You will add two new methods to your class: saveCurrentState() and loadCurrentState(). For brevity, I will simply include the code here and discuss it below:


public void saveCurrentState() {
   RecordStore myStore = null;
   try {
      myStore =
         RecordStore.openRecordStore("MileageCalcStore", true);
      int n = myStore.getNumRecords();
      // create the byte array representations for storing
      byte[] startingMileageBytes =
         Long.toString(this.startingMileageVal).getBytes();
      byte[] currentMileageBytes =
         Long.toString(this.currentMileageVal).getBytes();
      byte[] totalFuelBytes =
         Long.toString(this.totalFuelVal).getBytes();
      byte[] totalCostBytes =
         Long.toString(this.totalCostVal).getBytes();
      // do we already have a record set to use?
      if(n == 0) {
         // no - create new records
         myStore.addRecord(startingMileageBytes, 0,
                           startingMileageBytes.length);
         myStore.addRecord(currentMileageBytes, 0,
                           currentMileageBytes.length);
         myStore.addRecord(totalFuelBytes, 0, totalFuelBytes.length);
         myStore.addRecord(totalCostBytes, 0, totalCostBytes.length);
      }
      else {
         // use existing records
         myStore.setRecord(1, startingMileageBytes,
                           0, startingMileageBytes.length);
         myStore.setRecord(2, currentMileageBytes,
                           0, currentMileageBytes.length);
         myStore.setRecord(3, totalFuelBytes,
                           0, totalFuelBytes.length);
         myStore.setRecord(4, totalCostBytes,
                           0, totalCostBytes.length);
      }
   }
   catch(Exception ex) {
      // omitted for brevity - but you should do something
      // with this
   }
   finally {
      if(myStore != null) {
         try {
            myStore.closeRecordStore();
         }
         catch(Exception ex) {
            // ignore
         }
      }
   }
}

Some notes on the above:

  • The "true" when opening the record store indicates that a store should be created by that name if it doesn't already exist.
  • No doubt, there are better ways to get a byte array from the values. This is a quick example and converting to strings and then bytes keeps things nice and clear and makes things easier to debug on loading too, but feel free to implement storage and retrieval in your own style.
  • You are only storing the totals and the starting mileage; everything else can be calculated on load.
  • When creating new records, use addRecord (and the index number will increment automatically); when re-using existing ones, the index number is the first argument.
  • Remember to add any includes needed to get this to compile (like RecordStore. Right-click and say "Fix Imports."
  • The finally block is important to close the record set should anything go wrong. You should always include the finally block to clean up any dangling record sets.

Now, you also need the code to load the values back in:


public void loadCurrentState() {
   RecordStore myStore = null;
   try {
      myStore = RecordStore.openRecordStore("MileageCalcStore", true);
      int n = myStore.getNumRecords();
      if(n == 0) {
         // if no record set, use safe values
         this.startingMileageVal = 0;
         this.currentMileageVal  = 0;
         this.totalFuelVal       = 0;
         this.totalCostVal       = 0;
      }
      else {
         String startMile, currentMile, totCost, totFuel;
         startMile   = new String(myStore.getRecord(1));
         currentMile = new String(myStore.getRecord(2));
         totFuel     = new String(myStore.getRecord(3));
         totCost     = new String(myStore.getRecord(4));
         // now parse out the values
         this.startingMileageVal = Long.parseLong(startMile);
         this.currentMileageVal  = Long.parseLong(currentMile);
         this.totalMileageVal    = currentMileageVal
                                 - startingMileageVal;
         this.totalFuelVal       = Long.parseLong(totFuel);
         this.totalCostVal       = Long.parseLong(totCost);
      }
   }
   catch(Exception ex) {
      // TODO: do something with exception (ommitted for brevity)
   }
   finally {
      if(myStore != null) {
         try {
            myStore.closeRecordStore();
         }
         catch(Exception ex) {
            // ignore
         }
      }
   }
   // now update the averages and summary
   this.recalcAveragesAndUpdateSummary();
}




Page 4 of 5



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel