Return a "SELECT * FROM" in the JavaWeb browser

0

Hello everyone, I'm studying JavaWeb and I came across a boring bug !! I already researched a lot on the subject but I could not solve this error.

I have this method that is pulling all data from the "contacts" table of the database and returning it in a list.

public   ArrayList<User> buscarTodos() {
    //Monta a Query
    String sql = "select * from contatos";
    //Constroi PreparedStatement com SQL
    ArrayList<User> lista = new ArrayList<User>();
    try {
        PreparedStatement preparador = con.prepareStatement(sql);

        ResultSet resultado = preparador.executeQuery();
        while (resultado.next()){
            User contato = new User();
            contato.setContato_id(resultado.getInt("contato_id"));
            contato.setContato_nome(resultado.getString("contato_nome"));
            contato.setContato_tel(resultado.getString("contato_tel"));
            contato.setContato_email(resultado.getString("contato_email"));
            contato.setContato_sobrenome(resultado.getString("contato_sobrenome"));

            lista.add (contato);
        }
        preparador.close();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return lista;

}

But when I go to show this list it returns only the last repeated contact in several times, as in the image below:

Followthecodebycallingthemethod:

protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{System.out.println("Chamando Método Get");
    String acao = request.getParameter("acao");
    UsuarioDAO contatoDAO = new UsuarioDAO();


    if (acao!= null && acao.equals("exc")  ) {
        String id = request.getParameter("id");
        User contato = new User();
        contato.setContato_id(Integer.parseInt(id));
        contatoDAO.excluir(contato);
    } 



    List<User> lista = contatoDAO.buscarTodos();

    //Atribuir a Lista no request
    request.setAttribute("lista", lista );

    //Encaminhando para o JSP
    RequestDispatcher saida= request.getRequestDispatcher("exibeContatos.jsp");
    saida.forward(request, response);
} 

Can you help me?

Thank you!

    
asked by anonymous 07.12.2018 / 03:35

3 answers

1

I tested it here and it ran !, it passes its JSP to see how this foreach is.

public List<User> buscarTodos() throws Exception {

    List<User> results = new ArrayList<>();
    String sql = "SELECT * FROM contatos;";

    try (PreparedStatement stmt = con.prepareStatement(sql); 
            ResultSet resultSet = stmt.executeQuery()) {

        while (resultSet.next()) {
            results.add(populate(resultSet));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {

    }
    return results;
}

// Populate with this method

private User populate(ResultSet resultado) throws SQLException {

    User contato = new User();
    contato.setContato_id(resultado.getInt("contato_id"));
    contato.setContato_nome(resultado.getString("contato_nome"));
    contato.setContato_tel(resultado.getString("contato_tel"));
    contato.setContato_email(resultado.getString("contato_email"));
    contato.setContato_sobrenome(resultado.getString("contato_sobrenome"));

    return contato;
}
    
07.12.2018 / 08:10
0

Some tips that can help you:

1) Check your table in the database.

2) Check the returned list exactly by DAO, without going through the screen.

3) Put the method of listing the contacts in the class constructor. Because the GET can be called for each row of the table.

4) Try to pass your SQL as a executeQuery parameter

        ResultSet rs = statement.executeQuery(sql.toString());
    
07.12.2018 / 12:57
0

Follow the JSP:

(It was giving error when I put it full then it follows the image with the markers "<%% >".)

List<User>lista=(List<User>)request.getAttribute("lista");

for (User contato : lista){



<tr>
<td><%= contato.getContato_id() %> </td> 
<td><%= contato.getContato_nome() %></td>
<td><%= contato.getContato_sobrenome()%></td> 
<td><%= contato.getContato_tel() %></td> 
<td><%= contato.getContato_email() %></td>
<td><a href = "contatocontroller.do?acao=exc&id=<%= contato.getContato_id() %>">Excluir

    <a href = "contatocontroller.do?acao=alt&id=<%= contato.getContato_id() %>">Alterar
</td>
</tr>


<%

} 

%>
    
07.12.2018 / 16:25