我得到的标题中提到的错误从这个代码,我不知道为什么。。。
public String[] getLobbies() {
String sql = "SELECT * FROM lobby";
SQLResponse<ResultSet> response = unclosedOperate(PreparedStatement::executeQuery, sql);
SQLResponse<ResultSet> countResponse = unclosedOperate(PreparedStatement::executeQuery, "SELECT COUNT(*) AS count FROM lobby");
if (!response.hasResponse() || !countResponse.hasResponse()) return new String[0];
try {
if (countResponse.getResponse().next()) {
int count = countResponse.getResponse().getInt("count");
String[] array = new String[count];
if (response.getResponse().next()) {
for (int i = 0; i < count; i++) {
Logger.debug("count: " + count);
Logger.debug("i: " + i);
array[i] = response.getResponse().getString(i + 1);
}
}
return array;
}
return new String[0];
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(response.getResponse());
close(countResponse.getResponse());
}
return new String[0];
}
它正在把这个打印到控制台。。。
[07:14:57 ERROR]: count: 2
[07:14:57 ERROR]: i: 0
[07:14:57 ERROR]: count: 2
[07:14:57 ERROR]: i: 1
[07:14:57 WARN]: java.sql.SQLException: Column Index out of range, 2 > 1.
这不应该发生。。?sql是索引的,不是吗?列中有两个条目,我想检索这两个条目,但是当我检索第二个条目时,它会抛出错误,即使当我计算条目时,它显示其中有2个条目。。。哦,我的table现在看起来像这样。。。https://gyazo.com/8af53da8b78b38a63864ae5a1a8f43e6
1条答案
按热度按时间dphi5xsq1#
您遇到的问题不是遍历结果列表,而是尝试从响应访问结果集中的下一列。因为resultset只返回一列,所以只能访问
getString(1)
(列的偏移量为1,而不是从0开始)。而不是打电话
getResponse
然后循环,循环和调用getResponse
每一行,总是呼叫getString(1)
(或者更明确一点,使用实际的列名)。也就是说,这可以大大简化。你不需要得到
count
示例化数组。然后你就可以while
要遍历响应并构建数组。。。