Session10 J2ME Record Management System

1,684 views

Published on

J2ME Record Management System

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,684
On SlideShare
0
From Embeds
0
Number of Embeds
21
Actions
Shares
0
Downloads
37
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Session10 J2ME Record Management System

  1. 1. Outline-session 9 (09-April-2009) <ul><li>>> J2ME Record Management System </li></ul><ul><li>-Notification of Changes with Record Listener </li></ul><ul><li>-Exception Handling </li></ul>
  2. 2. Sorting with Record Comparator <ul><li>RecordComparator is a Java interface </li></ul><ul><li>implement this interface when you would like the enumerator to return records in sorted order </li></ul><ul><li>Implementation: </li></ul><ul><li>public class Comparator implements RecordComparator </li></ul><ul><li>{ </li></ul><ul><li>public int compare(byte[] rec1, byte[] rec2) </li></ul><ul><li>{ </li></ul><ul><li>String str1 = new String(rec1), str2 = new String(rec2); </li></ul><ul><li>int result = str1.compareTo(str2); </li></ul><ul><li>if (result == 0) </li></ul><ul><li>return RecordComparator.EQUIVALENT; </li></ul><ul><li>else if (result < 0) </li></ul><ul><li>return RecordComparator.PRECEDES; </li></ul><ul><li>else </li></ul><ul><li>return RecordComparator.FOLLOWS; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  3. 3. Sorting with Record Comparator <ul><li>// Create a new comparator for sorting </li></ul><ul><li>Comparator comp = new Comparator(); </li></ul><ul><li>// Reference the comparator when creating the result set </li></ul><ul><li>RecordEnumeration re = rs.enumerateRecords(null,comp,false); </li></ul><ul><li>// Iterate through the sorted results </li></ul><ul><li>while (re.hasNextElement()) </li></ul><ul><li>{ </li></ul><ul><li>String str = new String(re.nextRecord()); </li></ul><ul><li>... </li></ul><ul><li>} </li></ul>
  4. 4. Record Comparator API
  5. 5. String Sort with Compound Records <ul><li>When a record has multiple data types stored within it (what I'll refer to as a compound record), there may be more than one field that would be appropriate for sorting </li></ul><ul><li>String[] pets = {&quot;duke&quot;, &quot;tiger&quot;, &quot;spike&quot;, &quot;beauregard&quot;}; </li></ul><ul><li>boolean[] dog = {true, false, true, true}; </li></ul><ul><li>int[] rank = {3, 0, 1, 2}; </li></ul><ul><li>StringSort.java </li></ul><ul><li>IntSort.java </li></ul>
  6. 6. Searching with RecordFilter <ul><li>an enumerator can filter records </li></ul><ul><li>When using RecordComparator all records in a record store are in the result set. With a RecordFilter, only those records that match the filter criteria will become part of the enumerator result set. </li></ul>
  7. 7. Searching with RecordFilter <ul><li>class SearchFilter implements RecordFilter </li></ul><ul><li>{ </li></ul><ul><li>private String searchText = null; </li></ul><ul><li>public SearchFilter(String searchText) </li></ul><ul><li>304 </li></ul><ul><li>{ </li></ul><ul><li>// This is the text to search for </li></ul><ul><li>this.searchText = searchText.toLowerCase(); </li></ul><ul><li>} </li></ul><ul><li>public boolean matches(byte[] candidate) </li></ul><ul><li>{ </li></ul><ul><li>String str = new String(candidate).toLowerCase(); </li></ul><ul><li>// Look for a match </li></ul><ul><li>if (searchText != null && str.indexOf(searchText) != -1) </li></ul><ul><li>return true; </li></ul><ul><li>else </li></ul><ul><li>return false; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  8. 8. Searching with RecordFilter <ul><li>// Create a new search filter </li></ul><ul><li>SearchFilter search = new SearchFilter(&quot;search text&quot;); </li></ul><ul><li>// Reference the filter when creating the result set </li></ul><ul><li>RecordEnumeration re = </li></ul><ul><li>rs.enumerateRecords(search,null,false); </li></ul><ul><li>// If there is at least one record in result set, a match </li></ul><ul><li>was found </li></ul><ul><li>if (re.numRecords() > 0) </li></ul><ul><li>// Do something </li></ul>
  9. 9. RecordFilter -API <ul><li>RecordFilter API </li></ul><ul><li>Table 11.6. RecordFilter Interface: javax.microedition.rms.RecordFilter </li></ul><ul><li>Method Description </li></ul><ul><li>boolean matches(byte[] candidate) Search a record for a specific value </li></ul>

×