java更新mysql行(如果id已经存在)

igetnqfo  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(298)

我有一个有5个字段的简单表单(txtid,txtfirstname,txtlastname,txtcheek,txtssavings)。我只想把这些字段插入我的数据库表accounts中。在此步骤之前,我想检查我的txtid字段中的id是否已经存在于我的数据库中。如果是,那么我想用字段中的内容更新数据库行。如果没有,我想用内容创建一个新行。到目前为止,如果检查我的数据库中存在的id工作,但如果点击我的btn我得到以下错误消息:我不知道我做错了什么。
com.mysql.jdbc.exceptions.mysqlsyntaxerrorexception:您的sql语法有错误;检查与您的mariadb服务器版本相对应的手册,以了解使用第1行的near'(lastname,firstname,check,savings)值('tester'、'markus'、'450.00'、'50.00'的正确语法

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) {
            }
        }
    }
zc0qhyus

zc0qhyus1#

你让我修改你的代码吗?

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

相关问题