I have a Class Registration, which is responsible for View. In it I have this code:
public void listarTabela(){
DefaultTableModel val = (DefaultTableModel) jTable3.getModel();
val.getDataVector().removeAllElements();
UsuarioDAO usDAO = new UsuarioDAO();
List<Usuario> usuarios = usDAO.listarUsuarios();
int i = 0;
while(usuarios.size() > i){
val.addRow(new Object[] {String.valueOf(usuarios.get(i).getId()), usuarios.get(i).getNome(),
usuarios.get(i).getCpf(), usuarios.get(i).getEmail(), usuarios.get(i).getTelefone()});
i++;
In the class UserID (class responsible for the sql treatments that each button of the view will have running), I have the code:
public List<Usuario> listarUsuarios(){
String sql = "SELECT * FROM usuario";
ResultSet rs;
List<Usuario> usuarios = new ArrayList<Usuario>();
try{
PreparedStatement stmt = conecta.prepareStatement(sql);
rs = stmt.executeQuery();
while(rs.next()){
Usuario us = new Usuario();
us.setId(rs.getInt("id"));
us.setNome("nome");
us.setEmail("email");
us.setCpf("cpf");
us.setTelefone("telefone");
usuarios.add(us);
}
rs.close();
stmt.close();
return usuarios;
}catch(SQLException e){
throw new RuntimeException(e);
}
}
I also have a JTable and a JButton, which calls listarTabela();
. Every time I click the button, even though I call the class constructor, anytime I call " listarTabela()
" it returns me in JTable:
1, name, cpf, email, phone
2, name, cpf, email, phone
3, name, cpf, email, phone
The database is ok, it returns the correct values.