ResultSet在执行statement.execute(sql)时关闭

时间:2013-10-25 18:34:36

标签: resultset

在执行以下代码时,我收到错误java.sql.SQLException:ResultSet已关闭

公共类SavePoints {

public void extract(ResultSet rs)
{
     int c;
    try {
        while(rs.next())
        {
        c = rs.getInt("id");
       String d=rs.getString("name");
       String e=rs.getString("city");
       String f=rs.getString("state");
       String g=rs.getString("country");

    //Displaying values
     System.out.println("ID is:"+c+"\tName is:"+d+"\tCity is:"+e+"\tState is:"+f+"\tCountry is:"+g);
        }
    } catch (SQLException e1) {
        e1.printStackTrace();
    }

}

public static void main(String[] args) {
    SavePoints spobj=new SavePoints();
    try {
        Connection con=DriverManager.getConnection("jdbc:odbc:Divya", "SYSTEM", "tiger");
        Statement st=con.createStatement();
        ResultSet rs=st.executeQuery("select * from publishers");
        spobj.extract(rs);
        //DatabaseMetaData databaseMetaData = con.getMetaData();
        //System.out.println(databaseMetaData.getDriverMajorVersion());
        //Savepoint sp=con.setSavepoint("Deleting Rows");
        st.execute("delete from publishers where id=104");
        //con.rollback(sp);
        spobj.extract(rs);
    } catch (SQLException e) {

        e.printStackTrace();
    }


}

}

错误是什么?我找不到它了。请告诉我。我是新手..所以请简单解释一下。我很感谢你的帮助。谢谢:))

1 个答案:

答案 0 :(得分:0)

你曾两次拨打pobj.extract(rs);

第一次执行此函数时,结果集移动到最后,因为您正在使用rs.next() ..

要实现这一点,你可以使用可滚动的结果集

Statement st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, 
                                    ResultSet.CONCUR_READ_ONLY);

并且spobj.extract(rs);的下一次调用会反转rs.previous()

对结果集的调用