醒时风拂衣,情动忽提笔 2019-12-31 22:36 采纳率: 50%
浏览 1239
已采纳

jdbc连接数据库,查询成功返回数据但是还是报错java.sql.SQLException: Operation not allowed after ResultSet closed 这是怎么回事?

try {
Class.forName("com.mysql.jdbc.Driver");
String ur1="jdbc:mysql://localhost:3306/lingshi";
String username="root";
String password="root";
Connection con=DriverManager.getConnection(ur1,username,password);
Statement stmt=con.createStatement();
String sql1="select * from guogan";
ResultSet rs1=stmt.executeQuery(sql1);

    while(rs1.next()){
        String id=rs1.getString(1);
        System.out.print(id+"\t");
        String name=rs1.getString("name");
        System.out.print(name+"\t");
        String price=rs1.getString(3);
        System.out.print(price+"\t");
        String weight=rs1.getString("weight");
        System.out.print(weight+"\t");
        String birthdate=rs1.getString("birthdate");
        System.out.print(birthdate+"\t");
        String enddate=rs1.getString("enddate");
        System.out.print(enddate+"\t");
        String PD=rs1.getString("PD");
        System.out.print(PD+"\t");

        rs1.close();
        stmt.close();
        con.close();
    }
} catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}catch(SQLException e1){
    e1.printStackTrace();
}
我的结果如下
1   芒果干   13.00   106g    2019.12.29  2020.12.29  12months    java.sql.SQLException: Operation not allowed after ResultSet closed
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:963)
  • 写回答

2条回答

  • threenewbee 2020-01-01 09:45
    关注
        rs1.close();
        stmt.close();
        con.close();
    
    这些写在while循环后面,而不是里面
    

    问题解决的话,请点下采纳

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决