Birthday Database
/**** Chapter 5 Sample code for RMS Package****/
import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.rms.*;
public class BirthdayDB extends MIDlet {
static final String RSNAME = "birthdayDB";
private RecordStore rs = null;
public BirthdayDB() {
try {
openBirthdayDB(RSNAME);
populateSampleRecords();
//BEGIN: Test for basic funcitionality of a record store
displayRecordStoreinfo();
searchAndDisplayRecords(null, null); //Simple enumeration withouit any criteria
deleteBirthday(2);
updateBirthday(3, "Lisa", "03/07/99");
searchAndDisplayRecords(null, null); //Simple enumeration withouit any criteria
//END: Test for basic funcitionality of a record store
//BEGIN: Test for record listener
rs.addRecordListener((RecordListener) new BirthdayDBListener());
addBirthday("Maya","09/08/96");
deleteBirthday(3);
updateBirthday(4, "NewLisa", "03/08/99");
rs.removeRecordListener((RecordListener) new BirthdayDBListener());
//END: Test for record listener
//BEGIN: Complex test for record enumeration with record filter and record comparator
NameFilter nameFilter = new NameFilter("John");
NameComparator nameComparator = new NameComparator();
searchAndDisplayRecords(nameFilter,nameComparator);
//END: Complex test for record enumeration with record filter and record comparator
closeBirthdayDB();
}
catch( Exception e ){
System.out.print("Error in BirthdayDB.BirthdayDB-> ");
System.out.println("Exception : " + e.getMessage());
}
notifyDestroyed();
}
/****
This function will create/open database.
****/
private void openBirthdayDB(String name) {
// Just to avoid using wrong DB,
// First clear out the old record store
try {
RecordStore.deleteRecordStore( name );
}
catch( Exception e ){
// ignore any errors...
}
// attempt to create a new RecordStore database
try {
rs = RecordStore.openRecordStore( name, true );
}
catch( Exception e ){
System.out.print("Error in BirthdayDB.openBirthdayDB -> ");
System.out.println("Exception : " + e.getMessage());
}
}
/****
This function closes the DB if it is open.
****/
private void closeBirthdayDB() {
if (rs != null) {
try {
rs.closeRecordStore();
} catch (Exception e) {
System.out.print("Error in BirthdayDB.closeBirthdayDB -> ");
System.out.println("Exception : " + e.getMessage());
}
}
}
/****
This function will search for the records using criteria defined in
Filter class and it will display records in the order specfied in
Comparator class
****/
public void searchAndDisplayRecords(NameFilter f, NameComparator c){
try {
RecordEnumeration rEnum = rs.enumerateRecords( f, c , false);
System.out.println("Total Records in Enumeration: " + rEnum.numRecords());
while (rEnum.hasNextElement()){
byte [] bRec = rEnum.nextRecord();
//unpack a multi-column record
ByteArrayInputStream bais = new ByteArrayInputStream(bRec);
DataInputStream dis = new DataInputStream(bais);
String name = dis.readUTF();
String bDay = dis.readUTF();
System.out.println("Name: " + name + " BirthDay: "+ bDay);
}
}
catch (Exception e) {
System.out.print("Error in BirthdayDB.searchAndDisplayRecords -> ");
System.out.println("Exception : " + e.getMessage());
}
return;
}
/****
This function creates a byte stream for record and adds a record to database
****/
public void addBirthday(String name, String bDay) {
try{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeUTF(name);
dos.writeUTF(bDay);
byte [] bRec = baos.toByteArray();
rs.addRecord(bRec,0,bRec.length);
dos.close();
baos.close();
}
catch (Exception e) {
System.out.print("Error in BirthdayDB.addBirthdayDB -> ");
System.out.println("Exception : " + e.getMessage());
}
return;
}
/****
This function deleted a record with given recordId
****/
public void deleteBirthday(int recordId) {
try{
rs.deleteRecord(recordId);
}
catch (Exception e) {
System.out.print("Error in BirthdayDB.deleteBirthday -> ");
System.out.println("Exception : " + e.getMessage());
}
return;
}
/****
This function updates the record with new name and birthday at given recordId
****/
public void updateBirthday(int recordId, String newName, String newBDay) {
try{
// convert new data into byte stream
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeUTF(newName);
dos.writeUTF(newBDay);
byte [] bRec = baos.toByteArray();
rs.setRecord(recordId, bRec,0,bRec.length);
dos.close();
baos.close();
}
catch (Exception e) {
System.out.print("Error in BirthdayDB.updateBirthday -> ");
System.out.println("Exception : " + e.getMessage());
}
return;
}
/****
This function populates seed records
****/
public void populateSampleRecords() {
addBirthday("John", "01/04/1969");
addBirthday("Joe", "05/05/1976");
addBirthday("Keran", "09/12/1976");
addBirthday("Marry", "04/22/1979");
addBirthday("Paul", "06/26/1972");
addBirthday("Robert", "08/30/1971");
addBirthday("John", "01/04/1970");
}
/****
This function displays information of a record store
****/
public void displayRecordStoreinfo() {
try {
System.out.println("Name of the record store is " + rs.getName());
System.out.println("Number of records in this record store are " + rs.getNumRecords());
System.out.println("Size of record store is " + rs.getSize());
System.out.println("Available size for the record store " + rs.getSizeAvailable());
System.out.println("Version number of the record store is " + rs.getVersion());
System.out.println("Last Modified Date is " + rs.getLastModified());
}
catch (Exception e) {
System.out.print("Error in BirthdayDB.displayRecordStoreinfo -> ");
System.out.println("Exception : " + e.getMessage());
}
}
/****
MIDlet lifecycle functions
****/
public void destroyApp( boolean unconditional ) {
if (rs != null)
closeBirthdayDB();
}
public void startApp() {
}
public void pauseApp() {
}
}
class NameFilter implements RecordFilter {
String name;
public NameFilter(String filterOnThisName) {
name = filterOnThisName;
}
public boolean matches(byte [] candidateRecord) {
try{
ByteArrayInputStream bais = new ByteArrayInputStream(candidateRecord);
DataInputStream dis = new DataInputStream(bais);
String nameFromRec = dis.readUTF();
dis.close();
bais.close();
if (name.compareTo(nameFromRec) == 0 )
return true;
}
catch (Exception e) {
System.out.print("Error in NameFilter.matches -> ");
System.out.println("Exception : " + e.getMessage());
}
return false;
}
}
class NameComparator implements RecordComparator {
public int compare(byte [] rec1, byte [] rec2) {
try{
// read first record and extract name
ByteArrayInputStream bais1 = new ByteArrayInputStream(rec1);
DataInputStream dis1 = new DataInputStream(bais1);
String name1 = dis1.readUTF();
dis1.close();
bais1.close();
// read second record and extract name
ByteArrayInputStream bais2 = new ByteArrayInputStream(rec2);
DataInputStream dis2 = new DataInputStream(bais2);
String name2 = dis2.readUTF();
dis2.close();
bais2.close();
if (name1.compareTo(name2) < 0 )
return PRECEDES;
if (name1.compareTo(name2) > 0 )
return FOLLOWS;
}
catch (Exception e) {
System.out.print("Error in NameComparator.compare -> ");
System.out.println("Exception : " + e.getMessage());
}
return EQUIVALENT;
}
}
class BirthdayDBListener implements RecordListener {
public void recordAdded(RecordStore rs, int recordId) {
System.out.println("Record Added in record store " );
}
public void recordDeleted(RecordStore rs, int recordId) {
System.out.println("Record Deleted from record store ");
}
public void recordChanged(RecordStore rs, int recordId) {
System.out.println("Record Changed in record store ");
}
}
Related examples in the same category