TestDB.java Source code

Java tutorial

Introduction

Here is the source code for TestDB.java

Source

import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordComparator;
import javax.microedition.rms.RecordFilter;
import javax.microedition.rms.RecordListener;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreNotOpenException;

class TestDB implements RecordListener {
    public void recordAdded(RecordStore rs, int id) {
        try {
            System.out.println(rs.getName() + " added record " + id);
        } catch (RecordStoreNotOpenException e) {
        }
    }

    public void recordChanged(RecordStore rs, int id) {
        try {
            System.out.println(rs.getName() + " changed record " + id);
        } catch (RecordStoreNotOpenException e) {
        }
    }

    public void recordDeleted(RecordStore rs, int id) {
        try {
            System.out.println(rs.getName() + " removed record " + id);
        } catch (RecordStoreNotOpenException e) {
        }
    }
}

class TestFilter implements RecordFilter {
    public boolean matches(byte[] rec) {
        String r = new String(rec);
        return ((r.charAt(0) == 'a') || (r.charAt(0) == 'A'));
    }
}

class TestComparator implements RecordComparator {

    public int compare(byte[] rec1, byte[] rec2) {
        String r1 = new String(rec1);
        String r2 = new String(rec2);
        if (r1.compareTo(r2) > 0)
            return (RecordComparator.FOLLOWS);
        else if (r1.compareTo(r2) < 0)
            return (RecordComparator.PRECEDES);
        else
            return (RecordComparator.EQUIVALENT);
    }
}

public class J2METestDBTest extends MIDlet {

    public void startApp() {
        try {
            RecordStore anRMS = RecordStore.openRecordStore("TestRMS", true);
            anRMS.addRecordListener(new TestDB());

            String test = "This is a test";
            byte[] b = test.getBytes();
            anRMS.addRecord(b, 8, 6);
            anRMS.addRecord(b, 5, 2);
            RecordComparator rc = new TestComparator();
            byte[] r1 = anRMS.getRecord(1);
            byte[] r2 = anRMS.getRecord(2);
            System.out.println(rc.compare(r1, r2));

        } catch (Exception e) {
        }
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

}