簡體   English   中英

在Java中使用相同的jButton將數據插入和更新到MySQL

[英]Using the same jButton in Java to Insert and Update data into MySQL

我正在構建我的第一個Java應用程序。 我創建了分別稱為SAVE和UPDATE的按鈕,以將新數據插入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();
            }
        }

根據您的評論,我寫了一個小的代碼段來幫助您。 考慮你有這樣的桌子

-----------------------------------------
| 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
      }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM