I want the combo box to save the code of a field?

1

I'm doing a personal project, I have two tables:

|---------|
|Area     |
|---------|
|Cod_Area   |
|Nomb_Area|
|Ubicacion|
|---------|

|---------|
|Empleados|
|---------|
|DNI      |
|Nombre   |
|Apellido |
|Cod_Area     |
|Celular  |
|Cargo    |
|---------|

The latter is in a JFrame field Area of my tblEmpleados is a Combo Box , when I register an employee, in the Combo Box area I get the name of the area but I want you to save the area code in the BD and not the name, the name of the area only that appears when registering an employee in the JFrame . I get this error

  

Conversion error when converting the nvarchar value 'Accounting' to   data type int

This is the code to bring data from a support table to a Combo Box :

Statement Sent = cn.createStatement();
ResultSet rs = Sent.executeQuery("SELECT*FROM Area");
while(rs.next()){
this.ComboArea.addItem(rs.getString("Nomb_Area"));

This saves me data:

PreparedStatement pst = cn.prepareStatement("insert into Empleados values(?,?,?,?,?,?)");
pst.setString(1, txtDNI.getText().trim());
pst.setString(2, txtNombres.getText().trim());
pst.setString(3, txtApellidos.getText().trim());
pst.setString(4,ComboArea.getSelectedItem().toString());
pst.setString(5, txtCelular.getText().trim());
pst.setString(6, ComboCargo.getSelectedItem().toString());
    
asked by Jcuadros 27.05.2017 в 00:15
source

1 answer

0

remember that the JComboBox allows to save objects, therefore you can have a list of objects of type area and inside the class area you can save both the code and the name, what you want to show the user you put it in the toString method of that class and when you want to insert in the database of the comboBox you get the object ComboArea.getSelectedItem() , you cast it to the class Area and from that object you get the code, something like this ((Area)ComboArea.getSelectedItem()).getCodigo() .

    
answered by 15.06.2017 в 00:09