Returns the database's data in a JTextField

3

I'm having a question about listing the data I've written to a database with JTextField . I created a screen, with the field for the user to enter the cadastre ID in the bank and a FILTER button. I want, when I click on Filter, the data for that ID appears in the% s of% I created.

Here is the code for the filter button and the list code for the bank.

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?

    
asked by anonymous 31.03.2016 / 18:07

2 answers

2

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

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

You are creating a Agenda object with only the id property, and trying to call other properties that have not been set on this object.

As its GerenciaAgenda class returns a Agenda object, the correct thing is to assign this return to the Agenda object:

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

So you guarantee that the a object is actually getting the return of your method, after querying it in your database.

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

    
31.03.2016 / 19:34
1

Replace:

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

By:

Agenda a = ga.selecionar(Integer.parseInt(txtID.getText()));
    
31.03.2016 / 18:30