Is it possible to return the values of a ResultSet?

1

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.

    
asked by anonymous 16.06.2018 / 21:54

1 answer

1

Ideally, in these search methods, it is not to return boolean , but the very entity (or object) you are searching for at the bottom. This is because, as you yourself realize, returning boolean in this case has little use for the sequence your program needs. Would not it be much better to do a search and, finding, return Livro or, not finding, return null or even Livro empty, for example? You get the same confirmation effect that boolean gives you, but with the advantage of having the object available to work if it exists.

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

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

There will be a slight 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, a Livro will always be returned. If there is data in the database, one is filled; if not, a null book.

The signature of your method, then, would look like this:

public Livro checkEmprestimo (Emprestimo emprestimo) throws SQLException {

In controller , your logic would change too:

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 {
...
}

In this way, you have your object and can work with it in an easy and uncomplicated way.

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

Another thing is methods / constructors with lots of parameters. They are highly not recommended due to the difficulty of maintenance and legibility. Search for a design pattern named Builder .

    
16.06.2018 / 22:43