Weird Behavior of ResultSet

hello mates hope you having a nice day, i have a really weird problem with getting data from Mysql database in Java , in the method here :

 ResultSet tableExistence;
 .
 .
 .

    while (tableExistence.next()) {

          System.out.println("before : "+tableExistence.getInt(MyHttpServer.COL_ID));

        if(tableExistence.getString(MyHttpServer.COL_STATUS).equals(MyHttpServer.STATUS_AVAILABLE)){

          System.out.println("after : "+tableExistence.getInt(MyHttpServer.COL_ID));    

       ...
 }

weirdly the value of the "before" is for the right value of the id , but after the if method, the value of "after" returns some thing like 1234125151231 , any idea why is this problem ?!!!!

Jon Skeet
people
quotationmark

The ResultSet documentation states:

The docs do say "For maximum portability, result set columns within each row should be read in left-to-right order, and each column should be read only once."

So technically, it's somewhat reasonable. However:

  • I would actually expect that any modern, well-supported database would allow you to access the columns in an arbitrary order, multiple times
  • Just returning an incorrect value rather than throwing an exception to indicate the problem is pretty nasty

One explanation could be if you're fetching a clob or blob - the driver may decide in that case that "left to right only" is more reasonable, to avoid having to keep too much data in memory. It's still not great that it just returns corrupted data though.

So while this is a possible answer, I would also:

  • Check that neither the connection nor the ResultSet is being used from multiple threads concurrently
  • Check that you're using a recent version of the MySQL driver

people

See more on this question at Stackoverflow