Load data to a Java form from sql

0

I'm trying to load previously stored data in a bd to a Java form

What I want to achieve is that by selecting a list from the combobox load all the corresponding data (name, age, phone)

My problem: When selecting a rut from the list, the other fields do not change, they remain with the values corresponding to the first rut shown

The data is well saved, since I have a file in which I show, update and delete

I insert my method:

private void cbrutcActionPerformed(java.awt.event.ActionEvent evt) {                                       
    try {
    String rut = cbrutc.getSelectedItem().toString();
    String sql = "SELECT * "
            + "FROM  cliente "
            + "WHERE  rut =  '" + rut + "';";

        Conex cn = new Conex();
        ResultSet rs = cn.consulta();

        while (rs.next()) {
            String nm = rs.getString("nombre");
            String ed = rs.getString("edad");
            String tl = rs.getString("telefono");

            tfnombrec.setText(nm);
            tfedadc.setText(ed);
            tftelefonoc.setText(tl);

            tfmontoc.requestFocus();

        }

    } catch (Exception e) {
        System.out.println(e);
    }
    
asked by iFabio 27.06.2018 в 06:32
source

2 answers

0

You must link an event of the combo that is executed every time you change the value (I do not know which), but you give anticlick to the combo and you go to the option of events there tests which is executed every time change the value in the combo.

    
answered by 27.06.2018 в 07:25
0

If you are using jFrame and that is a jComboBox you can use jComboBox.getSelectedItem () and that returns an Object so you can pass it to String which will be easier to store it.

    
answered by 27.06.2018 в 10:56