I get the following error:
com.ibm.db2.jcc.am.SqlException: [jcc][t4][1090][10899][4.32.28] Invalid operation to read at current cursor position. ERRORCODE=-4476, SQLSTATE=02501
running the following "query":
final String sql = "SELECT COUNT(A1.ID) AS C1 FROM MY_SCHEMA.MY_TABLE A1 ";
try (final Connection connection = dataSource.getConnection();
final Statement statement = connection.createStatement();
final ResultSet resultSet = statement.executeQuery(sql)) {
Long count = resultSet.getObject(1, Long.class);
} catch (Exception ex) {
log.error("Error in query \"{}\"", sql, ex);
throw new RuntimeException(ex.getMessage(), ex);
}
I tried to use * instead of ID, added semicolon in the end, no alias for table...
You need to use resultSet.next()
like this:
Long count = 0L;
if (resultSet.next()) {
count = resultSet.getObject(1, Long.class);
}
...
Or:
Long count = resultSet.next() ? resultSet.getObject(1, Long.class) : 0L;
resultSet.next()
: Advances the cursor to the first row. Without this call, accessing the data in the ResultSet will throw an error.