gpt4 book ai didi

java - SQL异常: Unexpected token for UcanaccessConnection.准备语句

转载 作者:行者123 更新时间:2023-12-02 02:20:34 25 4
gpt4 key购买 nike

在此代码中,我正在检查数据库中是否有具有特定 ID 的记录。如果未找到 ID,数据库将更新为新记录:

public void save() throws SQLException {
String checkSQL = "Select count(*) as count from people where id=?";
PreparedStatement checkStatement = con.prepareStatement(checkSQL);

String insertSQL = "insert into people (ID, NAME, NI_NUMBER, NI_CAT, MOBILE_NUM, EMAIL, ADDRESS, AREA, POSTCODE) values(?, ?, ?, ?, ?, ?, ?, ?, ?) )";
PreparedStatement insertStatement = con.prepareStatement(insertSQL);

for (Employee person : people) {
int id = person.getId();
String name = person.getName();
String NINumber = person.getnINumber();
NICatagory cat = person.getnICatagory();
String mobileNumber = person.getMobileNum();
String email = person.getEmail();
String address = person.getAddress();
String area = person.getArea();
String postcode = person.getPostCode();

checkStatement.setInt(1, id);

ResultSet checkResult = checkStatement.executeQuery();
checkResult.next();

int count = checkResult.getInt(1);

if (count == 0) {
System.out.println("Inserting person with id " + id);

int col = 1;
insertStatement.setInt(col++, id);
insertStatement.setString(col++, name);
insertStatement.setString(col++, NINumber);
insertStatement.setString(col++, cat.name());
insertStatement.setString(col++, mobileNumber);
insertStatement.setString(col++, email);
insertStatement.setString(col++, address);
insertStatement.setString(col++, area);
insertStatement.setString(col++, postcode);

insertStatement.executeUpdate();

} else {
System.out.println("Updating person with id " + id);
}

}
insertStatement.close();
checkStatement.close();
}

编译后我收到 THIS错误

有什么想法可以解决这个问题吗?有人建议我在列名称周围使用括号,但这并没有解决我的问题。我还看到了使用 Statement 而不是 PreparedStatement 的建议,但是我不确定如何实现它,因为它不允许 checkStatement.setInt(1, id) ;

最佳答案

插入语句末尾有一个附加的 ):

String insertSQL = "....... ?, ?, ?, ?, ?, ?, ?, ?) )"; 
^
| here

应该是:

String insertSQL = "....... ?, ?, ?, ?, ?, ?, ?, ?)"; 

关于java - SQL异常: Unexpected token for UcanaccessConnection.准备语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48581913/

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