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")
把那三句删了就好。。。
是什么原因嘛,求教

```

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;
    }

把 return res.next();这行代码放到try里面,关闭连接的放到finally中 最return null;这样改一下

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