繁体   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