Hi
i have 2 tables
customer (cabinet_num which is integer)
and cabinet (cabinet_num Autoincrement and cabinet_title)
in my java i can display cabinet title to let the user choose a cabinet..but i want to collect the cainet_number in my customer table
customet.cabinet

package cabinet;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.Color;
import javax.swing.UIManager;
import javax.swing.JLabel;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.ScrollPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.ComponentOrientation;
import javax.swing.ScrollPaneConstants;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JOptionPane;
import javax.swing.JScrollBar;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import java.awt.Button;
import java.awt.event.ActionEvent;

public class cabinet extends JFrame {


    private JPanel contentPane;
    private JTextField txtCustomerInformation;
    private JTextField custnum;
    private JTextField name;
    private JTextField shop;
    private JScrollPane scrollPane;
    private JScrollPane scrollPane_1;
    private JTextArea address;
    private JTextField person1;
    private JLabel phone;
    private JTextField person3;
    private JLabel lblCabinetNo;
    private JLabel pers1;
    private JLabel pers2;
    private JTextField person2;
    private JComboBox jc;
    private JButton ajouter;
    private JButton update;
    private JButton effacer;
    private JButton quitter;

    /**
     * Launch the application.
     * @throws ClassNotFoundException 
     * @throws SQLException 
     */



    public cabinet() throws ClassNotFoundException, SQLException  {
        display(); 
        connecte();


    }
    **public void connecte() throws ClassNotFoundException, SQLException{
        Connection conn = null;
        //int jcn;
        try {
                // The driver allows you to query the database with Java
                // forName dynamically loads the class for you

                Class.forName("com.mysql.jdbc.Driver");

                // DriverManager is used to handle a set of JDBC drivers
                // getConnection establishes a connection to the database
                // You must also pass the userid and password for the database

                conn = DriverManager.getConnection("jdbc:mysql://localhost/cabinet","root","root");

                // Statement objects executes a SQL query
                // createStatement returns a Statement object

                Statement sqlState = conn.createStatement();

                // This is the query I'm sending to the database

                String selectStuff = "Select cabinet_num,cabinet_type from cabinet";

                // A ResultSet contains a table of data representing the
                // results of the query. It can not be changed and can 
                // only be read in one direction

                ResultSet rows = sqlState.executeQuery(selectStuff);

                // next is used to iterate through the results of a query
                while(rows.next()){
                    String res = rows.getString("cabinet_type");

                    if (res != null) {
                        res = res.trim();
                    }
                    jc.addItem(res);

                }
        }   
catch (SQLException ex) {

            // String describing the error

            System.out.println("SQLException: " + ex.getMessage());

            // Vendor specific error code

            System.out.println("VendorError: " + ex.getErrorCode());
        } 

        catch (ClassNotFoundException e) {
            // Executes if the driver can't be found
            e.printStackTrace();
        } 
    }       
         **

    public void display(){
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 809, 447);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);



        txtCustomerInformation = new JTextField();
        txtCustomerInformation.setEditable(false);
        txtCustomerInformation.setBounds(5, 5, 798, 40);
        txtCustomerInformation.setBackground(UIManager.getColor("Button.shadow"));
        txtCustomerInformation.setForeground(UIManager.getColor("OptionPane.errorDialog.titlePane.background"));
        txtCustomerInformation.setFont(new Font("Times New Roman", Font.BOLD, 30));
        txtCustomerInformation.setHorizontalAlignment(SwingConstants.CENTER);
        txtCustomerInformation.setText("CUSTOMER INFORMATION");
        contentPane.add(txtCustomerInformation);
        txtCustomerInformation.setColumns(10);

        JLabel cust = new JLabel("Customer No");
        cust.setFont(new Font("Arial", Font.BOLD, 12));
        cust.setBounds(5, 57, 104, 15);
        contentPane.add(cust);

        custnum = new JTextField();
        custnum.setBounds(127, 57, 114, 19);
        contentPane.add(custnum);
        custnum.setColumns(10);

        JLabel nom = new JLabel("Trading Name");
        nom.setFont(new Font("Arial", Font.BOLD, 12));
        nom.setBounds(5, 90, 104, 15);
        contentPane.add(nom);

        name = new JTextField();
        name.setBounds(127, 88, 272, 19);
        contentPane.add(name);
        name.setColumns(10);

        JLabel shope = new JLabel("Over Shop");
        shope.setFont(new Font("Arial", Font.BOLD, 12));
        shope.setBounds(5, 117, 104, 15);
        contentPane.add(shope);

        shop = new JTextField();
        shop.setBounds(127, 119, 114, 19);
        contentPane.add(shop);
        shop.setColumns(10);

        JLabel add = new JLabel("Full Trading Address");
        add.setFont(new Font("Arial", Font.BOLD, 12));
        add.setBounds(5, 144, 125, 15);
        contentPane.add(add);

        scrollPane_1 = new JScrollPane();
        scrollPane_1.setBounds(130, 150, 183, 81);
        contentPane.add(scrollPane_1);

        address = new JTextArea();
        scrollPane_1.setViewportView(address);
        address.setLineWrap(true);

        person1 = new JTextField();
        person1.setBounds(127, 243, 196, 19);
        contentPane.add(person1);
        person1.setColumns(10);

        phone = new JLabel("Phone No");
        phone.setFont(new Font("Arial", Font.BOLD, 12));
        phone.setBounds(5, 307, 70, 15);
        contentPane.add(phone);

        person3 = new JTextField();
        person3.setColumns(10);
        person3.setBounds(127, 304, 196, 19);
        contentPane.add(person3);

        lblCabinetNo = new JLabel("Cabinet Requested");
        lblCabinetNo.setFont(new Font("Arial", Font.BOLD, 12));
        lblCabinetNo.setBounds(332, 57, 114, 19);
        contentPane.add(lblCabinetNo);

        pers1 = new JLabel("Contact Person 1");
        pers1.setFont(new Font("Arial", Font.BOLD, 12));
        pers1.setBounds(5, 247, 104, 15);
        contentPane.add(pers1);

        pers2 = new JLabel("Contact Person 2");
        pers2.setFont(new Font("Arial", Font.BOLD, 12));
        pers2.setBounds(5, 277, 104, 15);
        contentPane.add(pers2);

        person2 = new JTextField();
        person2.setColumns(10);
        person2.setBounds(127, 274, 196, 19);
        contentPane.add(person2);
          jc = new JComboBox<String>();
          jc.setBounds(479, 57, 295, 24);
          contentPane.add(jc);

          ajouter = new JButton("Add");
          ajouter.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                Connection conn = null;
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                } catch (ClassNotFoundException e2) {
                    // TODO Auto-generated catch block
                    e2.printStackTrace();
                }

                // DriverManager is used to handle a set of JDBC drivers
                // getConnection establishes a connection to the database
                // You must also pass the userid and password for the database

                try {
                    conn = DriverManager.getConnection("jdbc:mysql://localhost/cabinet","root","root");
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                String cust,nom,boutique,addr,pers1,pers2,phone = null;
                int num = 0;
                cust=custnum.getText();
                nom=name.getText();
                boutique=shop.getText();
                addr=address.getText();
                pers1=person1.getText();
                pers2=person2.getText();
                phone=person3.getText(); 
                //num=(Integer) jc.getSelectedItem();

                try
                {                
                    //Connection con = connecte();          
                    String strQuery = "INSERT INTO customer(custnum,tradingname,overshop,address,contact1,contact2," +
                            "phone) VALUES (?,?,?,?,?,?,?)";
                    java.sql.PreparedStatement statement = conn.prepareStatement(strQuery);
                    statement.setString(1,cust);
                    statement.setString(2,nom);
                    statement.setString(3,boutique);
                    statement.setString(4,addr);
                    statement.setString(5,pers1); 
                    statement.setString(6,pers2);
                    statement.setString(7,phone);
                    //statement.setLong(8,num);

                    statement.executeUpdate();
                 JOptionPane.showMessageDialog(null,"Record Added to customer table");
                }
                catch(Exception e)
                {
                    JOptionPane.showMessageDialog(null,"Exception: "+ e.toString());
                }


            }
          });
          ajouter.setBounds(26, 348, 104, 40);
          contentPane.add(ajouter);

          update = new JButton("Update");
          update.setBounds(153, 348, 104, 40);
          contentPane.add(update);

          effacer = new JButton("Delete");
          effacer.setBounds(274, 348, 104, 40);
          contentPane.add(effacer);

          quitter = new JButton("Exit");
          quitter.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                System.exit(0);
            }
          });
          quitter.setBounds(395, 348, 104, 40);
          contentPane.add(quitter);

        JTextArea ta = new JTextArea();
        JScrollPane sp = new JScrollPane(ta);
        JFrame f = new JFrame();
        f.getContentPane().add(sp);
    }
    public static void main(String[] args) throws ClassNotFoundException, SQLException {


        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    cabinet frame = new cabinet();

                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

_num=cabinet.cabinet_num

Can I confirm what you are asking?
You have some Cabinets, each with a number and a title. You want to display the title in the GUI, but get the number when the user has made his selection. Is that right?
Are all the titles unique?

Edited 2 Years Ago by JamesCherrill

Hi
the table has number and title
1-wood
2-plastic
3-iron and so
i want to collect the number (1 ou 2 ou any other number)..to put it on custumer table who has a field called cabinet_number and it is int
thank you very much

another of reasons to post an SSCCE, short, runnable, compilable with hardcoded value in local variable for JTable/XxxTableModel instead of shadowing JDBC

i want to show
**` String selectStuff = "Select cabinet_num,cabinet_type from cabinet";

// A ResultSet contains a table of data representing the
// results of the query. It can not be changed and can
// only be read in one direction
ResultSet rows = sqlState.executeQuery(selectStuff);
// next is used to iterate through the results of a query
while(rows.next()){
String res = rows.getString("cabinet_type");
int num=rows.getString("cabinet_num");///the error is here
if (res != null) {
res = res.trim();
}
jc.addItem(res);`**

i want for my selectedvalue() to be a number not the title ...
int x
x=getselectedvalue(number);is this what i need to do

again short, runnable, compilable with hardcoded value in local variable for JTable/XxxTableModel instead of shadowing JDBC

This article has been dead for over six months. Start a new discussion instead.