线程“main”中的异常java.sql.SQLException:ResultSet关闭后不允许操作

时间:2021-12-09 01:28:10

So, I'm getting the error in the title while executing the below process and I really don't know what can be wrong.

所以,我在执行下面的过程时得到了标题中的错误,我真的不知道什么是错的。

What I'm doing is trying to bring some data from a database to create an object and fill and arraylist with several of it's objects, if not found should throw an exceptio

我正在做的是尝试从数据库中提取一些数据来创建一个对象,并用它的几个对象填充和arraylist,如果找不到则应该抛出异常

public ArrayList<Caso> buscarCasosPorJuez(String cedula) throws java.sql.SQLException,Exception{
    java.sql.ResultSet rs;
    String sql;
    Caso caso;
    ArrayList<Caso> casos = new ArrayList();
    sql="SELECT * "+
    "FROM tcaso "+
    "WHERE CedulaJ='"+cedula+"';";
    Conector.getConector().ejecutarSQL(sql);
    rs = Conector.getConector().ejecutarSQL(sql,true);

    while (rs.next()){
        caso = new Caso(
            rs.getInt("NumCaso"),
            rs.getString("DescripcionCaso"),
            rs.getString("EstadoCaso"),
            rs.getDate("FechaCreacion").toLocalDate(),
            persona.buscarJuezPorCedula(rs.getString("CedulaJ")),
            persona.buscarQuerellante(rs.getString("CedulaQ"))
            );
        casos.add(caso);
    }

    rs.close();
    return casos;
}

Below are the two methods the "New Caso" is calling on the two final lines.

下面是两个方法,“新卡索”呼吁两个最后一行。

public Juez buscarJuezPorCedula(String cedula) throws java.sql.SQLException,Exception{
    Juez juez = null;
    java.sql.ResultSet rs;
    String sql;
    sql = "SELECT Sala,Usuario,Clave,NombreJ,ApellidosJ,TelefonoJ,CedulaJ "+
    "FROM tjuez "+
    "WHERE CedulaJ='"+cedula+"';";
    rs = Conector.getConector().ejecutarSQL(sql,false);

    if (rs.next()){
        juez = new Juez(
            rs.getInt("Sala"),
            rs.getString("Usuario"),
            rs.getString("Clave"),
            rs.getString("NombreJ"),
            rs.getString("ApellidosJ"),
            rs.getString("TelefonoJ"),
            rs.getString("CedulaJ"));
    } else {
        throw new Exception ("Persona no encontrada intentelo de nuevo.");
        }

    rs.close();
    return juez;
}

public Querellante buscarQuerellante(String cedula) throws java.sql.SQLException,Exception{
    Querellante querellante = null;
    java.sql.ResultSet rs;
    String sql;
    sql = "SELECT DireccionQ,NombreQ,ApellidosQ,TelefonoQ,CedulaQ "+
    "FROM tquerellante "+
    "WHERE CedulaQ='"+cedula+"';";
    rs = Conector.getConector().ejecutarSQL(sql,true);

    if (rs.next()){
        querellante = new Querellante(
            rs.getString("DireccionQ"),
            rs.getString("NombreQ"),
            rs.getString("ApellidosQ"),
            rs.getString("TelefonoQ"),
            rs.getString("CedulaQ"));
    } else {
        throw new Exception ("Persona no encontrada intentelo de nuevo.");
    }

    rs.close();
    return querellante;
}

Thanks for all the help you can provide.

感谢您提供的所有帮助。

1 个解决方案

#1


1  

I suspect you are closing the Statement object in ejucutarSQL(), which closes all ResultSets derived from it.

我怀疑你正在关闭ejucutarSQL()中的Statement对象,它关闭了从它派生的所有ResultSet。

You can't actually write all-purpose SQL-executing methods like that, at least not without using CachedRowSet, which costs memory.

实际上,您无法编写这样的通用SQL执行方法,至少在没有使用CachedRowSet的情况下,这会花费内存。

You need to restructure this code along more conventional lines, with explicit Connector and Statement objects as local variables so you can close them in reverse order of acquisition.

您需要沿着更常规的行重构此代码,并将显式的Connector和Statement对象作为局部变量,以便您可以按获取的相反顺序关闭它们。

You should be using PreparedStatements instead of building arguments into SQL strings. If you throw an exception you are also leaking the result set.

您应该使用PreparedStatements而不是在SQL字符串中构建参数。如果抛出异常,也会泄漏结果集。

For example:

public Querellante buscarQuerellante(String cedula) throws java.sql.SQLException,Exception{
    String sql = "SELECT DireccionQ,NombreQ,ApellidosQ,TelefonoQ,CedulaQ "+
        "FROM tquerellante "+
        "WHERE CedulaQ=?";
    try (Connnector conn = ...; // TODO
        PreparedStatement ps = conn.prepareStatement(sql);
        )
    {
        ps.setObject(1, cedula);
        try (ResultSet rs = ps.execute())
        {
            if (rs.next()){
                return new Querellante(
                    rs.getString("DireccionQ"),
                    rs.getString("NombreQ"),
                    rs.getString("ApellidosQ"),
                    rs.getString("TelefonoQ"),
                    rs.getString("CedulaQ"));
            } else {
                throw new Exception ("Persona no encontrada intentelo de nuevo.");
            }
        }
    }
}

Note that try-with-resources always closes everything allocated, and in the reverse order of acquisition.

请注意,try-with-resources始终会关闭所有已分配的内容,并且采用相反的顺序。

#1


1  

I suspect you are closing the Statement object in ejucutarSQL(), which closes all ResultSets derived from it.

我怀疑你正在关闭ejucutarSQL()中的Statement对象,它关闭了从它派生的所有ResultSet。

You can't actually write all-purpose SQL-executing methods like that, at least not without using CachedRowSet, which costs memory.

实际上,您无法编写这样的通用SQL执行方法,至少在没有使用CachedRowSet的情况下,这会花费内存。

You need to restructure this code along more conventional lines, with explicit Connector and Statement objects as local variables so you can close them in reverse order of acquisition.

您需要沿着更常规的行重构此代码,并将显式的Connector和Statement对象作为局部变量,以便您可以按获取的相反顺序关闭它们。

You should be using PreparedStatements instead of building arguments into SQL strings. If you throw an exception you are also leaking the result set.

您应该使用PreparedStatements而不是在SQL字符串中构建参数。如果抛出异常,也会泄漏结果集。

For example:

public Querellante buscarQuerellante(String cedula) throws java.sql.SQLException,Exception{
    String sql = "SELECT DireccionQ,NombreQ,ApellidosQ,TelefonoQ,CedulaQ "+
        "FROM tquerellante "+
        "WHERE CedulaQ=?";
    try (Connnector conn = ...; // TODO
        PreparedStatement ps = conn.prepareStatement(sql);
        )
    {
        ps.setObject(1, cedula);
        try (ResultSet rs = ps.execute())
        {
            if (rs.next()){
                return new Querellante(
                    rs.getString("DireccionQ"),
                    rs.getString("NombreQ"),
                    rs.getString("ApellidosQ"),
                    rs.getString("TelefonoQ"),
                    rs.getString("CedulaQ"));
            } else {
                throw new Exception ("Persona no encontrada intentelo de nuevo.");
            }
        }
    }
}

Note that try-with-resources always closes everything allocated, and in the reverse order of acquisition.

请注意,try-with-resources始终会关闭所有已分配的内容,并且采用相反的顺序。