×

Discussion Board

Results 1 to 9 of 9
  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    20

    organize records in recordstore

    Hi there,

    i'm trying to use the recordstore to save my preferences and other stuff. The problem that i have is, how can i find a specific record in the rms?

    When i store the username and password in seperate records and start the application again, how do i know the record id?
    can someone explain me a good way to organize such things? i read something about recordenumeration and recordfilter, so my idea was to store an id to every record and filter that? but that isn't really smooth...

    thanks for your help!

    -matze

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: organize records in recordstore

    If you are just saving preferences, a small amount of data (say, ~1k), then I recommend storing everything in one record, always record ID #1.

    Use a ByteArrayOutputStream and DataOutputStream to store a sequence of values into a byte array. You can read these back using a ByteArrayInputStream and DataInputStream.

    Save and load like this:

    Code:
    private static void savePreferences(byte[] data) throws RecordStoreException {
        RecordStore rs = RecordStore.openRecordStore(PREFERENCES_RS, true);
        try {
            if (rs.getNumRecords() == 0) {
                rs.addRecord(data, 0, data.length);
            } else {
                rs.setRecord(1, data, 0, data.length);
            }
        } finally {
            rs.closeRecordStore();
        }
    }
    
    private static byte[] loadPreferences() throws RecordStoreException {
        byte[] data;
        RecordStore rs = RecordStore.openRecordStore(PREFERENCES_RS, true);
        try {
            if (rs.getNumRecords() == 0) {
                data = null;
            } else {
                data = rs.getRecord(1);
            }
        } finally {
            rs.closeRecordStore();
        }
        return data;
    }
    Graham.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    20

    Re: organize records in recordstore

    hi, thanks for your reply. unfortunatly i have to store a lot more. i recieve a XML file from a web server and parse it. after parsing i have a vector with all the data from the xml. i have to store this vector in the rms too...
    so your idea doesn't work here. do you have another solution for storing the vector and the username+password?

    maybe i have to change the vector to another type (problem is that i have 2 different objects stored in the vector).

    thanks!

    matze

  4. #4
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: organize records in recordstore

    How much data?

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    20

    Re: organize records in recordstore

    my first test xml file was about 4-7kb. but there is a lot of overhead. so i extract the xml and then it should be only 2-3kb.

  6. #6
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: organize records in recordstore

    No problem putting that in one record. Your app will be faster and more robust if you use one record.

    Graham.

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    20

    Re: organize records in recordstore

    mh ok, but when i start the app first time, there is no data...then i put in usernamen and password and after that i recieve the xml...so you think i should save that everything in one record and read it out completly when i only need username and password or the other data?
    but then i have to sperate it somehow, like that i can get only username and password. how does it work?

  8. #8
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: organize records in recordstore

    Well, I'd split the data into two record stores, one for username and password, and the other for downloaded data (one record in each store).

    To make a class "storable":

    Code:
    public interface Storable {
        public int getClassID();
        public void write(DataOutput out) throws IOException;
    }
    Code:
    public class SomeClass implements Storable {
        public static final int CLASS_ID = 0x12345678;
    
        private int anIntField;
        private String aStringField;
        private boolean aBooleanField;
    
        public SomeClass(DataInput in) throws IOException {
            anIntField = in.readInt();
            aStringField = in.readUTF();
            aBooleanField = in.readBoolean();
        }
    
        public int getClassID() {
            return CLASS_ID;
        }
    
        public void write(DataOutput out) throws IOException {
            // write all the object properties
            out.writeInt(anIntField);
            out.writeUTF(aStringField);
            out.writeBoolean(aBooleanField);
        }
    }
    Write a whole bunch of objects to a byte[] that you can save in a record:

    Code:
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        DataOutputStream dout = new DataOutputStream(bout);
        try {
            Enumeration en = someVector.elements();
            while (en.hasMoreElements()) {
                Storable s = (Storable) (en.nextElement());
                dout.writeInt(s.getClassID());
                s.write(dout);
            }
        } finally {
            dout.close();
        }
        byte[] data = bout.toByteArray();  // here's the array to store in RMS
    Read them back the same way (using a ByteArrayInputStream and a DataInputStream), using the class IDs to work out which class to instantiate.

    Graham.

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    20

    Re: organize records in recordstore

    alright thanks, i will try it like this!

    matze

Similar Threads

  1. How to fetch records from recordstore to append on canvas
    By j.wawdhane in forum Mobile Java General
    Replies: 1
    Last Post: 2009-10-27, 12:30
  2. Replies: 2
    Last Post: 2009-05-05, 19:11
  3. RMS limits - max records per recordstore
    By conos in forum Mobile Java General
    Replies: 3
    Last Post: 2007-02-28, 10:15
  4. Records of RecordStore still available?
    By MelBreinesberger in forum Mobile Java General
    Replies: 1
    Last Post: 2006-03-17, 18:56
  5. nokia 3410 - problem with deleting records from a recordstore
    By tillstoschus in forum Mobile Java General
    Replies: 0
    Last Post: 2002-11-11, 21:24

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×