J2ME : Store Database

import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.rms.*;

public class TestStore extends MIDlet {

    static final String DBNAME = “mydata”;

    public TestStore() {

        RecordStore rs = null;

        // Data is persistent across MIDlet invocations.
        // So, first clear out the old record store…

        try {
            RecordStore.deleteRecordStoreDBNAME );
        }
        catchException e ){
            // ignore any errors…
        }

        // Now create a new one and dump
        // each element out….

        try {
            rs = RecordStore.openRecordStoreDBNAME,
                                              true );

            byte[] data1 = “Here is the first record”.getBytes();
            byte[] data2 = “And here is the second”.getBytes();
            byte[] data3 = “And then the third”.getBytes();

            data3[00;
            data3[data3.length-1(byte1;

            rs.addRecorddata1, 0, data1.length );
            rs.addRecorddata2, 0, data2.length );
            rs.addRecorddata3, 0, data3.length );

            dumpRecordStorers, System.out );


            rs.closeRecordStore();
        }
        catchRecordStoreException e ){
            System.out.println);
        }

        notifyDestroyed();
    }

    public void dumpRecordStoreRecordStore rs,
                                 PrintStream out )
    {
        ifrs == null return;

        StringBuffer hexLine = new StringBuffer();
        StringBuffer charLine = new StringBuffer();

        try {
            int    lastID = rs.getNextRecordID();
            byte[] data = new byte[100];
            int    size;

            forint i = 1; i < lastID; ++i ){
                try {
                    size = rs.getRecordSize);
                    ifsize > data.length ){
                        data = new bytesize * ];
                    }

                    out.println“Record ” + i +
                              ” of size ” + size );

                    rs.getRecordi, data, );
                    dumpRecorddata, size, out,
                           hexLine, charLine, 16 );

                    out.println“” );
                }
                catchInvalidRecordIDException e ){
                    continue;
                }
            }
        }

        catchRecordStoreException e ){
            out.println“Exception reading record store: ” + e );
        }
    }

    private void dumpRecordbyte[] data, int size,
                             PrintStream out,
                             StringBuffer hexLine,
                             StringBuffer charLine,
                             int maxLen )
    {
        ifsize == return;

        hexLine.setLength);
        charLine.setLength);

        int count = 0;

        forint i = 0; i < size; ++i ){
            char b = (char) ( data[i0xFF );

            ifb < 0x10 ){
                hexLine.append‘0’ );
            }

            hexLine.appendInteger.toHexString) );
            hexLine.append‘ ‘ );

            if( ( b >= 32 && b <= 127 ||
                Character.isDigit||
                Character.isLowerCase||
                Character.isUpperCase) ){
                charLine.append( (char);
            else {
                charLine.append‘.’ );
            }

            if++count >= maxLen || i == size-){
                whilecount++ < maxLen ){
                hexLine.append”   ” );
                }

                hexLine.append‘ ‘ );
                hexLine.appendcharLine.toString() );

                out.printlnhexLine.toString() );
 
                hexLine.setLength);
                charLine.setLength);
                count = 0;
            }
        }
    }

    public void destroyAppboolean unconditional ) {
    }

    public void startApp() {
    }

    public void pauseApp() {
    }
}


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.