[英]Java updating mysql row if id already exists
I have a simple form with 5 fields. 我有一个简单的表格,有5个字段。 (txtID, txtFirstName, txtLastName, txtCheque, txtSavings).
(txtID,txtFirstName,txtLastName,txtCheque,txtSavings)。 All I want to do is inserting these fields into my database table "accounts".
我想要做的就是将这些字段插入我的数据库表“accounts”。 Before that step I want to check if the ID from my txtID field already exists in my database.
在该步骤之前,我想检查我的txtID字段中的ID是否已存在于我的数据库中。 If yes then I want to update the database row with the content from the fields.
如果是,那么我想用字段中的内容更新数据库行。 And if not I want to create a new row with the content.
如果不是,我想用内容创建一个新行。 So far the check if the ID exists in my DB works but if click on my btn I get the following error message: I dont relly know what I'm doing wrong.
到目前为止,检查我的数据库中是否存在ID,但如果点击我的btn,我会收到以下错误消息:我不太清楚地知道我做错了什么。
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax;
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:您的SQL语法中有错误; 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
检查与您的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) {
}
}
}
do you let me to make some changes in your code? 你让我在你的代码中做一些改变吗?
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.