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.