This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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 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
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.