Operation not allowed after ResultSet closed

1

I'm having a problem with a method whose function is to return a list of messages sent by the user (assuming the user is an attribute of the method class)

The code in question is the following:

private final String url = "jdbc:mysql://localhost:3306/mensajeria";
private final String driver = "com.mysql.jdbc.Driver"; 

public List<Mensaje> verMensajesEnviados() {
    List<Mensaje> mensajes = new LinkedList<>();

    iniciarConexion();
    if (conexion != null && usuario != null) {
        try {
            try (Statement st = conexion.createStatement()) {
                String consulta =
                        "SELECT * FROM mensajes WHERE de='" + 
                            usuario.getEmail() + "'";
                try (ResultSet res = st.executeQuery(consulta)) {
                    while (res.next()) {
                        Usuario u = getMsgPara(res.getString("para"));
                        if (u != null) {
                            String texto = res.getString("texto");
                            Mensaje msg = new Mensaje(usuario, u);
                            msg.setCuerpo(texto);
                            mensajes.add(msg);
                        }
                    }
                }
            }
        } catch (SQLException ex) {
            System.out.println(ex.getMessage() + 
                    ". >>> Error de Obtencion de Mensajes Enviados!!");
        } finally {
            finalizarConexion();
        }
    }
    return mensajes;
}

public Usuario getMsgPara(String email) {
    Usuario u = null;
    iniciarConexion();
    if (conexion != null) {
        try {
            try (Statement st = conexion.createStatement()) {
                String selectSQL =
                        "SELECT * FROM mensajes WHERE para='" + email + "'";
                try (ResultSet res = st.executeQuery(selectSQL)) {
                    if (res.next()) u = getUsuario(email);
                }
            }
        } catch (SQLException ex) {
            System.out.println(ex.getMessage() + 
                    ". >>> Error de Obtencion de Usuario!!");
        } finally {
            finalizarConexion();
        }
    }
    return u;
}

public Usuario getUsuario(String email) {
    Usuario u = null;
    iniciarConexion();
    if (conexion != null) {
        try {
            Statement st = conexion.createStatement();
            String selectSQL = 
                "SELECT * FROM usuarios WHERE email='" + email + "'";
            ResultSet res = st.executeQuery(selectSQL);
            while (res.next()) {
                String nombre = res.getString("nombre");
                String apellidos = res.getString("apellidos");
                String pwd = res.getString("password");

                u = new Usuario(nombre, apellidos, email, pwd);
            }
        } catch (SQLException ex) {
            System.out.println(ex.getMessage() + 
                    ". >>> Error de Obtencion de Usuario!!");
        } finally {
            finalizarConexion();
        }
    }
    return u;
}

private void iniciarConexion() {
    if (conexion == null) {
        try {
            Class.forName(driver);
            conexion = DriverManager.getConnection(url, "root", "");
        } catch (ClassNotFoundException ex) {
            System.out.println(ex.getMessage() + 
                    ". >>> Error de Conexion 1!!");
        } catch (SQLException ex) {
            System.out.println(ex.getMessage() + 
                    ". >>> Error de Conexion 2!!");
        }
    }
}

private void finalizarConexion() {
    if (conexion != null) {
        try {
            conexion.close();
            conexion = null;
        } catch (SQLException ex) {
            System.out.println(ex.getMessage() + 
                    ". >>> Error de Desconexion!!");
        }
    }
}

The problem is that I always throw the exception

Operation not allowed after ResultSet closed. >>> Error de Obtencion de Mensajes Enviados!!

I've been reviewing the code from top to bottom and I do not see what the problem is, I've been combing the internet looking to solve the problem but there's no way.

    
asked by Álvaro Rojas 27.03.2017 в 17:01
source

2 answers

0

The problem is that you have a Connection object that you reuse in the two methods getMsgPara and getUsuario. In both you initialize and close it, but always on the same instance.

The sequence would be:

  • getMsgTo: startConnection ()
  • getMsgFor: call to getUser
  • getUser: startConnection ()
  • getUser: finalize Connection ()
  • getMsgFor: you work with a Connection object already closed, and it gives the error.
  • The easiest thing you can do is to remove the Connection connection property from the class and reuse it and leave the code like this:

    private Connection iniciarConexion() {
            try {
                Class.forName(driver);
                return DriverManager.getConnection(url, "root", "");
            } catch (ClassNotFoundException ex) {
                System.out.println(ex.getMessage() + 
                        ". >>> Error de Conexion 1!!");
            } catch (SQLException ex) {
                System.out.println(ex.getMessage() + 
                        ". >>> Error de Conexion 2!!");
            }
    }
    
    private void finalizarConexion(Connection conexion) {
        if (conexion != null) {
            try {
                conexion.close();
                conexion = null;
            } catch (SQLException ex) {
                System.out.println(ex.getMessage() + 
                        ". >>> Error de Desconexion!!");
            }
        }
    }
    public List<Mensaje> verMensajesEnviados() {
        List<Mensaje> mensajes = new LinkedList<>();
    
        Connection conexion = iniciarConexion();
        if (conexion != null && usuario != null) {
            try {
                try (Statement st = conexion.createStatement()) {
                    String consulta =
                            "SELECT * FROM mensajes WHERE de='" + 
                                usuario.getEmail() + "'";
                    try (ResultSet res = st.executeQuery(consulta)) {
                        while (res.next()) {
                            Usuario u = getMsgPara(res.getString("para"));
                            if (u != null) {
                                String texto = res.getString("texto");
                                Mensaje msg = new Mensaje(usuario, u);
                                msg.setCuerpo(texto);
                                mensajes.add(msg);
                            }
                        }
                    }
                }
            } catch (SQLException ex) {
                System.out.println(ex.getMessage() + 
                        ". >>> Error de Obtencion de Mensajes Enviados!!");
            } finally {
                finalizarConexion(conexion);
            }
        }
        return mensajes;
    }
    public Usuario getMsgPara(String email) {
        Usuario u = null;
        Connection conexion = iniciarConexion();
        if (conexion != null) {
            try {
                try (Statement st = conexion.createStatement()) {
                    String selectSQL =
                            "SELECT * FROM mensajes WHERE para='" + email + "'";
                    try (ResultSet res = st.executeQuery(selectSQL)) {
                        if (res.next()) u = getUsuario(email);
                    }
                }
            } catch (SQLException ex) {
                System.out.println(ex.getMessage() + 
                        ". >>> Error de Obtencion de Usuario!!");
            } finally {
                finalizarConexion(conexion);
            }
        }
        return u;
    }
    
    public Usuario getUsuario(String email, Connection conexion) {
        Usuario u = null;
            try {
                Statement st = conexion.createStatement();
                String selectSQL = 
                    "SELECT * FROM usuarios WHERE email='" + email + "'";
                ResultSet res = st.executeQuery(selectSQL);
                while (res.next()) {
                    String nombre = res.getString("nombre");
                    String apellidos = res.getString("apellidos");
                    String pwd = res.getString("password");
    
                    u = new Usuario(nombre, apellidos, email, pwd);
                }
            } catch (SQLException ex) {
                System.out.println(ex.getMessage() + 
                        ". >>> Error de Obtencion de Usuario!!");
            } 
        }
        return u;
    }
    
        
    answered by 27.03.2017 / 17:44
    source
    0

    It may be because you are not closing the connection correctly, you may have a statement running at any given time, the best option is to close ( st.close () ) the first statement and execute the second.

        
    answered by 27.03.2017 в 17:42