Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

incorrect results returning from my DB

 
Brendan Cregan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as you are only expecting 1 row you could do:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic