Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with resultset.getInt in servlet

Status
Not open for further replies.

Stretchwickster

Programmer
Apr 30, 2001
1,746
GB
Hi,

I'm just starting out with Java servlets and I'm having a problem with something that should be simple.

I've made the following declaration of a resultset variable:
Code:
ResultSet rs = null;
Further on in my servlet I execute a select query:
Code:
rs = stmt2.executeQuery(SQLStatement);

I then run a while loop to iterate through and display the query results in an html table:
Code:
while ( rs.next() )
{
  //stored_bookid = rs.getInt("bookid");
  stored_bookid = 1;
  stored_title = rs.getString("title");
  stored_author = rs.getString("author");
  stored_price = rs.getString(&quot;price&quot;);				  out.println(&quot;<TR><TD><FONT COLOR='#FFFFFF'>&quot; + stored_title + &quot;</FONT></TD>&quot; );
  ...
}
I can retrieve the title, author and price strings without a problem, but the servlet bombs out when it reaches the first line in the while loop (which I have now commented). Instead, I've hard-coded stored_bookid to test that the rest of my code works - and it does. It's just when I use getInt that the program flies out into a catch statement. Anyone got any ideas?

Clive [infinity]
 
Hi,

Is stored_bookid an int datatype ? coz rs.getInt(String); returns int variable.

int stored_bookid;

while (rs.next()){
stored_bookid = rs.getInt(&quot;bookid&quot;);
}

OR
while (rs.next()){
stored_bookid = Integer.parseInt(rs.getString(&quot;bookid&quot;));
}

Hope it will help you..

Cheers
Venu



 
bearing in mind that the java String datatype is mapped to a SQL VARCHAR or VARCHAR2 variable, and a java int dataype is mapped to SQL NUMBER (generally, but depending on db vendor).
 
Yes, stored_bookid was declared as an int.

The problem was with my SQL statement and thus was undetectable to you guys!

I had the following:
Code:
SELECT title, author, price FROM book ...
instead of:
Code:
SELECT bookid, title, author, price FROM book ...

So I was trying to access a column that didn't exist in the ResultSet. I hate that blindness that causes programmers to fail to see their own obvious errors!

Thanks for your posts anyway!

Clive [infinity]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top