Next, I'd like to: If the book is already borrowed from the bank, show to borrowers, and if not, allow the loan. So, if there was already this loan, I'd like to return this information to Control (who called it) and display it, because if I try to display it in DAO BEFORE closing the resultSet, it gives the error:
java.sql.SQLException: After end of result set
And if I try to SHOW AFTER closing the resultSet, it is not possible since it has been closed.
Is it possible to save this information (name, date of loan, responsible) in a list or something, and return to the method you called?
Method in CONTROL that calls DAO:
public void RealizaEmprestimo (String tituloLivro, String isbn, Date dataE,
String horaE, Date dataD, String horaD,
double multa, String funcionario, String responsavel,
String tipoResponsavel)
throws SQLException {
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dataFormatadaE = formatter.format(dataE);
String dataFormatadaD = formatter.format(dataD);
Emprestimo emprestimo = new Emprestimo (dataFormatadaE, horaE, dataFormatadaD, horaD,
multa, funcionario, responsavel,tituloLivro,
isbn, tipoResponsavel);
if(ed.checkEmprestimo(emprestimo)){
**Aqui é onde seria exibida as informações retornadas da DAO**
}
else {
ed.insert(emprestimo);
JOptionPane.showMessageDialog(null, "Emprestimo realizado com sucesso!");
}
}
DAO Method:
public boolean checkEmprestimo (Emprestimo emprestimo) throws SQLException {
PreparedStatement stmt= c.prepareStatement("SELECT * FROM emprestimo WHERE isbn = ? ");
stmt.setString(1, emprestimo.getIsbn());
ResultSet rs = stmt.executeQuery();
boolean hasEmprestimo = rs.next();
if (!rs.next()) {
String tipoResponsavel = rs.getString("tipoResponsavel");
String responsavel = rs.getString("responsavel");
String dataEmprestimo = rs.getString("dataEmprestimo");
String horaE = rs.getString("horaE");
String dataDevolucao = rs.getString("dataDevolucao");
String horaD = rs.getString("horaD");
JOptionPane.showMessageDialog(null, "Emprestado para o " + tipoResponsavel + " " +
responsavel + " no dia " + dataEmprestimo + " às " +horaE + "\n\n" +
"Entrega prevista para " + dataDevolucao + (" às ") + horaD);
}
stmt.close();
rs.close();
return hasEmprestimo;
}
PS: If I shot the IF, it works IF IF you have the loan in the bank, but if it does not, it gives an error in assigning those values to the variables.
And I would not like to handle this if and else in DAO, because in MVC the one who takes care of logic is the controller.