0
嘗試的recources給了我以下異常:重複使用PreparedStatement的原因的SQLException
值java.sql.SQLException:不允許操作的ResultSet關閉
我的代碼之後:
public Set<Tablet> viewAllTablets(int offset, int noOfRecords) throws OutOfRangeException {
Set<Tablet> tabletSet = new HashSet<>();
Tablet tablet = null;
try(Connection connection = dataSource.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT SQL_CALC_FOUND_ROWS * FROM tablets limit " + offset + ", " + noOfRecords + ";");
ResultSet resultSet = preparedStatement.executeQuery();
ResultSet resultSet1 = preparedStatement.executeQuery("SELECT FOUND_ROWS()");){
while (resultSet.next()){
tablet = new Tablet();
tablet.setTabletId(resultSet.getInt("idTablet"));
tablet.setName(resultSet.getString("name"));
tablet.setNeedRecepie(resultSet.getBoolean("need_recipe"));
tablet.setPrice(resultSet.getDouble("price"));
tablet.setTypeId(resultSet.getInt("type_id"));
tablet.setDescription(resultSet.getString("description"));
tablet.setTabletType(TypeFactory.getType(tablet.getTypeId()));
tablet.setWeight(resultSet.getDouble("weight_of_pack"));
tablet.setPillsCount(resultSet.getInt("pills_count"));
tabletSet.add(tablet);
}
if(resultSet1.next())
this.noOfRecords = resultSet1.getInt(1);
} catch (SQLException e) {
e.printStackTrace();
}
return tabletSet;
}
請包含ENTIRE,COMPLETE堆棧跟蹤,並在您的代碼中標識引發異常的行。沒有這些基本的信息,沒有人能幫助你。 –
嘗試使用資源自動關閉打開的資源,當try塊完成 – pahan
@pahan:雖然這是真的,但它與上述無關。在資源嘗試完成後,OP不訪問(也不能訪問)這些ResultSet。 –