java.sql.SQLException:结果集已关闭

时间:2012-04-02 18:57:29

标签: java resultset sqlexception

当我尝试执行我的代码时,我收到错误:

  

错误[TripDAO] TripDAO:findAllTripsByActivityId()
                java.sql.SQLException:结果集已关闭。

我收到错误的方法如下:

public List<Trip> findAllTripsByActivityId(int id) {
    List<Trip> trips = new ArrayList<Trip>();

    if (obtainConnection()) {
        try {
            PreparedStatement stm = null;
            ResultSet rs = null;
            stm = con.prepareStatement(findAllTripsByActivityIdQuery
                    .toString());
            logger.info("Wybrano id: " + id);
            stm.setInt(1, id);
            rs = stm.executeQuery();
            while (rs.next()) {
                trips.add(setInObject(rs));
            }
        } catch (Exception e) {
            logger.error("TripDAO: findAllTripsByActivityId() " + e);
        } finally {
            closeConnection();
        }
    }
    return trips;
}

public Trip setInObject(ResultSet rs) throws SQLException {
    Trip trip = new Trip();
    int id = rs.getInt("id");
    trip.setId(id);
    ...
    return trip;
}

我不知道这段代码可能出错了什么。你能救我吗?

1 个答案:

答案 0 :(得分:0)

您正在将整个ResultSet传递给setInObject方法,这可能会破坏它。尝试从findAllTripsByActivityId方法中的结果集中提取值。