my question is how can I fill in the fields of my JFrame
with the data stored in my bbdd using hibernate?
That is, I select a row of my JTable
, I get its id and by clicking on a edit button there will be a form that will have all the fields filled with the data that my MySQL bbdd is in. p>
This is the form I want to fill out when I select a row of my JTable but I can not do it.
The code I use in my class OperacionesClientes to obtain the client's data is:
public List<Clientes> obtenerCliente(Short idcliente){
this.iniciarOperacion();
String consulta = "FROM Clientes as C WHERE C.clientesidCliente = :idcliente";
List<Clientes> clienteObtenido = this.session.createQuery(consulta).setParameter("idcliente", idcliente).list();
return clienteObtenido;
}
Within my JFrame I make the following method to bring the data and fill in the fields:
/**
* Llena los campos del form cliente extraidos de la base de datos.
*
* @param idCli Recibe como parametro un tipo Short que es el perteneciente al idcliente.
*/
private void obtener(Short idCli) {
try {
List listaCliente = operacionesClientes.obtenerCliente(idCli);
DefaultComboBoxModel modeloLocalidad = new DefaultComboBoxModel<>();
if (listaCliente.size() > 0) {
Iterator consulta = listaCliente.iterator();
while (consulta.hasNext()) {
jtxtID.setText(String.valueOf(clientes.getClientesidCliente()));
jtxtNombreApellido.setText(clientes.getNombreApellido());
jdcFechaNacimiento.setDate(clientes.getFechaNacimiento());
this.validarSexoDesdeMySQL();
jdcFechaIngreso.setDate(clientes.getFechaIngreso());
jtaObjetivos.setText(clientes.getObjetivos());
jtxtPesoInicial.setText(String.valueOf(clientes.getPesoInicial()));
jtaLecturaCorporal.setText(clientes.getLecturaCorporal());
modeloLocalidad.addElement(new Localidad(localidad.getLocalidadidLocalidad(), localidad.getLocalidad()));
jcboLocalidad.setModel(modeloLocalidad);
}
} else {
JOptionPane.showMessageDialog(null, "No se encuentran registros.", "Registro", JOptionPane.WARNING_MESSAGE);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Se produjo el siguiente error: " + e, "Error", JOptionPane.ERROR_MESSAGE);
}
}
In the same JFrame I have another method that I call from the constructor of the same so that when loading it loads the data passing it by parameter the ModifiedClientID that is sent when selecting the JTable row:
private void cargarDatos() {
try {
obtener(idClienteModificado);
clientes.setNombreApellido(jtxtNombreApellido.getText());
clientes.setFechaNacimiento(jdcFechaNacimiento.getDate());
clientes.setSexo(this.sexo());
clientes.setFechaIngreso(jdcFechaIngreso.getDate());
clientes.setObjetivos(jtaObjetivos.getText());
clientes.setPesoInicial(Float.parseFloat(jtxtPesoInicial.getText()));
clientes.setLecturaCorporal(jtaLecturaCorporal.getText());
clientes.setClientesLocalidadidLocalidad(this.localidad());
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "No se han podido cargar los datos.\nEl tipo de error es:\n" + e, "Error", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
}
I would greatly appreciate your help to identify my error, a few days ago I am with this error, thank you very much.