Database query j2me including criterias
Asked Answered
P

2

2

I am creating an application with J2ME. for connecting with database i am using RecordStore.

so to get a record i need to write as follow:

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;
    }

This is first get all records and traverse through it to search a record. But i have thousands of records and i just need some based on criteria is there any way to resolve this problem ? I do not want to traverse through thousands of records to get ten records.

One more thing i am confused about UI part that LWUIT is better or JSR is better to implement ?

Piper answered 13/8, 2012 at 9:47 Comment(1)
Open a new question for the UI part.Guarneri
D
2

The code you are talking in the question is method from my answer. I have developed this code about a year ago for searching purpose only. The basic of RMS is a structured version of flat file. You can not fire a query on RMS. There is no in-built method for searching records from RMS. This is the reason that after many r & d i developed the above code.

Detribalize answered 13/8, 2012 at 15:19 Comment(4)
i have searched a lot and got that answer from your answer. but the issue is that my application takes much time to show 10 records as it has to filter through 10000 records.Piper
agree, but you have no other option to search from a flat file kind of data structure.Detribalize
is there any other option of RMS can we use database ?Piper
for J2ME we have no other option than RMS.Detribalize
G
0

If you are calling SearchRecord a lot you could make String [] data an attribute. This would lead to a smaller version of your method:

public boolean SearchRecord(String rec, int pos )
{
    rec = rec.substring(0, pos).trim();
    for ( int i = 0 ; i < this.data.length ; i++ )
    {
        String comp = this.data[i].substring(0, pos);
        if ( rec.equals(comp.trim()) )
        {
            return true;
        }
    }

    return false;
}
Guarneri answered 13/8, 2012 at 11:45 Comment(2)
i just dont want to traverse through all records.. I want it to provide criteria like in sql we arite select employee where fname='abc' here we get all record and then find record with first anme i want to get that record only from databasePiper
You can use a RecordFilter when calling RecordStore.enumerateRecords, but, for each comparison, you will have to convert from byte[] to String.Guarneri

© 2022 - 2024 — McMap. All rights reserved.