gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:03:31 25 4
gpt4 key购买 nike

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

com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '(LastName, FirstName,Cheque,Savings) VALUES('Tester','Markus','450.00','50.00" at line 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) {
}
}
}

关于如果id已经存在,Java更新mysql行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53137913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com