How to show the string of the selected item in a JComboBox stored from a MySQL table

-1

I have the following problem, when selecting a row of my JTable to modify its data, I call a form whose data of the table is filled in the fields of the same, inside the form there is a JCombobox that is filled with the stored data of the table of my BD, my question is, How can I show the string of said id stored in the table of my database in the combo? Because I get the id number, that is, it shows me the id number inside the combo and what I want to show me the string associated with that id.

The combo is loaded in this way.

private DefaultComboBoxModel comboLocalidad() {
        DefaultComboBoxModel valor = new DefaultComboBoxModel();
        try {
            Connection miComando = AdministradorConfiguracion.obtenerComandoMySql();
            CallableStatement obtenerLocalidades = miComando.prepareCall("call obtener_localidades()");
            ResultSet rs = obtenerLocalidades.executeQuery();
            while (rs.next()) {
                cboLocalidad.setModel(valor);
                valor.addElement(new Localidad(rs.getInt("Nro"), rs.getString("Localidad")));
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error al intentar auto completar busqueda:\n"
                    + e, "Error en la operación", JOptionPane.ERROR_MESSAGE);
        }
        return valor;
    }

The locality class.

public class Localidad{
    public int idlocalidad;
    public String localidad;

    public Localidad(int idlocalidad, String localidad) {
        this.idlocalidad = idlocalidad;
        this.localidad = localidad;
    }
    ////////////////////////////////////////////////////
    public int getIdlocalidad() {
        return idlocalidad;
    }

    public void setIdlocalidad(int idlocalidad) {
        this.idlocalidad = idlocalidad;
    }

    public String getLocalidad() {
        return localidad;
    }

    public void setLocalidad(String localidad) {
        this.localidad = localidad;
    }

    @Override
    public String toString(){
        return localidad;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof Localidad)) {
            return false;
        }
        Localidad localidad = (Localidad) obj;
        if (idlocalidad != localidad.idlocalidad) {
            return false;
        }
        if (this.localidad != null ? !this.localidad.equals(localidad.localidad) : localidad.localidad != null) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 89 * hash + (this.localidad != null ? this.localidad.hashCode() : 0);
        hash = 89 * hash + this.idlocalidad;
        return hash;
    }
}

I fill in the form data.

private void cargarDatos() {
    obtener(idModificado);
    cliente.setNombre(txtNombre.getText());
    cliente.setApellido(txtApellido.getText());
    cliente.setDomicilio(txtDomicilio.getText());
    cliente.setTelefono(txtTelefono.getText());
    cliente.setFacebook(txtFacebook.getText());
    cliente.setIdlocalidad(Integer.parseInt(cboLocalidad.getSelectedItem().toString()));
}

The method get ()

public void obtener(int idCli) {
        try {
            DefaultComboBoxModel valor = new DefaultComboBoxModel();
            Connection miComando = AdministradorConfiguracion.obtenerComandoMySql();
            CallableStatement obtenerCliente = miComando.prepareCall("call obtener_cliente(?)");
            obtenerCliente.setInt(1, idCli);
            obtenerCliente.execute();
            ResultSet rs = obtenerCliente.executeQuery();
            while (rs.next()) {
                jtxtId.setText(String.valueOf(rs.getInt("idcliente")));
                txtNombre.setText(rs.getString("nombre"));
                txtApellido.setText(rs.getString("apellido"));
                txtDomicilio.setText(rs.getString("domicilio"));
                txtTelefono.setText(rs.getString("telefono"));
                txtFacebook.setText(rs.getString("facebook"));

                cboLocalidad.setSelectedItem(rs.getInt("idlocalidad"));
            }
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "Error al intentar obtener el cliente:\n"
                    + e, "Error en la operación", JOptionPane.ERROR_MESSAGE);
        }
    }

Which is responsible for executing the MySQL stored procedure, which is:

CREATE DEFINER='root'@'localhost' PROCEDURE 'obtener_cliente'(IN id int)
SELECT idcliente, 
            nombre, 
            apellido, 
            domicilio, 
            telefono, 
            facebook, 
            idlocalidad
    FROM   Cliente
    WHERE  idcliente= id

When loading the form with all the corresponding fields, the JComboBox is displayed as such.

I hope you made me understand. I would greatly appreciate your help. Thanks

    
asked by Gerardo Ferreyra 07.05.2017 в 20:22
source

1 answer

0

The solution was to instantiate class Localidad within the method get () working with the same class by passing the object and the string of the same constructor. I had to create a model with class DefaultComboBoxModel and add the elements. Which would be as follows.

public void obtener(int idCli) {
    try {
        DefaultComboBoxModel valor = new DefaultComboBoxModel();
        Connection miComando = AdministradorConfiguracion.obtenerComandoMySql();
        CallableStatement obtenerCliente = miComando.prepareCall("call obtener_cliente(?)");
        obtenerCliente.setInt(1, idCli);
        obtenerCliente.execute();
        ResultSet rs = obtenerCliente.executeQuery();
        while (rs.next()) {
            jtxtId.setText(String.valueOf(rs.getInt("idcliente")));
            txtNombre.setText(rs.getString("nombre"));
            txtApellido.setText(rs.getString("apellido"));
            txtDomicilio.setText(rs.getString("domicilio"));
            txtTelefono.setText(rs.getString("telefono"));
            txtFacebook.setText(rs.getString("facebook"));
            //Instanciamos la clase Localidad y le asignamos con rs el campo idlocalidad y localidad.
            valor.addElement(new Localidad(rs.getInt("idlocalidad"), rs.getString("localidad")));
            cboLocalidad.setModel(valor);
        }
    } catch (SQLException e) {
        JOptionPane.showMessageDialog(null, "Error al intentar obtener el cliente:\n"
                + e, "Error en la operación", JOptionPane.ERROR_MESSAGE);
    }
}

In my DB, modify the stored procedure which adds the "locality" field so that the locality can be passed to the constructor of class Localidad , which is as follows.

SELECT idcliente, 
            nombre, 
            apellido, 
            domicilio, 
            telefono, 
            facebook, 
            C.idlocalidad,
            L.localidad
    FROM   Cliente C LEFT OUTER JOIN localidades L
    ON      C.idlocalidad = L.idlocalidad
    WHERE  idcliente= id

Also make a union with the table locations of my BD.

In my method, loadData () is as follows.

private void cargarDatos() {
        obtener(idModificado);
        cliente.setNombre(txtNombre.getText());
        cliente.setApellido(txtApellido.getText());
        cliente.setDomicilio(txtDomicilio.getText());
        cliente.setTelefono(txtTelefono.getText());
        cliente.setFacebook(txtFacebook.getText());
        Localidad cbo = (Localidad) cboLocalidad.getSelectedItem();
        int id = cbo.getIdlocalidad();
        cliente.setIdlocalidad(id);
    }

The result is:

    
answered by 07.05.2017 в 22:54