簡體   English   中英

如果id已經存在,則Java更新mysql行

[英]Java updating mysql row if id already exists

我有一個簡單的表格,有5個字段。 (txtID,txtFirstName,txtLastName,txtCheque,txtSavings)。 我想要做的就是將這些字段插入我的數據庫表“accounts”。 在該步驟之前,我想檢查我的txtID字段中的ID是否已存在於我的數據庫中。 如果是,那么我想用字段中的內容更新數據庫行。 如果不是,我想用內容創建一個新行。 到目前為止,檢查我的數據庫中是否存在ID,但如果點擊我的btn,我會收到以下錯誤消息:我不太清楚地知道我做錯了什么。

com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:您的SQL語法中有錯誤; 檢查與您的MariaDB服務器版本對應的手冊,以便在'(LastName,FirstName,Check,Savings)VALUES附近使用正確的語法('Tester','Markus','450.00','50.00'在第1行

private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        try{
            String id = txtID.getText();
            String checkid ="SELECT * FROM accounts where ID=?";
            pst=conn.prepareStatement(checkid);
            pst.setString(1, id);
            rs = pst.executeQuery();

            boolean recordAdded = false;
            while(!rs.next()){            
                 recordAdded = true;
            }
            if(recordAdded){
              // the statement for inserting goes here.
            }else{
                String sql ="UPDATE accounts SET " + "(LastName,FirstName,Cheque,Savings) VALUES" + "(?,?,?,?)";
                pst=conn.prepareStatement(sql);
                pst.setString(1,txtLastName.getText());
                pst.setString(2,txtFirstName.getText());                
                pst.setString(3,txtCheque.getText());
                pst.setString(4,txtSavings.getText());
                pst.executeUpdate();
                getAllAccounts();
                JOptionPane.showMessageDialog(null, "Customer Updated");
            }

        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }
        finally {
            try{
                rs.close();
                pst.close();
                getAllAccounts();
            }
            catch(Exception e) {
            }
        }
    }

你讓我在你的代碼中做一些改變嗎?

private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {                                          

    try {

        String sql = "UPDATE accounts SET LastName = ?, FirstName = ?, Cheque = ?, Savings = ? where id = ?";
        pst=conn.prepareStatement(sql);
        pst.setString(1,txtLastName.getText());
        pst.setString(2,txtFirstName.getText());                
        pst.setString(3,txtCheque.getText());
        pst.setString(4,txtSavings.getText());
        pst.setString(5,txtID.getText());
        int updatedRowCount = pst.executeUpdate();
        // no record with id = txtID
        if(updatedRowCount == 0) {

            pst.close();                

            sql = "insert into accounts (ID,LastName,FirstName,Cheque,Savings) values (?,?,?,?,?,?) ";
            pst = conn.prepareStatement(sql);
            pst.setString(1,txtID.getText());
            pst.setString(2,txtLastName.getText());
            pst.setString(3,txtFirstName.getText());
            pst.setString(4,txtCheque.getText());
            pst.setString(5,txtSavings.getText());
            pst.executeUpdate();

        }

        getAllAccounts();
        JOptionPane.showMessageDialog(null, updatedRowCount > 0 ? "Customer Updated" : "Customer Inserted");

    }
    catch(Exception e){
        getAllAccounts();
        JOptionPane.showMessageDialog(null, e);
    }
    finally {
        try{
            pst.close();
        }
        catch(Exception e) {
        }
    }
}

暫無
暫無

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

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