This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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
 
Marshal
Posts: 67163
169
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: 1085
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
Posts: 38906
684
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!