Is it possible to return the values of a Resultset?

Asked

Viewed 215 times

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.

  • 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.

  • if (!rs.next()) You mean you’re gonna get into if if there is no further information on ResultSet (see the documentation). And when you try to use getString in a ResultSet which no longer has any data, "After end of result set" error occurs. It is right to do if (rs.next()), or, in your case, do if (hasEmprestimo) (call for next() will again move the ResultSet for the next record, then you should not call several times if it is to check only one result).

1 answer

1

The ideal, in these search methods, is not to return boolean, but the very entity (or object) you are seeking at the base. That’s because, as you yourself realized, return a boolean in this case has little use for the sequence your program needs. It would not be much better to do a search and, finding, return a Livro or, not finding, return null or even a Livro empty, for example? You get the same confirmation effect as the boolean gives you, but with the advantage of having the object available to work if it exists.

So instead of your method return a boolean, return an object created by you and that would contain the recovered data in the ResultSet. For example, a class Livro:

class Livro {
  private String tipoResponsavel;
  private String responsavel;
  ...
  //Construtores e getters/setters
}

There will be a small change in your DAO method. You instead of setting hasEmprestimo according to the search, will feed the object Livro to return it later:

ResultSet ...

Livro livro;
if (rs != null && rs.next()) {
    livro = new Livro();
    livro.setTipoResponsavel = rs.getString("tipoResponsavel");
    ... //setar as outras propriedades
}

return livro;

Now, always a Livro will be returned. If there is data in the database, one filled; if there is not, a null book.

The signature of his method, then, would be like this:

public Livro checkEmprestimo (Emprestimo emprestimo) throws SQLException {

In the controller, your logic would also change:

Emprestimo emprestimo ...

Livro livroPesquisado = ed.checkEmprestimo(emprestimo); //livro preenchido ou nulo

if(livroPesquisado != null) {
  //sua lógica aqui, acessando o objeto livroPesquisado e fazendo o que quiser
} else {
...
}

This way, you have your object and can work with it easily and uncomplicated.

Finally, an architectural observation/design. Your DAO class should not be responsible for anything other than the database query. Displaying a message there to the user, as you do today, should be the responsibility of a service class, which receives data from your View (the user interface) and connects to the DAO, or from the Controller class itself.

Other things are methods/builders with a lot of parameters. They are highly not recommended due to the difficulty of maintenance and readability. Search for a Pattern design called Builder.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.