only the first item in the combobox will be returened even if i selected another item.
HELP!

try{
            //combobox of the products
            rs = stmt.executeQuery(query2);
            rs.last();
            int i = rs.getRow();
            String [] v = new String [i];
            rs.beforeFirst();
            i = 0;
            while(rs.next())
            {
                v[i] = rs.getString(2);
                i++;
                //break;
            }

             prodna = new JComboBox(v);
        }
        catch(Exception e)
        {

        }
        
        getContentPane().add(prodna);
        srch = prodna.getSelectedItem().toString();
        //ordNo = new JLabel(srch);
        ordNo.setText(srch);

        label_5 = new JLabel("Product Name:");      //***Label
        getContentPane().add(label_5);

        label_6 = new JLabel("Quantity");            //***Label
        getContentPane().add(label_6);

        gqty = new JTextField("");
        getContentPane().add(gqty);

        ok = new JButton("OK");
        getContentPane().add(ok);

        cancel = new JButton("Cancel");
        getContentPane().add(cancel);

        setSize(getPreferredSize());

        ok.addActionListener(new ActionListener(  ) {
            public void actionPerformed(ActionEvent ae) {


                        try {
                            rs = stmt.executeQuery(query2);
                            rs.beforeFirst();
                                    while(rs.next()){
                                        String pna = rs.getString("prodNa");
                                        String pnum = rs.getString("prodNo");
                                        String qty1 = rs.getString("qty");
                                        int qty11 = Integer.parseInt(qty1);;
                                        String price = rs.getString("unitPrice");
                                         y = Integer.parseInt(gqty.getText());
                                         tprice = Float.parseFloat(price);
                                         if (pna.equalsIgnoreCase(srch.trim()))
                                        {
                                            System.out.print(pna);
                                            System.out.println(tprice);
                                            System.out.println(y);
                                            System.out.println(x);
                                            System.out.println(pnum);
                                            tp = (int) (y * tprice);
 
                                            stmt.executeUpdate("INSERT INTO `orderdetails` (`orderNo`,`prodNo`,`qty`, `payable`)  VALUES('"+x+"','"+pnum+"','"+y+"','"+tp+"');");
                                            pqty = qty11 - y;
                                            stmt.executeUpdate("UPDATE `products` SET `qty` = 'pqty' WHERE `products`.`prodNo` =1005 ;");
                                            break;

                                         }

                                    }
                        } catch (SQLException ex) {
                            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                        }

                            setVisible(false);
                            newOrder n = new newOrder();
                            n.setVisible(true);
                            n.setLocation(500, 200);
            }

                });

1) don't create JComboBox inside Databases Connection, create this JComponent before connection,

2) put this data to the Vector, inside ResultSet loops only add myVector.add(rs.getString(2)),

3) test if isn't rs.getString(2) null or empty, because emty JComboBoxes Item doesn't looks like nice....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.