This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

How to count rows - count(*) and Java  RSS feed

 
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
 
Author and ninkuma
Marshal
Posts: 66783
168
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.
 
Ranch Hand
Posts: 1084
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
 
author & internet detective
Marshal
Posts: 38502
653
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 ]
 
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
Boost this thread!