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

时间:2018-11-04 05:03:26

标签: java mysql

我有一个包含5个字段的简单表格。 (txtID,txtFirstName,txtLastName,txtCheque,txtSavings)。我要做的就是将这些字段插入数据库表“ accounts”。在执行此步骤之前,我想检查我的txtID字段中的ID是否已存在于数据库中。如果是,那么我想使用字段中的内容更新数据库行。如果不是,我想用内容创建一个新行。到目前为止,检查ID是否存在于我的数据库中是否有效,但是如果单击btn,我将收到以下错误消息:我不知道自己在做什么错。

  

com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:您有一个错误   用你的SQL语法;检查与您的MariaDB相对应的手册   服务器版本,以在'(LastName,   FirstName,Cheque,Savings)VALUES('Tester','Markus','450.00','50.00“ at   第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) {
            }
        }
    }

1 个答案:

答案 0 :(得分:3)

您允许我对您的代码进行一些更改吗?

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