• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a strange problem on "rs.getString()"

 
Daniel Washington
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ResultSet rs=statement.executeQuery("select a,b from table1");
String str1=rs.getString("a"); //this is surely correct

String str2=rs.getString("a");

but when do rs.getString("a") again,it is wrong and some strange error happen ,why?the "rs" still wait on the current cursor,why can't i do the
same rs.getString("a") two times?

anybody have the same experience please give me help,thx a lot!!
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the Javadoc for ResultSet (always a good place to start):

The ResultSet interface provides getter methods (getBoolean, getLong, and so on) for retrieving column values from the current row. Values can be retrieved using either the index number of the column or the name of the column. In general, using the column index will be more efficient. Columns are numbered from 1. 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.

Basically it's saying it's not guaranteed to work, perhaps because it's not an explicit requirement of the JDBC spec.

My stock response to queries such as, "I'm doing something that I probably shouldn't be and it doesn't work or behaves strangely", is "Don't do that then!" Best practice is to call the method once and store the returned value in a variable if you need it again. As it says, do it in order too; that was a new one on me!

Jules
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This was put in there to allow the driver to stream the results at the field level. So when you call rs.getString(2), the driver might just read the second field of the current record from the server's data stream and return that to you - with no way to go back to the first field, or even to return the field a second time.

- Peter
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aaaah, I getcha! Always nice to have a proper explanation.

Thanks Peter.
 
Daniel Washington
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thx to peter and Jules anyway.
But I am puzzled that if the results is stream.we know
"rs.getString(1)
rs.getString(2)
"which sentence of them is first is doesn't matter,so we can sure
the content is just fixed by the index or columnname,then why we cannot
find that content by columnname once a time?
just cannot understand.

Originally posted by Peter den Haan:
This was put in there to allow the driver to stream the results at the field level. So when you call rs.getString(2), the driver might just read the second field of the current record from the server's data stream and return that to you - with no way to go back to the first field, or even to return the field a second time.

- Peter


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic