During analyze legacy part of project which I'm working at, I've found following method:
public int getCount(String tableName, Connection connection) throws SQLException {
try (Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(
"SELECT COUNT (*) FROM " + tableName
)) {
return resultSet.getInt(1);
}
}
In my mind, after query execution ResultSet has cursor before first row, and to move it, I should invoke some of navigation methods, like next(). And only then invoke getXXX() to obtain value of cell. From javadocs method next() summary
Moves the cursor forward one row from its current position. 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 the second row the current row, and so on.
So, can anybody explain how it works here?