How to call the value of a jComboBox?

0

I do the following query to add elements to the comboBox:

        cmbCargo.setModel(obj.LlenarCombobox("costo", "SELECT * FROM tipoServicio"));

(The first value is the field that is shown in the query)

Using this method

        public DefaultComboBoxModel LlenarCombobox(String DisplayMemberCmb, String SelectCombo)
{
    DefaultComboBoxModel modelo = new DefaultComboBoxModel();
    try
    {
        Statement st = cn.createStatement();
        ResultSet rs = st.executeQuery(SelectCombo);

        while(rs.next())
        {
            modelo.addElement(rs.getString(DisplayMemberCmb));
        }
        st.close();

    }
    catch(Exception ex)
    {
        JOptionPane.showMessageDialog(null, "Conexio_Metodos.LlenarCombobox Error:\n"+ex);
    }
    return modelo;
}

I want to show the field "service type" which is a Varchar that contains a description, but work in the system with the field "cost" with which I do operations to obtain a final cost.

    
asked by Andres Henriquez 31.08.2017 в 02:59
source

1 answer

1

With my little knowledge I give you my opinion according to what I understood of your code you have a "cost" table in which you select all the fields or columns within them if you show the type of service and you want to work with the value of each one you should first get the service type example: TYPE_SERVICE: GAS COST: 00.00 you must select the gas service to do a search with the name of the service and get the value of it in a variable and with it you would work. To show the type of service:

modelo.addElement(rs.getString(tipoServicio));

TO BE MORE SPECIFIC HERE AN EXAMPLE

package nodelado;

import javax.swing. ; import java.awt. ; import java.sql. ; import java.awt.event. ;

public class Nodelado {

public static void main(String[] args) {
    combo c = new combo();
}

}

class combo extends JFrame implements ActionListener {

Statement stm;
ResultSet rst;
JComboBox combo = new JComboBox();
JButton boton = new JButton("PRECIONAME");
JTextField texto = new JTextField("0");

public combo() {
    try {
        Connection conexion = DriverManager.getConnection("jdbc:ucanaccess://d:/modelado.accdb");
        stm = conexion.createStatement();
        rst = stm.executeQuery("SELECT * FROM tipoServicio");

        while (rst.next()) {
            combo.addItem(rst.getString("tipoServicio"));

        }
        System.out.println("CONEXION REALIZADA CON EXITO");
    } catch (SQLException sql) {
        System.out.println("ERROR AL CONECTARSE A LA BASE DE DATOS" + sql);
    }
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(null);
    setVisible(true);
    setSize(300, 300);
    texto.setBounds(150,100,100,20);
    boton.setBounds(25, 150, 150, 20);
    combo.setBounds(25, 100, 100, 20);
    boton.addActionListener(this);
    add(texto);
    add(boton);
    add(combo);

}

@Override
public void actionPerformed(ActionEvent e) {
    Double valor = Double.parseDouble(texto.getText());
    String seleccion = combo.getSelectedItem().toString();
    try {
        rst = stm.executeQuery("SELECT * FROM tipoServicio WHERE (TipoServicio='" + seleccion + "')");
        while (rst.next()) {
            System.out.println(rst.getDouble("costo"));
            System.out.println("EL TOTAL ES " +(valor * rst.getDouble("costo")) );
        }

    } catch (SQLException sql) {
        System.out.println(sql);
    }
}

}

I did it using a database in access but that is the logic that I understood.

In my case I only have two records

I WAIT FOR YOU HELP.

    
answered by 31.08.2017 / 17:14
source