• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ResultSet check if there are any values

 
Reggie McDougal
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to check a ResultSet to see if it contain any rows, I tried rs.next() method but this is unreliable first() thows an exception on a forward coursor

I just need to check if the rs contains any rows at all if any one can help.

Reg
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65118
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried rs.next() method but this is unreliable


How so?
 
Bob Rocks
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you use rs.next() and the result set is null, you will probably need to use a do while instead of just a while for the loop... never had a problem myself though, maybe if we had some more info on your specific problem.
 
Rudy Simon Yeung
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the code snippets and it works for me.

Statement st1 = db2Conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
System.out.println("Search Begin: " + new java.util.GregorianCalendar().getTime());
ResultSet resultSet1 = null;
try {
resultSet1 = st1.executeQuery(formatSQLQuery(aContext, REMITCCYIND_DONT_CARE, null));
} catch (java.sql.SQLException e)
{
releaseResources(resultSet1, st1, db2Conn);
throw new java.sql.SQLException();
}

if (isToBank)
{
if (resultSet1.first()) // CBID code found in branch file
{
.....

and you can use beforeFirst() to reset the pointer of the result set.

Rudy
[ November 10, 2004: Message edited by: Rudy Simon Yeung ]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34839
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reggie,
rs.next() is fine and the recommended way of doing this:

Note that a database will never return null for the resultset after doing a query. It will return an empty resultset.
 
Reggie McDougal
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats what I was doing but the weirdest thing, the recordSet would not return all the records that met the criteria,

I mean I execute the query then check it with rs.next() and oracle would just return some of the records not all, not to mention the fact that if there is data it skips the first record, which I don't want.

So I reckon Rudy has the right Idea beforeFirst().

I'm using a while(rs.next()) and writing the data to an ArrayList to get the number of values that way and it works perfectly.

Thats what I meant by unreliable.

Reg

[ November 10, 2004: Message edited by: Reggie McDougal ]
[ November 10, 2004: Message edited by: Reggie McDougal ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic