我嘗試通過 JDBC PreparedStatement 執行以下 SQL 陳述句:
SELECT TOP(1) * FROM offsets sto WHERE sto.done = 0 AND sto.instance_index = 1
在我的資料庫中,這很高興回傳 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]
到底是怎么回事 ?
我究竟做錯了什么 ?
uj5u.com熱心網友回復:
結果集在回傳的第一行之前開始。稱呼
rs.next();
Offset offset = new Offset();
offset.setId(rs.getLong("id"));
. . .
布爾下一個()
拋出 SQLException
將游標從其當前位置向前移動一行。ResultSet 游標最初位于第一行之前;第一次呼叫 next 方法使第一行成為當前行;第二次呼叫使第二行成為當前行,依此類推。當呼叫 next 方法回傳 false 時,游標位于最后一行之后。
結果集
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/427419.html
