0
Hello guys I am studying Javaweb and I came across a boring mistake!! I’ve done a lot of research on the subject but I couldn’t solve this mistake.
I have this method that is pulling all data from the table "contacts" from the database and returning 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 will display this list it returns only the last repeated contact several times, as in the image below:
Follow the code by calling the method:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, 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!
Post your HTML, please
– Alysson Chicó