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

[3507 byte] By [karan88a] at [2007-11-27 2:18:32]
# 1
HiYou are using setRecord() method. It will overwrite the previous record, So first u delete the previous recod and insert the new record using addRecord() method.bye
NelsonJosepha at 2007-7-12 2:18:02 > top of Java-index,Java Mobility Forums,Java ME Technologies...