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?