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.
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.
Posted by: mike | May 29, 2003 02:00 AM
Tony
Actually that does make sense. I was curious about that. Your explanation makes much more sense.
Posted by: Tony | May 29, 2003 07:02 AM
Darren
I think what you wanted to say was
Then you can say
nid = rs.getInt("max_userid");Posted by: Darren | May 29, 2003 12:32 PM
Eric
SQL in JSP? I think I have that somewhere on my list of bad ideas...
Posted by: Eric | May 29, 2003 09:00 PM
Tony
Why is SQL in JSP a bad idea? Should it be done via a bean? Why not in a scriptlet?
Posted by: Tony | May 30, 2003 06:28 AM
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.
Posted by: Mike | June 2, 2003 08:37 AM
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).
Posted by: Eric | June 3, 2003 01:22 PM