GeeCON Prague 2014*
The moose likes JDBC and the fly likes How to check if resultset has only one row or more? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to check if resultset has only one row or more?" Watch "How to check if resultset has only one row or more?" New topic
Author

How to check if resultset has only one row or more?

Vidya Moorthy
Ranch Hand

Joined: Nov 13, 2003
Posts: 57
I have to do a query to a table. If resultset has only one row, I have to set the bean in one particular way and if the resultset has multiple rows in a different way.
I tried


The problem with this code is even if the row count from the query is one, it sets ABC into the bean.
Should I try to get the count and then check if count > 1? If that is the case, then I have to do two queries, one to get the count and the other to do the query itself. Please advise.
Thanks in advance
Vidyaram

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Just read the records to find out how many there are:


But if it were me I would be considering writing a query involving "count(*)" rather than potentially returning a large number of rows just to ignore them.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Paul Clapham wrote:But if it were me I would be considering writing a query involving "count(*)" rather than potentially returning a large number of rows just to ignore them.

Indeed; this is the best way
Vidya Moorthy
Ranch Hand

Joined: Nov 13, 2003
Posts: 57
Thank you for posting the answers quickly!

Vidyaram
 
GeeCON Prague 2014
 
subject: How to check if resultset has only one row or more?