public int findTotalCount() throws SQLException {
String sql = "select count(*) from kg_goodstype ";
ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
ps.close();
if(rs.next()) {
return rs.getInt(1);
}
else
{
return 0;
}
}
为什么会报这个错误:Operation not allowed after ResultSet closed
调用ps.close的时候rs也被关闭了,你需要把ps.close放到最后面去
因为你在操作结果之前就关闭了数据库,close必须在你操作结果之后关闭
// 打开连接 Connection
// 创建 PrepareStatement
// 获取 结果集 ResultSet
// 关闭结果集 ResultSet
// 关闭 PrepareStatement
// 关闭连接 Connection
代码修改如下:
public int findTotalCount() throws SQLException {
String sql = "select count(*) from kg_goodstype ";
ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
return rs.getInt(1);
} else {
return 0;
}
ps.close();
}