How to use Record Management Store in BlackBerry-Add new records delete them and retrieve data when need

 

The RMS is a persistent storage API in Java® ME.I found code in blackberry site and develop to store the name of record and value that need to store

This class creates object that holds data values

package rms;
import java.io.*;
public final class datastor
{
    private String name;
    private String value;
    private String fullData;
    private static final short NameD = 0;
    private static final short valueD = 1;

    public datastor(byte[] data) throws java.io.IOException
    {
        fromByteArray(data);
    }   
    datastor(String name1, String value1)
    {
        name = name1;
        value = value1;
        fullData = name + ": " + value;
    }   
    public String toString()
    {
        return fullData;
    }   
    String getName()
    {
        return name;
    }   
    String getvalue()
    {
        return value;
    }   
    byte[] toByteArray() throws java.io.IOException
    {
        byte[] data;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        dos.writeShort(NameD);
        dos.writeUTF(name);
        dos.writeShort(valueD);
        dos.writeUTF(value);
        data = baos.toByteArray();   
        return data;
    }   

    private void fromByteArray(byte[] array) throws java.io.IOException
    {
        ByteArrayInputStream bais = new ByteArrayInputStream(array);
        DataInputStream dis = new DataInputStream(bais);
        short tag;          
        try
        { 
            while(true)
            {
                tag = dis.readShort();
                if (tag == NameD)
                {
                    name = dis.readUTF();
                }
                else if (tag == valueD)
                {
                    value = dis.readUTF();
                }
                else
                {
                    dis.readUTF();
                }
            }    
        }
        catch(EOFException e)
        {
            fullData = name + ": " + value;
        }
    }
}

This class use to perform the actions such as create delete data records in rms and also use to create datastore objecs

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

public final class dataStorAcc
{
    RecordStore _rs;

    public dataStorAcc(String name) throws RecordStoreException, java.io.IOException
    {
        _rs = RecordStore.openRecordStore(name, true, RecordStore.AUTHMODE_ANY, false);
    }   

    public dataStorAcc(String recordStoreName, String vendorName, String suiteName) throws RecordStoreException, java.io.IOException
    {
        _rs = RecordStore.openRecordStore( recordStoreName, vendorName, suiteName );
    }    

    public synchronized int add(String name, String value) throws java.io.IOException, RecordStoreNotOpenException, RecordStoreException
    {
        datastor cd = new datastor(name, value);
        //Creates object
        byte[] data = cd.toByteArray();     
        //converts to byte
        return _rs.addRecord(data, 0, data.length);
        //add to record store
    }

    public synchronized void edit(int index, String artist, String title) throws java.io.IOException, RecordStoreNotOpenException, RecordStoreException
    {
        datastor cd = new datastor(artist, title);
        byte[] data = cd.toByteArray();       
        _rs.setRecord(index, data, 0, data.length);
    }

    public datastor getDataobj(int recordID) throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException, java.io.IOException
    {
        byte[] data = _rs.getRecord(recordID);
        return new datastor(data);
    }    

    public synchronized void delete(int recordId) throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException
    {
        _rs.deleteRecord(recordId);
    }

    RecordEnumeration enumerate() throws RecordStoreNotOpenException
    {
        return _rs.enumerateRecords(null, null, true);
    }
}

This is how we can use this within our application

01)Declare DataStorAcc object

dataStorAcc _db;      // RMS reference
RecordEnumeration _enum;

_db = new dataStorAcc("My Music");//Creates new Datastore named My Music
_enum = _db.enumerate(); //Creates enumerator

02)Add new record to rms

_db.add(“name”,”value”);

03)Delete from rms

_db.delete(tecordID);

04)Retrive data

recordId = _enum.nextRecordId();
String data=getDataobj(recordId).toString();

2 comments:

  1. Great Man !i Just bought my new blackberry man. I was trying to learn record management in it but was completely confused. You helped me understanding it. Thank You Very much. I will certainly go for it.

    ReplyDelete

Empowering the Future of API Management: Unveiling the Journey of WSO2 API Platform for Kubernetes (APK) Project and the Anticipated Alpha Release

  Introduction In the ever-evolving realm of API management, our journey embarked on the APK project eight months ago, and now, with great a...