• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

a strange problem on "rs.getString()"

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
 
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
JulianInactive KennedyInactive
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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



 
She's out of the country right now, toppling an unauthorized dictatorship. Please leave a message with this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic