I am trying to execute query select count(*) from users thru jdbc, but get this exception:
Here is the code:
Does it mean that my driver does not support this? Also, will I get a faster result if I do select count(*) or rs.last(); rs.getRow(); ? thanks, Alex [ April 30, 2004: Message edited by: Alex Kravets ]
All right brain, you don't like me and I don't like you, but let's just do this one thing so I can get back to killing you with beer.<br /> <br />- Homer Simpson
Generally, you want to use 'select count(*)' rather than rs.last(). Think about what would happen if your query could result in 1,000,000 rows and you called rs.last(): the database would have to step through all the rows to get to the one you ask for. Your first set of code should work, assuming the query returns a row. It normally should, but you can test it by using this code:
If the query returns no rows, then it is possible that the database or driver does not support count(*), or that there is something else wrong with some other code of yours that you did not post. Your second example will never work. You are asking the result set for a column named 'id,' however your SQL command does not retrieve this column. It retrieves 'count(id).' Use rs.getInt(1) or rs.getString(1).
Alex, Your second example won't work for two reasons. One is what Kevin pointed out. You can get around that by using an index or an explicit column name, like select count(id) as "id" from users Also, you need to call rs.getNext() before you call rs.getString() or rs.getInt(). Otherwise the resultset is not pointing to a valid row and you get exceptions like you did.