Return of incorrect bank values when preeencher a JTable

0

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.

    
asked by anonymous 01.10.2017 / 20:15

1 answer

2

There is no verifiable example in the question but it seems that you are not retrieving values from the columns correctly.

Note this section:

   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();

You populate the Usuario object with strings that, I suppose, are the names of the columns in your bank table. The correct way to do this is through Resultset and your methods getXXXX where this XXXX is the type to be rescued.

An example, if the type of all columns in your table are String:

   rs = stmt.executeQuery();
   while(rs.next()){
       Usuario us = new Usuario();
       us.setId(rs.getInt("id"));
       us.setNome(rs.getString("nome"));
       us.setEmail(rs.getString("email"));
       us.setCpf(rs.getString("cpf"));
       us.setTelefone(rs.getString("telefone"));
       usuarios.add(us);
   }
   rs.close();
   stmt.close();

Except for the id column that was right, other columns will try to retrieve a String type. If the returned type of any of the columns is not String, refer to the class documentation for the getXXXX method appropriate for the type returned by it.

    
01.10.2017 / 20:24