• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to count rows - count(*) and Java

 
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1087
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
THANK YOU SO MUCH!!!

It does work!!!

Giovanni
 
What a show! What atmosphere! What fun! What a tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic