1
Next, I would like to: if the book is already "borrowed" in the bank, to show to those who borrowed it, and if not, allow the loan. With this, if this loan already existed, I would like to return this information to Control (who called) and display there, because if I try to display in DAO BEFORE closing the resultset, gives the error:
java.sql.SQLException: After end of result set
And if I try to display AFTER closing the resultset, it is not possible, since it has been closed.
There is the possibility to save this information (name, loan date, responsible) in a list or something, and return to the method called?
Method in CONTROL calling 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!");
}
}
Metodo DAO:
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 take it out of the IF, it works IF I have the loan in the bank, but if I don’t have it, it gives error when assigning these values to the variables.
Nor would I like to treat this if and Else in the DAO, because in the MVC standard who takes care of logic is the controller.
If it became difficult to understand, I would like IF I had the loan in the bank, which was returned to the CONTROLLER to be displayed there (the information), and leave the DAO only with the function of searching in the bank.
– Weslley Fillipe
The topics indicated do not work, that’s why I launched this new. They could inform the reason before giving negative and the doubt persists.
– Weslley Fillipe
if (!rs.next())
You mean you’re gonna get intoif
if there is no further information onResultSet
(see the documentation). And when you try to usegetString
in aResultSet
which no longer has any data, "After end of result set" error occurs. It is right to doif (rs.next())
, or, in your case, doif (hasEmprestimo)
(call fornext()
will again move theResultSet
for the next record, then you should not call several times if it is to check only one result).– hkotsubo