By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,806 Members | 1,380 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,806 IT Pros & Developers. It's quick & easy.

SQL Statement based on JCombobox selection

P: 1
Here is the actionMouseClicked. 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.


Expand|Select|Wrap|Line Numbers
  1.  
  2. private void addBtnMouseClicked(java.awt.event.MouseEvent evt) {                                    
  3.  
  4.         String CBox2 =(String)comboBoxCS.getSelectedItem();        
  5.  
  6.         String sql = "update carstock set CarStock = ? where DriverID = ('CBox2')";
  7.  
  8.         try{
  9.             pst = conn.prepareStatement(sql);
  10.             pst.setString(1, addRemove.getText());
  11.  
  12.  
  13.             pst.executeUpdate();
  14.             JOptionPane.showMessageDialog(null, "Updated");        
  15.             UpdateJTable();
  16.         }
  17.         catch (Exception e){
  18.             JOptionPane.showMessageDialog(null, e);
  19.         }
  20.  
  21.     }      
  22.  
Mar 21 '13 #1
Share this Question
Share on Google+
1 Reply


10K+
P: 13,264
Add an ActionListener to your combobox then in the actionPerformed method do
Expand|Select|Wrap|Line Numbers
  1. if(e.getSource() instanceof JComboBox) {
  2.  JComboBox cb = (JComboBox)e.getSource();
  3.  String selectedValue= (String)cb.getSelectedItem();
  4. //do something with the selected value
  5. }
Mar 22 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.