File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes incorrect results returning from my DB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "incorrect results returning from my DB" Watch "incorrect results returning from my DB" New topic
Author

incorrect results returning from my DB

Brendan Cregan
Ranch Hand

Joined: Nov 11, 2011
Posts: 35
Hi All,

Im working with a small database for a simple enough quiz servlet program, i have the db working ok, it just prints out questions, im also using the DB to store the row of a random questions so that i can look up the correct answers between servlet instances.(this could be the complete wrong way to approach the problem, but it seems to work ok).my only problem is when i try to retrieve the row of the answer, im using microsoft access and i can see from the access program that its storing the correct question row as an int, but when i try to retrieve it im getting an error saying its a boolean which seems unusual since its storing correctly in the database.

this is from my checkAnswer servlet where im attempting to retrieve the answerRow, and its giving an invalid type of boolean.


any help would be much appreciated!

Go Raibh Maith Agat!


Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

resultset.next() tells you if there are any more rows in the result set.
Remember a result set can contain many columns for a single row, and lots of rows.

.
this page has a full example of how to use result sets.

http://docs.oracle.com/javase/tutorial/jdbc/basics/retrieving.html
Brendan Cregan
Ranch Hand

Joined: Nov 11, 2011
Posts: 35
Wendy Gibbons wrote:resultset.next() tells you if there are any more rows in the result set.
Remember a result set can contain many columns for a single row, and lots of rows.

.
this page has a full example of how to use result sets.

http://docs.oracle.com/javase/tutorial/jdbc/basics/retrieving.html


thanks Wendy, i have implemented this


but the problem is i need to read the value outside of the while loop, is there any other way to implement the answerRow.getInt() without the while()?

Thanks

Brendan
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

It isn't the loop which is the problem. The problem is that you declared the answerRow variable inside the loop, thus restricting its scope so that it is only visible to code inside the loop. Since you want it to be visible outside the loop, then you have to declare it outside the loop.

Don't confuse declaring the variable (int answerRow) with assigning a value to it (answerRow = answerID.getInt(1)). You can do those two things separately, and in this case you should. Declare the variable outside (before) the loop, assign it the value inside the loop.
Brendan Cregan
Ranch Hand

Joined: Nov 11, 2011
Posts: 35
Paul Clapham wrote:It isn't the loop which is the problem. The problem is that you declared the answerRow variable inside the loop, thus restricting its scope so that it is only visible to code inside the loop. Since you want it to be visible outside the loop, then you have to declare it outside the loop.

Don't confuse declaring the variable (int answerRow) with assigning a value to it (answerRow = answerID.getInt(1)). You can do those two things separately, and in this case you should. Declare the variable outside (before) the loop, assign it the value inside the loop.


thanks , I did understand the problem, but didnt know that declaring it ouside the loop was the solution!
Thanks Paul!

Brendan
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

as you are only expecting 1 row you could do:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: incorrect results returning from my DB