May 28, 2003

Weird JSP and MySQL Error

I was trying to run a query and get the Max ID of the ResultSet.
ResultSet rs = stmt.executeQuery("SELECT max(userid) " +
                                   "FROM answers");
	while (rs.next())
	{
		nid = rs.getInt("userid");
What was happening was that I was getting an error Column userid not found. Which was really odd, because the column was there. Thank God for the Java Forums, because what I had to add in the last line was: nid = rs.getInt(1); Not sure as to why this is. But it works. Properly. Either an oddity of JSPs or MySQL.
Post Info

Tagged As Coding, MySQL

Comments are Open (7)

Posted at 03:30 PM

Comments

mike

I think this is because the select statement isn't actually returning a column value, but
the *result* of executing the MAX function on that column value, so you need to reference
the result, rather than the actual column name.

Hope that makes sense.

Tony

Actually that does make sense. I was curious about that. Your explanation makes much more sense.

Darren

I think what you wanted to say was

  SELECT max(userid) AS max_userid FROM answers

Then you can say

  nid = rs.getInt("max_userid");

Eric

SQL in JSP? I think I have that somewhere on my list of bad ideas...

Tony

Why is SQL in JSP a bad idea? Should it be done via a bean? Why not in a scriptlet?

Mike

It's not necessarily a bad idea if you've only got a few JSPs but the MVC (model view controller) design allows the seperation of presentation / content / logic which makes things much easier to maintain in the long run.

Eg. Use Servlets for your business logic, put data into beans and the JSP just deals with presentation
and uses the values held in the beans.

Eric

I agree w/Mike. IMHO there should be no SQL in JSP and no HTML in Classes. As far as using Servlets, I prefer it but its often not allowed, or not worth it. Even if you are POSTing to JSP you still shouldn't have any SQL in there. Its just one of those things thats a little weird at first, but after a while you see the benefits (like taking the TV out of your bedroom).

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

Search
Comments on this post
Categories
Info
Tony Stephens
Copyright © 1995-2005
Site Version:
10
Licensed:
Creative Commons
Validate:
XHTML, CSS, 508, RSS
Subscribe
Blog-Fu, Link-Fu