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

Posted on 2005-05-03
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

    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

    Is the second sql statement look like this?


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

    Author Comment

    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) 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?

    LVL 16

    Expert Comment

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

    Accepted Solution

    Hi, MrTerrence.

    According to this URL ( 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"?
    LVL 4

    Assisted Solution

    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 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

    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

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    hash value 2 29
    Return ZipOutputStream using Servlet 2 27
    java continue statement 10 52
    parentbit challenge 3 37
    If you have upgraded to Java2 update 10 on a Microsoft Windows client, you may have discovered that your Java application does not work as it did before.  For example, the colors of your Java2D graphic may be all wrong for no apparent reason. Aft…
    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    The viewer will learn how to implement Singleton Design Pattern in Java.
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

    730 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now