2014-10-29 47 views
0

我正在構建我的第一個Java應用程序。我創建了一個名爲SAVE和UPDATE的獨立按鈕,分別將新數據插入MySQL數據庫和更新修改。兩個按鈕都可以正常工作,但我現在正在考慮將兩個功能組合到一個按鈕中。我該如何解決它?在Java中使用相同的jButton插入和更新數據到MySQL

下面是我的代碼爲單獨的按鈕,完美,但獨立工作;

保存功能

public void actionPerformed(ActionEvent arg0) { 

try{ 

String sql ="Insert into attendance (counseleeID,attendanceDate,present) values (?,?,?)"; 

       pst=conn.prepareStatement(sql); 
       pst.setString(1, comboBoxCounseleeID.getSelectedItem().toString()); 
       pst.setString(2, ((JTextField)dateChooser.getDateEditor().getUiComponent()).getText()); 
       pst.setString(3, comboBoxPresent.getSelectedItem().toString()); 
       pst.execute(); 


       JOptionPane.showMessageDialog(null, "Saved"); 

       comboBoxCounseleeID.setSelectedItem(null); 
       ((JTextComponent) dateChooser.getDateEditor().getUiComponent()).setText(null); //review 
       comboBoxPresent.setSelectedItem(null); 


        }   

        catch(Exception e) 
      { 
        JOptionPane.showMessageDialog(null, e); 

      } 

      finally { 
        try{ 
         rs.close(); 
          pst.close(); 
         // conn.close(); 
         } 
         catch(Exception e) { 
             } 
          } 
       Update_table(); 
     } 

更新功能

public void actionPerformed(ActionEvent arg0) { 

int p =JOptionPane.showConfirmDialog(null, "Are you sure you want to save these changes?","Update 
    Warning",JOptionPane.YES_NO_OPTION); 
      if(p==0){ 
      try { 
       String s1= comboBoxCounseleeID.getSelectedItem().toString(); 
       String s2= ((JTextField)dateChooser.getDateEditor().getUiComponent()).getText(); 
       String s3= comboBoxPresent.getSelectedItem().toString(); 

String sql ="UPDATE attendance SET counseleeID='"+s1+"',attendanceDate='"+s2+"',present='"+s3+"' 
WHERE (counseleeID='"+s1+"' AND attendanceDate='"+s2+"') "; 
       pst = conn.prepareStatement(sql); 
       pst.execute(); 


       JOptionPane.showMessageDialog(null, "Updated"); 

       comboBoxCounseleeID.setSelectedItem(null); 
       ((JTextComponent) dateChooser.getDateEditor().getUiComponent()).setText(""); //review 
       comboBoxPresent.setSelectedItem(""); 


      } catch (Exception e2) { 
       JOptionPane.showMessageDialog(null, e2); 
      } 

      finally { 
        try{ 
         rs.close(); 
          pst.close(); 
         // conn.close(); 
         } 
         catch(Exception e2) { 
             } 
          } 
       Update_table(); 
      } 
     } 
+1

當你點擊按鈕嘗試檢查你的數據庫是否該記錄是通過簡單地利用已經存儲'選擇'。如果'ResultSet'包含記錄,則使用'update'查詢,否則'插入'記錄到數據庫中。 – 2014-10-29 12:10:18

+0

我需要如何編寫IF ELSE語句來幫助您確定系統何時插入或更新數據。我無法弄清楚。 – myelow 2014-10-29 13:50:48

回答

0

按照您的意見我已經寫了一小段代碼,這將有助於你。考慮你的表,因爲這

----------------------------------------- 
| id  | name  | salary  | 
+----------+-------------+--------------+ 
| 1  | abc   | 2000   | 
+----------+-------------+--------------+ 
| 2  | kbc   | 3000   | 
+----------+-------------+--------------+ 

表名:EMP 使用下面的代碼:

JButton btn = new JButton("Save"); 
btn.addActionListener((ActionEvent e)->{ 
     //assuming that you have already create the connection to database 
     PreparedStatement ps = con.PreparedStatement("select * from emp where id=?"); 
     ps.setInt(1,yourInput); 
     ResultSet rs = ps.executeQuery(); 
     if(rs.hasNext()) // means record is already available (i.e. Update record) 
     { 
      //update your record here 
     } 
     else   // means no record available (i.e. insert a record) 
     { 
      //insert you record here 
     } 
}); 
+0

感謝您的幫助。我終於能夠做到了。 – myelow 2014-10-29 17:43:29

+0

你可以標記我的答案是正確的 – 2014-10-29 18:09:50

相關問題