什么是“查询返回多个ResultSet”的解决方案

时间:2012-05-09 11:36:27

标签: java postgresql resultset

我收到错误:

ERROR [NewsDAO] findAll(): org.postgresql.util.PSQLException: Multiple ResultSets were returned by the query.

我正在使用postgresql-8.4-703.jdbc4.jar。

我的代码如下:

private static StringBuilder findAllQuery = new StringBuilder();

    {
    findAllQuery.append("SELECT * FROM news;");
}

    public List<News> findAll() {
    Statement stm = null;
    ResultSet rs = null;

    List<News> results = new ArrayList<News>();
    if (obtainConnection()) {
        try {
            stm = con.createStatement();
            rs = stm.executeQuery(findAllQuery.toString());
            while(rs.next())
                results.add(setInObject(rs));
        } catch (Exception e) {
            logger.error("findAll(): " + e);
        } finally {
            logger.info("Zamknalem");
            closeConnection();
        }
    }
    return results;
}
    public News setInObject(ResultSet rs) throws SQLException {
    News news = new News();
    news.setId(rs.getInt("id"));
    news.setTitle(rs.getString("title"));
    news.setDescription(rs.getString("description"));
    //TODO: timestamp
    news.setDate(rs.getDate("date"));
    User user = new User();
    user.setId(rs.getInt("user_id"));
    news.setUser(user);
    news.setActive(rs.getBoolean("active"));
    return news;
}

我不知道为什么会收到此错误。有什么想法吗?

1 个答案:

答案 0 :(得分:14)

findAllQuery.toString()必须有multiple select statements

<强> 修改

正如JB Nizet指出的那样,你应该尝试从语句中删除分号。 Postgresql JDBC驱动程序在分号上拆分语句,因此可能会发出两个语句。