Database in J2ME
Asked Answered
R

2

3

I am new in J2ME.

In my Application, I want to add the Multiple Records in the Record Store and also want to access it.

How can I add the multiple Records in the Record Store and how can I access it?

Refinery answered 23/7, 2012 at 4:31 Comment(0)
B
8

Here is my library code for RMS, just study it, it is very easy to implement, all the methods like insert,updated, delete is there.

import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotOpenException;
import com.project.gui.components.CustomAlert;
import com.project.gui.midlet.MyMidlet;

public class RMSStore 
{
    private RecordStore rs = null;

    public void openRecordStore(String str)
    {
        try 
        {
            if(rs == null)
            {
                rs = RecordStore.openRecordStore(str, true);
            }
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }

    public void closeRecordStore()
    {
        try 
        {
            if(rs!=null)
            {
                rs.closeRecordStore();
            }
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }

    public void deleteRecordStore(String storenName) 
    {
        try 
        {
            RecordStore.deleteRecordStore(storenName);
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }

    public void deleteRMS(String storenName) 
    {
        int count = 0;

        try
        {
            RecordStore newRS = RecordStore.openRecordStore(storenName, true);
            count = newRS.getNumRecords();
            newRS.closeRecordStore();
        }
        catch ( Exception e ) 
        {
            System.out.println ( "Error while Opening " + e.toString() );
        }

        if ( count > 0 )
        {
            try 
            {
                RecordStore.deleteRecordStore(storenName);
            }
            catch (Exception e) 
            {
                e.printStackTrace();
            }

        }
    }


    public static String[] listAllRecordStore () 
    {
        return RecordStore.listRecordStores();
    }

    public boolean SearchRecord(String Rec)
    {
        String [] data = getRecordData();

        for ( int i = 0 ; i < data.length ; i++ )
        {
            if ( Rec.toString().trim().equals(data[i].toString().trim()) )
            {
                data = null; // System.gc();
                return true;
            }
        }

        data = null; // System.gc();
        return false;
    }

    public boolean SearchRecord(String Rec, int pos )
    {
        String [] data = getRecordData();

        Rec = Rec.substring(0,pos);
        for ( int i = 0 ; i < data.length ; i++ )
        {
            data[i] = data[i].substring(0, pos );
            if ( Rec.toString().trim().equals(data[i].toString().trim()) )
            {
                data = null; // System.gc();
                return true;
            }
        }

        data = null; // System.gc();
        return false;
    }


    public int getCurrentRecordID ( RMSStore rmsTable, String Rec )
    {
        RecordEnumeration re = null; 
        try
        {
            re = rmsTable.getRecordEnumData();

            while ( re.hasNextElement() )
            {
                int id = re.nextRecordId();
                String record = rmsTable.getRecordFromId(id);

                if ( record.indexOf(Rec) != -1 )
                {
                    return id;
                }
            }
        }
        catch ( Exception e ) { System.out.println ( "getCurrentRecordID Error:" + e.toString() );  }
        return -1;
    }

    public int writeRecord(String str)
    {
        int id = 0;
        try 
        {
            id = rs.addRecord(str.getBytes(), 0, str.getBytes().length);
        } 
        catch (RecordStoreFullException e) 
        {
            CustomAlert memoryFullAlert = new CustomAlert("");
            memoryFullAlert.setString("Memory Full");
            MyMidlet.getDisplay().setCurrent(memoryFullAlert);
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
        return id;
    }

    public int writeByteRecord(byte[] data)
    {
        int id = -1;

        try 
        {
            id = rs.addRecord(data, 0, data.length);
        } 
        catch (RecordStoreFullException e) 
        {
            e.printStackTrace();
            CustomAlert memoryFullAlert = new CustomAlert("");
            memoryFullAlert.setString("Memory Full");
            MyMidlet.getDisplay().setCurrent(memoryFullAlert);          
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
        return id;
    }

    public int getRecordCount()
    {
        try 
        {
            return rs.getNumRecords();
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
        return 0;
    }

    public byte[] getRecordDataFromId(int id)
    {
        byte[] data = null;
        try 
        {
            data = rs.getRecord(id);            
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
        return data;
    }

    public String getRecordFromId(int id)
    { 
        return new String(getRecordDataFromId(id));
    }

    public byte[] getRecordByteFromId(int id)
    { 
        return getRecordDataFromId(id);
    }

    public void deleteRecord(int id)
    {
        try 
        {
            rs.deleteRecord(id);
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }

    public boolean checkRecordExists(String compare)
    {
        for(int i = 0; i < getRecordCount(); i++)
        {           
            if(compare.equals(getRecordFromId(i + 1)))
            {
                return true;
            }
        }       
        return false;
    }

    public int getMaxRMSSize()
    {
        int size  = 0;
        try 
        {
            size = rs.getSizeAvailable() + rs.getSize();
        } 
        catch (RecordStoreNotOpenException e) 
        {
            e.printStackTrace();
        }

        return size;
    }

    public void setRecordById(String str, int id)
    {
        try 
        {
            rs.setRecord(id, str.getBytes(), 0, str.getBytes().length);
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }

    public int getNextRecordId() 
    {
        int id = 0;
        try 
        {
            id = rs.getNextRecordID();
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
        return id;
    }

    public RecordEnumeration getRecordEnumData () 
    {
        try 
        {
            return rs.enumerateRecords(null, null, false);
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
            return null;
        }
    }

    public String [] getRecordData()
    {
        String[] str = null;
        int counter = 0;
        try 
        {
            RecordEnumeration enumeration = rs.enumerateRecords(null, null, false);
            str = new String[rs.getNumRecords()];

            while(enumeration.hasNextElement())
            {
                try 
                {
                    str[counter] = (new String(enumeration.nextRecord()));
                    counter ++;
                } 
                catch (Exception e) 
                {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }
}
Bevan answered 23/7, 2012 at 4:59 Comment(3)
Mr. Lucifer, please provide me the CustomAlert and MyMidlet file,So i can understand it visually.Refinery
@Lucifer, give me (publish there) full example with Midlet methodes. I have some problems with your code.Unarmed
@ArthurAlunts, This answer was posted before a decade, Sorry I do not have that code with me now.Bevan
I
1

RMS is a record based data storage mechanism, so you can store multiple records very easily, see following blog to see how RMS works.

http://www.ibm.com/developerworks/library/wi-rms/

Irrepressible answered 23/7, 2012 at 4:35 Comment(5)
Thanks a lot jeet.It helped me.But One Problem Still Occur, when i close the emulator,then database is also deleted.Is there any solution for this?Refinery
are you using eclipse or netbeans ?Bevan
I am using netbeans IDE 7.1.2 for j2me development.Refinery
it happens on netbeans and eclipse that data may get destroy when you close the emulator, i suggest you to try it on real device, it will surly work properly.Bevan
Yes on device it don't deleted. ru.#1553222Unarmed

© 2022 - 2024 — McMap. All rights reserved.