我怎样才能 return 一个结果集?

How can I return a ResultSet?

我想寻求帮助。

我需要 return 结果的方法。 但是我得到一个异常:java.sql.SQLException:ResultSet 关闭后不允许操作

public class A implements AutoCloseable {

    protected Connection getConnection() {
        Connection connection = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(url, user, password);
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return connection;
    }

    public ResultSet GetResultSet() {

        ResultSet resultSet = null;

        try (Connection connection = getConnection()) {

            Statement statement = connection.createStatement();

            if (statement.execute(SELECT_ALL_USERS)) {
                resultSet = statement.getResultSet();
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return resultSet;
    }

    @Override
    public void close() throws SQLException {

    }
}

我不知道该怎么办。问题出在哪里? 你能给我任何提示吗?

非常感谢您的帮助。

也许我应该覆盖 ResultSet 接口中的 next() 方法。但是我不知道怎么做。

您使用 try-with-resources 语句获取连接:

try (Connection connection = getConnection()) {

该语句的重点是在最后关闭连接。关闭连接也会关闭它的语句和结果集。

不要return一个ResultSet。 Return List 个对象。

请也遵守 Java 命名约定。