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. :)
– user28595