SQL Statement based on JCombobox selection

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • imabutcher
    New Member
    • Mar 2013
    • 1

    SQL Statement based on JCombobox selection

    Here is the actionMouseClic ked. It should be able to update the selected row with new data from a form.
    The updating works for the most part I.e It updates all data in one column to the same data (which is fine as long as I can get the SQL to work)

    I need it so the SQL statement recognizes the ComboBox selection and updates the database accordingly.


    Code:
       
    private void addBtnMouseClicked(java.awt.event.MouseEvent evt) {                                    
                       
            String CBox2 =(String)comboBoxCS.getSelectedItem();        
            
            String sql = "update carstock set CarStock = ? where DriverID = ('CBox2')";
            
            try{
                pst = conn.prepareStatement(sql);
                pst.setString(1, addRemove.getText());
                
                
                pst.executeUpdate();
                JOptionPane.showMessageDialog(null, "Updated");        
                UpdateJTable();
            }
            catch (Exception e){
                JOptionPane.showMessageDialog(null, e);
            }
        
        }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Add an ActionListener to your combobox then in the actionPerformed method do
    Code:
    if(e.getSource() instanceof JComboBox) {
     JComboBox cb = (JComboBox)e.getSource();
     String selectedValue= (String)cb.getSelectedItem();
    //do something with the selected value
    }

    Comment

    Working...