Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if resultset has one row or more?

Tags:

How to check if resultset has one row or more with JDBC?

like image 689
TopCoder Avatar asked Apr 07 '10 10:04

TopCoder


People also ask

How do I find the number of rows in a ResultSet?

Getting the number of rows using methodsThe last() method of the ResultSet interface moves the cursor to the last row of the ResultSet and, the getRow() method returns the index/position of the current row.

How do I get only one row in a ResultSet?

You can use absolute to navigate to the first row: ResultSet rs = ...; rs. absolute(1); // Navigate to first row int id = rs. getInt("id"); ...

How do you know if ResultSet contains data?

The JDBC ResultSet doesn't provide any isEmpty(), length() or size() method to check if its empty or not. Hence, when a Java programmer needs to determine if ResultSet is empty or not, it just calls the next() method and if next() returns false it means ResultSet is empty.

How can I count the ResultSet in SQL?

You can get the column count in a table using the getColumnCount() method of the ResultSetMetaData interface. On invoking, this method returns an integer representing the number of columns in the table in the current ResultSet object.


2 Answers

ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table1"); boolean isMoreThanOneRow = rs.first() && rs.next(); 

You didn't ask this one, but you may need it:

boolean isEmpty = ! rs.first(); 

Normally, we don't need the row count because we use a WHILE loop to iterate through the result set instead of a FOR loop:

ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table1"); while (rs.next()) {     // retrieve and print the values for the current row     int i = rs.getInt("a");     String s = rs.getString("b");     float f = rs.getFloat("c");     System.out.println("ROW = " + i + " " + s + " " + f); } 

However, in some cases, you might want to window the results, and you need the record count ahead of time to display to the user something like Row 1 to 10 of 100. You can do a separate query with SELECT COUNT(*) first, to get the record count, but note that the count is only approximate, since rows can be added or removed between the time it takes to execute the two queries.

Sample from ResultSet Overview

like image 171
Marcus Adams Avatar answered Oct 16 '22 23:10

Marcus Adams


There are many options, and since you don't provide more context the only thing left is to guess. My answers are sorted by complexity and performance ascending order.

  1. Just run select count(1) FROM ... and get the answer. You'd have to run another query that actually selects and returns the data.
  2. Iterate with rs.next() and count until you're happy. Then if you still need the actual data re-run same query.
  3. If your driver supports backwards iteration, go for rs.next() couple of times and then rewind back with rs.previous().
like image 44
mindas Avatar answered Oct 16 '22 23:10

mindas