NoBug. 2021-11-18 13:26 采纳率: 50%
浏览 36
已结题

ResultSet.close()一调用就出错Operation not allowed after ResultSet closed。不用就行


```java
package cn.JDBC;

import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.Properties;

public class JDBCSignIn {

    public static boolean sign(String username,int pswd) throws SQLException {
        Connection con = null;
        PreparedStatement stmt;
        ResultSet res = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql:///db1","root","113277468");
            System.out.println("连接驱动成功");
        } catch (ClassNotFoundException | SQLException e) {
            System.out.println("连接驱动失败");
        }
        String sql = "select * from signin where name= ? and pw = ?";
        try {
            stmt = con.prepareStatement(sql);
            stmt.setString(1,username);
            stmt.setInt(2,pswd);
            res = stmt.executeQuery();
        } catch (SQLException e) {
            System.out.println("查询失败");
        }
        res.close();
        stmt.close();
        con.close();
        return res.next();
    }

}
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/093619212736193.png "#left")
加了释放资源就出错


![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/248530312736158.png "#left")
把那三句删了就好。。。
是什么原因嘛,求教

```

  • 写回答

2条回答 默认 最新

  • CSDN专家-sinJack 2021-11-18 13:31
    关注

    res.close();
    return res.next();
    这不是很明显吗,你把res对象都关闭了,还能返回?
    对象关闭一般放在finally块中,避免异常情况下,某个对象没有关闭,造成资源占用。

    public static boolean sign(String username,int pswd) throws SQLException {
            Connection con = null;
            PreparedStatement stmt;
            ResultSet res = null;
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                con = DriverManager.getConnection("jdbc:mysql:///db1","root","113277468");
                System.out.println("连接驱动成功");
            } catch (ClassNotFoundException | SQLException e) {
                System.out.println("连接驱动失败");
            }
            String sql = "select * from signin where name= ? and pw = ?";
            try {
                stmt = con.prepareStatement(sql);
                stmt.setString(1,username);
                stmt.setInt(2,pswd);
                res = stmt.executeQuery();
                 return res.next();
            } catch (SQLException e) {
                System.out.println("查询失败");
            }finally{
                 res.close();
                stmt.close();
                con.close();
            }
           return null;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月26日
  • 已采纳回答 11月18日
  • 创建了问题 11月18日

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名