0

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

公共类 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();
    }


}

}

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

4

1 回答 1

0

你给 spobj.extract(rs);打了两次电话,

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

为此,您必须使用可滚动的结果集

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

并且对于下一次调用spobj.extract(rs);reverse 调用结果集rs.previous()

于 2013-10-29T12:35:42.167 回答