aspose file tools*
The moose likes JDBC and the fly likes problem while retrieving one row & column from resultset Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "problem while retrieving one row & column from resultset Object" Watch "problem while retrieving one row & column from resultset Object" New topic
Author

problem while retrieving one row & column from resultset Object

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

I m executing a quary using Statement interface which returns only one row & column
which i m accessing through like

can any one has any solution for this ?
thanks


The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

It's not clear what your exact problem is. Do you get runtime errors, compilation errors, etc.? If yes, please include them here. One thing I noticed is this: rs.next vs rs.next() And are you using a ResultSet or something else? You code snippet doesn't show it clearly.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Draw a diagram of the execution pathway through that loop. Draw lines representing the scope of the empid variable. Confirm that your result set actually contains any lines.
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

Campbell Ritchie wrote:Draw a diagram of the execution pathway through that loop. Draw lines representing the scope of the empid variable. Confirm that your result set actually contains any lines.

thanks for the reply , but I had solved this problem , cursor was i guess on the current row and there was only 1 row and 1 column so in that case we dont have to loop through the result set we just have to get the value like resultset or rs.getString( 1 );
rs = resultset object
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

naved momin wrote:thanks for the reply , but I had solved this problem , cursor was i guess on the current row and there was only 1 row and 1 column so in that case we dont have to loop through the result set we just have to get the value like resultset or rs.getString( 1 );
rs = resultset object

The cursor gets positioned on the first row by calling rs.next() or some other method in the ResultSet interface, so instead of guessing, it would be better to have a look at the actual code. If you're learning JDBC now, it is better to clarify all ambiguities right now, rather than get burned later on.

Whenever you intend to fetch exactly one row from the database, you should always check whether it is just one row being returned (ie. at least and at most one row). I generally do so using these statements:

This makes it clear to anyone dealing with your code (including yourself a year from now) that you really expect just one row from the database, and also helps to spot and identify potential bugs easily. I do so even for statements which are guaranteed to return only one row, such as SELECT COUNT(*) FROM SOME_TABLE. This way the checks will catch situation when the query itself is changed without necessary updates to the related code.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem while retrieving one row & column from resultset Object