Return of incorrect bank values when filling a Jtable

Asked

Viewed 35 times

0

I have a Registration Class, which is responsible for the View. 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 User class (class responsible for the sql treatments that each view button has 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 that calls listarTabela();. Every time I click the button, even if I call the class builder, any time I call the "listarTabela()" She returns to me at 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.

  • Please access the link and provide a [mcve] so that it is possible to test the problem.

  • What is missing ?

1 answer

2


There is not a verifiable example in the question but by all indications, you are not rescuing column values correctly.

Note this excerpt:

   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 object Usuario with strings that, I suppose, are the column names of your database table. The correct way to do this is through Resultset and its 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 the column id who was right, too many columns will try to recover a String type. If the type returned from any of the columns is not String, see in the class documentation the getXXXX method suitable for the type returned by it.

  • Thank you very much !! It worked !!!

  • @Pedroanselmo remembering that you can complete the question by accepting the answer by clicking on v next door.

  • Even I’m trying to do this, I just don’t understand how yet, I’m new around here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.