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

select count(*) does not work

 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Kevin Mukhar
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34401
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic