Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to count rows - count(*) and Java

 
Giovanni De Stefano
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
I would like to check if a record exists or not in a table. I have been searching the forum and I have found this topic that is 5 years old! :-)

In that discussion, someone suggested to use something like:

using the database functionality for better performances instead of using next() or last() and getRow() on the ResultSet object returned by a select * query.

My problem is that I have no idea how to use Java to send the query and have back an int instead of a ResultSet.

Any ideas or suggestions? Thank you for your help!
Giovanni
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65337
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will get back a resultset with a single row that contains a single column containing the count.
 
Shailesh Chandra
Ranch Hand
Posts: 1082
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Giovanni De Stefano:

My problem is that I have no idea how to use Java to send the



query and have back an int instead of a ResultSet.



Giovanni,

I would recommend first you read about JDBC.
here is the link for jdbc. It will help you to start with.

for your problem you can execute the query as



then after executing query you will get a resultset let say rs and you can fetch count as rs.getInt("COUNT")

hope this helps

Shailesh
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shailesh Chandra:
[CODE]
then after executing query you will get a resultset let say rs and you can fetch count as rs.getInt("COUNT")

hope this helps

Shailesh

Giovanni,
Note that you have to call rs.next() before the rs.getInt() call.
 
Giovanni De Stefano
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your help...
...but it doesn't work!

This is the code I use (without try/catch blocks):

But I get SQLException: Column 'COUNT' not found
...any ideas?

Thank you again
Giovanni
[ May 16, 2005: Message edited by: Giovanni De Stefano ]
 
James Swan
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


or modify your query to something like:

"select count(*) as MY_COUNT..."

then you can lookup a named column in your result set:
result.getInt("MY_COUNT");
[ May 16, 2005: Message edited by: James Swan ]
 
Giovanni De Stefano
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THANK YOU SO MUCH!!!

It does work!!!

Giovanni
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic