为什么这个 JDBC select return 没有任何行?

Why doesn't this JDBC select return any rows?

我有以下 SQL 语句,我试图通过 JDBC PreparedStatement 执行:

SELECT TOP(1) * FROM offsets sto WHERE sto.done = 0 AND sto.instance_index = 1

在我的数据库中,这很高兴 returns 1 行。

但是一旦我在 JdbcTemplate 中执行它:

private final String selectSql = "SELECT TOP(1) * " +
            "FROM offsets sto " +
            "WHERE sto.done = 0 " +
            "AND sto.instance_index = ? ";

template.query(selectSql, new Object[]{1}, new int[]{Types.INTEGER} ,rs -> {

    Offset offset = new Offset();
    offset.setId(rs.getLong("id"));
    offset.setInstance_index(rs.getInt("instance_index"));
    offset.setDone(rs.getBoolean("done"));

    return Optional.of(offset);

});

我得到:

Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The result set has no current row.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:237) ~[mssql-jdbc-9.4.0.jre11.jar:na]
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.verifyResultSetHasCurrentRow(SQLServerResultSet.java:563) ~[mssql-jdbc-9.4.0.jre11.jar:na]
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getterGetColumn(SQLServerResultSet.java:2046) ~[mssql-jdbc-9.4.0.jre11.jar:na]

这是怎么回事?我做错了什么?

结果集在返回的第一个行之前开始。通话

 rs.next();
 Offset offset = new Offset();
 offset.setId(rs.getLong("id")); 
 . . .

boolean next()

throws SQLException

Moves the cursor froward 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. When a call to the next method returns false, the cursor is positioned after the last row.

Resultset