数据库操作错误之“Operation not allowed after ResultSet closed”

进行数据库查询时出现的错误:

select * from `book_info` where `Name` = 'asa'

java.sql.SQLException: Operation not allowed after ResultSet closed
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
    at com.mysql.jdbc.ResultSet.checkClosed(ResultSet.java:666)
    at com.mysql.jdbc.ResultSet.next(ResultSet.java:7274)
    .... ....

    at java.lang.Thread.run(Thread.java:619)

查询的错误代码如下:

        DbBean db=new DbBean();
        String sql = "select * from `book_info` where `Name` = 'asa' ";//like '%"+inputer+"%' ";
        String ss = "flush privileges";
        ResultSet rs=null;
        ArrayList<String> newdata = new ArrayList<String>();
         try{
            rs=db.executeQuery(sql);
            db.executeUpdate(ss);

            while(rs.next()){
                   for(int i=3;i<=8;i++){
                       newdata.add(rs.getString(i));
                   }
           }
        }catch(Exception e){
            e.printStackTrace();         
        }finally{
            rs.close();
            db.close();
        }

>>以上代码出现错误并非是statement创建多个对象所致(就一个rs而已)

>>问题在于只有一个rs的前提下,某些执行,比如excuteQuery()执行完成后会自动把ResultSet关掉

>>如何避免?

>>>>分清楚哪些执行完后要关RS,即哪些excute需要RS,哪些不需要

>>>>多个sql语句执行避免用一个RS对象


猜你喜欢

转载自blog.csdn.net/u012935646/article/details/41768665
今日推荐