Issue in adding records in RMS ! ! !
Hi all,
I am seting current time, Normal hours ,extrahours and total hours from TEXtFileds.
My requiement is that when i click the save command that time it should be saved in RMS.
But every time when i execute the MIDlet and click the save command it overwrite previous records so every time it gives same recordID.
iam sending my code please check it and Give me some suggestion .
public class TimesheetEntryRS {
private int id;
private long time;
private int todayhours;
private int extrahours;
private int totalhours;
RecordStore rs;
/* default constructor */
public TimesheetEntryRS () {
id=0;
time=0;
todayhours=0;
extrahours=0;
totalhours=0;
RecordStore rs=null;
try {
// open a record store named file
rs = RecordStore.openRecordStore("Time Entry RecordStore",true);
int ID = rs.getNextRecordID();
System.out.println("TD:"+ID+"Number of Records:"+rs.getNumRecords());
if(rs.getNumRecords()>0) {
byte data[] = rs.getRecord(ID-1);
// parse the record
ByteArrayInputStream bais= new ByteArrayInputStream(data);
DataInputStream dis= new DataInputStream(bais);
try {
time=dis.readLong();
todayhours=dis.readInt();
extrahours=dis.readInt();
totalhours=dis.readInt();
System.out.println("Time:"+time);
System.out.println("TodyaHours:"+todayhours);
System.out.println("ExtraHours:"+extrahours);
System.out.println("TotalHours:"+totalhours);
}catch(Exception e) {}
dis.close();
}
}catch(Exception e) {
System.out.println("Error: "+e.getMessage());
}finally {
if(rs!=null) {
try {
rs.closeRecordStore();
}catch(Exception e) {}
}
}
//
}
public boolean save() {
//
try {
rs = RecordStore.openRecordStore("Time Entry RecordStore",true);
} catch (RecordStoreFullException e1) {
e1.printStackTrace();
} catch (RecordStoreNotFoundException e1) {
e1.printStackTrace();
} catch (RecordStoreException e1) {
e1.printStackTrace();
}
//convert to byte array
byte data[]=null;
System.out.println("Working till save() in RSclass at 1:");
try {
ByteArrayOutputStream baos= new ByteArrayOutputStream();
DataOutputStream dos= new DataOutputStream(baos);
dos.writeLong(getTime());
dos.writeInt(getTodayhours());
dos.writeInt(getExtrahours());
dos.writeInt(getTotalhours());
data=baos.toByteArray();
//close
baos.close();
dos.close();
}catch(Exception e) {}
boolean success=false;
//save data to file
try {
System.out.println("Working till save() in RSclass at 2:");
// open a record store named file
int ID = rs.getNextRecordID();
if(rs.getNumRecords()>0) {
System.out.println("Working till save() in RSclass at 3:");
rs.setRecord(ID, data, 0, data.length);
}
else {
System.out.println("Working till save() in RSclass at 4:");
rs.addRecord(data,0,data.length);
}
success=true;
}catch(Exception e) {
System.out.println("Error: "+e.getMessage());
}finally {
if(rs!=null) {
try{
rs.closeRecordStore();
}catch(Exception e) {}
}
}
return success;
}
}
-
Best regards
karan

