Hi friends,
I've got error while retrieving records from RMS, after deleting a record in it. The error message is that InvalidRecordID exception.
My doubt is, after deleting a record in RMS, how can we retrieve records. Say for example, an RMS contains five records. I just delete 3 record. Upto this everying goes fine. But while retrieving remaining records after deletion, now it shows only first two records. Remaining two records are not displayed and it shows the above error message.
I hereby included the source code for the deletion application. Kindly give your idea regarding the same.
Expecting for your response,
Chandru
------------------------------------------------
Here you have to give input as any data and store them by clicking menu command and select Add option in it. If you select Retrieve command it will show the records in RMS. To delete a record say 3, you just give input as 3 and select Delete option from Menu command. Now it will delete 3rd record successfully.
But, if you try to retrieve remaining records now only first two records will be displayed and while accessing third record you will be getting error.
Here is the code.
------------------------------------------------
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
public class RSDelete extends MIDlet implements CommandListener
{
private Command retrieveCommand;
private Command okCommand;
private Command deleteCommand;
private Display display;
private TextBox t = null;
String dbn=new String("Sample");
RecordStore rs=null;
int currpos,maxchar;
public RSDelete()
{
display = Display.getDisplay(this);
retrieveCommand = new Command("Retrieve", Command.OK, 2);
okCommand = new Command("Add", Command.OK, 2);
deleteCommand = new Command("Delete", Command.OK, 2);
t = new TextBox("Enter your data :", "",256, 0);
t.addCommand(retrieveCommand);
t.addCommand(okCommand);
t.addCommand(deleteCommand);
t.setCommandListener(this);
}
public void startApp()
{
display.setCurrent(t);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void commandAction(Command c, Displayable s)
{
try
{
if(c == okCommand)
{
rs=RecordStore.openRecordStore(dbn,true);
String name = "";
name=new String(t.getString());
byte[] data=name.getBytes();
rs.addRecord(data,0,name.length());
System.out.println(" Record is updated");
rs.closeRecordStore();
}
if(c == deleteCommand)
{
rs=RecordStore.openRecordStore(dbn,true);
int id = Integer.parseInt(t.getString());
int totalRecords = rs.getNumRecords();
boolean flag = false;
for(int i = 1; i <= totalRecords; i++)
{
if(i == id)
{
rs.deleteRecord(id);
flag = true;
break;
}
}
if(flag)
System.out.println("record :"+id+" is deleted ");
else
System.out.println("No match found");
rs.closeRecordStore();
} /* end of Add command */
if(c == retrieveCommand)
{
rs=RecordStore.openRecordStore(dbn,true);
String result = "";
byte[] data = null;
System.out.println("Rows in the rs are :");
System.out.println("records in rms :"+rs.getNumRecords());
int totalRecords = rs.getNumRecords();
for(int i = 1; i <= totalRecords; i++)
{
data = rs.getRecord(i);
result = new String(data);
System.out.println(i+": " + result);
}
rs.closeRecordStore();
}
}catch(RecordStoreException e)
{
System.out.println(" Error :"+e);
}
} /* end of command Action */
} /* end of class */
-----------------------------------------------------------