Second Query说,结果集已关闭

时间:2015-10-24 22:02:29

标签: java sql sql-server jdbc

我有2节课。问题是,如果我尝试执行第二个查询,我得到的结果集是关闭的异常。第二个查询rs.next返回true。你能告诉我在哪个位置关闭了错误的资源吗?如果我从releaseResources()方法中删除this.queryResult.close()this.statement.close(),则错误为"连接已关闭。我不是每次调用都会创建一个新的连接实例,preparedstatement和resultset吗?

主:

public static void main(String[] args) throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException, InterruptedException {
                DatabaseConnectionHelper con = new DatabaseConnectionHelper();
                ResultSet rs = con.executeQuery("SELECT * FROM [winfiler].[dbo].[customer_data]");
                System.out.println(rs.next());
                while(rs.next()){
                    System.out.println(rs.getString("name"));
                }
                con.releaseResources();
                ResultSet rs3 = con.executeQuery("SELECT * FROM [winfiler].[dbo].[customer_data]");
                System.out.println(rs3.next());
                while(rs3.next()){
                    System.out.println(rs.getString("name"));
                }
    }

数据库连接的Helperclass:

public class DatabaseConnectionHelper {

    private Connection connection=null;
    private PreparedStatement statement=null;
    private ResultSet queryResult=null;

    public Connection connectToDatabase(){
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            connection = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=******;user=********;password=********");
            return connection;
        } catch (ClassNotFoundException ex) {
            MessageHelper.createClassNotFoundExceptionMessage("Eine exception ist aufgetreten", "Classe konnte nicht gefunden werden (Verbindung zur Datenbank)", ex.getMessage());
        } catch (SQLException ex) {
            MessageHelper.createSQLExceptionMessage("Eine exception ist aufgetreten", "Probleme beim der Datenabfrage - DatabaseConnectionHelper", ex.getMessage());
        }
            return null;
    }

    public ResultSet executeQuery(String query){
        try{
            statement = connectToDatabase().prepareStatement(query);
            queryResult = statement.executeQuery();


            return queryResult;
        }catch (SQLException ex) {
            MessageHelper.createSQLExceptionMessage("Eine exception ist aufgetreten", "Probleme beim der Datenabfrage - DatabaseConnectionHelper", ex.getMessage());
        }
        return null;
    }

    public boolean releaseResources(){
        try {
            this.queryResult.close();
            this.statement.close();
            this.connection.close();
            return true;
        } catch (SQLException ex) {
            MessageHelper.createSQLExceptionMessage("Eine exception ist aufgetreten", "Resourcen konnten nicht freigegeben werden - DatabaseConnectionHelper", ex.getMessage());
        }
        return false;
    }
}

2 个答案:

答案 0 :(得分:1)

您的实际问题是您在第二次循环中拨打rs.getString("name");而不是rs3.getString("name");

您的connectToDatabase()会创建新连接。因此,只要您不同时使用DBConnectionHelper,这就不是问题。

BTW1:你应该避免使用System.out(rs.next());,因为它有副作用并跳过第一行。 BTW2:您的releaseResources()可以改进:您应该忽略/分别捕获RS和Satement中的异常,或者至少关闭连接本身。

答案 1 :(得分:0)

问题在于,当您在第二个查询之前调用con.releaseResources()时,您实际上是在释放连接对象。