How to get row count using ResultSet in Java?

To get the row count using a ResultSet object in Java, you can use the last() method to move the cursor to the last row, and then use the getRow() method to get the row number:

ResultSet rs = statement.executeQuery("SELECT * FROM table");
rs.last();
int rowCount = rs.getRow();

Note that this approach is only accurate if the ResultSet object contains all the rows of the query result. If the ResultSet object is scrollable and does not contain all the rows, the row count may not be accurate.

Alternatively, you can use the getFetchSize() method to get the number of rows fetched from the database. This method returns the number of rows that are fetched from the database at a time when the ResultSet object is populated.

int rowCount = rs.getFetchSize();

I hope this helps. Let me know if you have any questions.