| Author |
ResultSet check if there are any values
|
Reggie McDougal
Ranch Hand
Joined: Sep 27, 2004
Posts: 69
|
|
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
|
You can never drink too much
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56521
|
|
I tried rs.next() method but this is unreliable
How so?
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Bob Rocks
Greenhorn
Joined: Oct 21, 2004
Posts: 23
|
|
|
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
Joined: Jun 06, 2003
Posts: 15
|
|
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
internet detective
Marshal
Joined: May 26, 2003
Posts: 26489
|
|
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.
|
[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
|
 |
Reggie McDougal
Ranch Hand
Joined: Sep 27, 2004
Posts: 69
|
|
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 ]
|
 |
 |
|
|
subject: ResultSet check if there are any values
|
|
|