gpt4 book ai didi

java - MySQL 查询通过 Java 语法错误

转载 作者:太空宇宙 更新时间:2023-11-03 12:05:28 26 4
gpt4 key购买 nike

我有这段代码将记录插入到帐户表中:

String accNumber = jTextField5.getText();
String accName = jTextField4.getText();
String accAddress1 = jTextField3.getText();
String accAddress2 = jTextField2.getText();
String accCity = jTextField6.getText();
String accCounty = jTextField7.getText();
String accPostCode = jTextField9.getText();
String accContact = jTextField8.getText();

String query = "Insert into accounts (AccNo, name, address, address2, address3, City, County, PostCode, contact) values (?, ?, ?, ?, ?, ?, ?, ?, ?)";

try{

connect.pst = connect.con.prepareStatement(query);
connect.pst.setString(1, accNumber);
connect.pst.setString(2, accName);
connect.pst.setString(3, accAddress1);
connect.pst.setString(4, accAddress2);
connect.pst.setString(5, null);
connect.pst.setString(6, accCity);
connect.pst.setString(7,accCounty);
connect.pst.setString(8, accPostCode);
connect.pst.setString(9, accContact);
connect.pst.execute();
JOptionPane.showMessageDialog(null, "Saved");

}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}

这工作得很好,并将记录插入到帐户表中。接下来我有这段代码,用于更新包含 6 个字段的 orderstable 表;订单号、账户号、发票号、描述、金额、增值税。订单号字段已设置为自动递增代码:

String accNumber = jTextField29.getText();
String invNo = jTextField20.getText();
String description = jTextField21.getText();
String vat = jTextField22.getText();
String amount = jTextField23.getText();

String query = "Insert into orderstable (Order Number, AccNo, Invoice Number, Description, Amount, VAT) values (?, ?, ?, ?, ?, ?)";
try{

connect.pst = connect.con.prepareStatement(query);
connect.pst.setString(1, "3");
connect.pst.setString(2, accNumber);
connect.pst.setString(3, invNo);
connect.pst.setString(4, description);
connect.pst.setString(5, amount);
connect.pst.setString(6, vat);
connect.pst.execute();
JOptionPane.showMessageDialog(null, "Saved");

}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}

上述代码返回 SQL 语法错误。我不确定为什么。此 SQL 语法错误的原因可能是什么?

最佳答案

列名不能有空格,如果这样做,则需要在反引号中加上 then

Insert into orderstable 
(`Order Number`, AccNo, `Invoice Number`, Description, Amount, VAT)

点击这里了解更多详情http://dev.mysql.com/doc/refman/5.0/en/identifiers.html

关于java - MySQL 查询通过 Java 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26842959/

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