Add a record in recordset doesnt append at the end of record, but replace deleted record.

Posted on 2005-05-03
Medium Priority
Last Modified: 2008-02-01
Hi all,

Let's say i have three records in a record set: Record 1,2,3. When i deleted record 2, now there is: Record 1,3. When i add a new record again, it shows that the new record is added as record 4,
I suppose the sequence will be 1,3,4. But when i recreate a new recordset, create a recordenumeration and try to read from the beginning..what i read out is record 1,4,3. :O!? Looks like the record 4 is not appended at the end of the recordset but replaced the deleted record?

I guess somehow the recordset pointer is still pointing at the location od the deleted record?
my code is quite standard, i always open a recordstore and close it after use. like this:
when deleting record 2 (simplified..)
recordStore = RecordStore.openRecordStore(recordStoreName,true)
when inserting new record
recordStore = RecordStore.openRecordStore(recordStoreName,true)
byte[] b = .........;
int ID = recordStore.addRecord(b, 0, b.length); // this ID shows the record added is record 4 recordStore.closeRecordStore();
When enumerating again
db2 =RecordStore.openRecordStore(picRsName,true);
RecordEnumeration recordEnumeration = db2.enumerateRecords(null,null,false);
    int id = recordEnumeration.nextRecordId();
    System.out.println("reading >> record ID:" + id); //******************Over here, the output is 1, 4,3!
    ByteArrayInputStream bais = new ByteArrayInputStream(db2.getRecord(id));
    ...............reading bais...

p/s: To ensure addRecord() really add at the new record AFTER the last record, i even tried to go through a nextRecord() loop and makesure the pointer is pointing at last record before i call addRecord(), but the sequence read out is still wrong.
Question by:MrTerrence
LVL 16

Expert Comment

ID: 13923963
Pls show the second sql statement. Is the second recordset sorted by id?
Actually, no matter what data sequence stored in the db, but how to get it in your sequence by programming.
LVL 16

Expert Comment

ID: 13923983
Is the second sql statement look like this?


'ORDER BY ID' will sort the recordset by 'ID' field ascendently.

Author Comment

ID: 13924096
Hi Gnoon,

I m not sure did u misunderstood anything, hmm but I m working on a J2ME mobile application. The recordset i mention is within the Record Management System (RMS)...so i didnt use any SQL in sorting actually.

Actually i didnt sort at all when i try to read out the records again, as shown in part 3 of the code, i just open the recordset, create an enumeration, then loop through each record. i thought this will give me the record one by one in the sequence how i added them.

any clues?

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 16

Expert Comment

ID: 13924256
Sorry man, maybe I cant help you because I' not familiar with J2ME :-/ .
LVL 10

Accepted Solution

edwardiii earned 400 total points
ID: 13929019
Hi, MrTerrence.

According to this URL (http://www-128.ibm.com/developerworks/wireless/library/wi-rms/) under "Deleting records", it mentions when you delete a record from a RecordStore, the ID that record had (in your case, "2") in no longer used in the RecordStore.  So it seems logical that an enumeration will return 1,4,3, because when you add a new record, the RecordStore drops it in the first open "hole", which would be where Record 2 used to be.

So, given that apparently-by-design functionality, can you sort your RecordStore records to get the output desired?  I'm wondering if you can use the RecordComparator methods listed in the same page in "Listing 7. Comparing records and determine relative sort order"?

Assisted Solution

hkang042997 earned 400 total points
ID: 13932077
If you want to keep your data sorted by the order of record entry, you might add a couple of bytes to the front of your records to store the record ID and sort using the RecordComparator interface mentioned by edwardiii above.

You can try setting the data length to 0 (last parameter) instead of deleting the record.  This is equivalent to giving the record a null value which you can then filter out the null records.

recordStore.setRecord(recordID, b, 0, 0)

See http://developers.sun.com/techtopics/mobility/midp/ttips/recordstores/ from where the following bit of code was taken.

public class MyFilter implements RecordFilter {
  public boolean matches( byte[] recordData ){
    return( recordData.length > 0 && recordData[0] != 0 );

RecordEnumeration recordEnumeration = db2.enumerateRecords(new MyFilter(),null,false);


Author Comment

ID: 13932342
Though i didnt get a confirmative explaination on the scenario above (and i cant find any documentation abt it), but both the methods mentioned are workable. Thank you.

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month15 days, 22 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question