Returns database data in a Jtextfield

Asked

Viewed 707 times

3

I have a question regarding listing the data I recorded in a database for a JTextField. I created a screen, with the field for the user to enter the registration ID in the database and a FILTER button. When you click filter, I want the data from that ID to appear in JTextField that I created.

Follow the Filter button code and the database listing code.

Filter:

JButton btnFiltrar = new JButton("Filtrar");
    btnFiltrar.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            GerenciaAgenda ga = new GerenciaAgenda();
            Agenda a = new Agenda();
            a.setId(Integer.parseInt(txtID.getText()));
            ga.selecionar(a.getId());
            txtNome.setText(a.getNome());
            txtEmail.setText(a.getEmail());
            txtCpf.setText(a.getCpf());
        }
    });

Method to make the selection:

public Agenda selecionar(int id){
        Connection c = new Conexao().criarConexao();
        String sql = "SELECT * FROM agenda WHERE id=?";
        try {
            PreparedStatement p = c.prepareStatement(sql);
            p.setInt(1, id);

            ResultSet resultado = p.executeQuery();

            if (resultado.next()){
                Agenda a = new Agenda();
                a.setId(id);
                a.setNome( resultado.getString("nome"));
                a.setEmail(  resultado.getString("email"));
                a.setCpf( resultado.getString("cpf"));
                return a;
            }

        } catch (SQLException ex) {
            Logger.getLogger(GerenciaAgenda.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            new Conexao().fecharConexao(c);    
        }
        return null;
    }

What am I missing ?

  • Hello, if any of the answers answered it, it would be interesting to mark it as accepted, thus will serve as reference for other users. :)

2 answers

2


As shown in other answers, the problem is in this excerpt:

GerenciaAgenda ga = new GerenciaAgenda();
Agenda a = new Agenda();
a.setId(Integer.parseInt(txtID.getText()));
ga.selecionar(a.getId());

You are creating an object Agenda only with the property of id, and trying to call other properties that were not set in this object.

As your class GerenciaAgenda returns an object Agenda, the correct is to assign this return to the object Agenda:

GerenciaAgenda ga = new GerenciaAgenda();
Agenda a = ga.selecionar(Integer.parseInt(txtID.getText()));

Thus, you ensure that the object a is actually getting the return of your method after query in your database.

Beware to check if this return is null before accessing its properties, avoiding NullPointerException, and also check if the value passed by txtID is not empty or a value that cannot be converted to int, avoiding exceptions of the type NumberFormatException.

  • Thank you, Diego.

1

Substitute:

 Agenda a = new Agenda();
 a.setId(Integer.parseInt(txtID.getText()));
 ga.selecionar(a.getId());

For:

Agenda a = ga.selecionar(Integer.parseInt(txtID.getText()));

Browser other questions tagged

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