J2ME Search example in Middlet Record Store

Hi

The below example is used to show the way we can search the data in Record Store of Middlet, This example is based on login authentication where the data has stored in special format [username]#[password] e.g testuser#password.

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

public class ExampleSearch
{

private RecordStore recordstore = null;
private RecordEnumeration recordEnum= null;
private Filter filter = null;

// get the record store
public void getRecStore(){
recordstore= RecordStore.openRecordStore(“myRec”,true);
}
//
public boolean login(String userName,String password){
getRecStore();
filter = new Filter(userName+”#”+password); // create a filter on the search text
recordEnum= recordstore.enumerateRecords(filter, null, false);
if (recordEnum.numRecords() > 0)
{
return true;
}else{
return false;
}
}

}

/*
*    This class is used as a filter for this example which is exetnding the RecordFilter
*
*
*/
class Filter implements RecordFilter{
private String search = null;
private ByteArrayInputStream inputstream = null;
private DataInputStream datainputstream = null;
public Filter(String search)
{
this.search = search.toLowerCase();
}
public boolean matches(byte[] suspect)
{
String string = new String(suspect).toLowerCase();
if (string!= null && string.indexOf(search) != -1)
return true;
else
return false;
}
public void filterClose()
{
try
{
if (inputstream != null)
{
inputstream.close();
}
if (datainputstream != null)
{
datainputstream.close();
}
}
catch ( Exception error)
{
}
}
}

Thanks
R Vashi

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s