How do I get the row count in JDBC?

You’re going to have to do this as a separate query, for example: SELECT COUNT(1) FROM table_name Some JDBC drivers might tell you but this is optional behaviour and, more to the point, the driver may not know yet. This can be due to how the query is optimised eg two example execution strategies in … Read more

How to get row count using ResultSet in Java?

If you have access to the prepared statement that results in this resultset, you can use connection.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); This prepares your statement in a way that you can rewind the cursor. This is also documented in the ResultSet Javadoc In general, however, forwarding and rewinding cursors may be quite inefficient for large result sets. … Read more

ResultSet.getString(1) throws java.sql.SQLException: Invalid operation at current cursor position

You should use the next statement first. ResultSet set = statement.executeQuery(); if (set.next()) { userName = set.getString(1); //your logic… } UPDATE As the Java 6 Documentation says A ResultSet cursor is initially positioned before the first row; the first call to the method next makes the first row the current row; the second call makes … Read more

Get Number of Rows returned by ResultSet in Java

First, you should create Statement which can be move cursor by command: Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); Then retrieve the ResultSet as below: ResultSet rs = stmt.executeQuery(…); Move cursor to the latest row and get it: if (rs.last()) { int rows = rs.getRow(); // Move to beginning rs.beforeFirst(); … } Then rows variable will contains … Read more