[英]SQL Exception: Unexpected token for UcanaccessConnection.prepareStatement
在這段代碼中,我正在檢查數據庫中具有特定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();
}
編譯后我收到此錯誤
任何想法如何解決這個問題? 有人建議我在列名前后使用方括號,但這不能解決我的問題。 我也看到了使用Statement而不是PreparedStatement的建議,但是我不確定如何實現此方法,因為它不允許checkStatement.setInt(1,id);。
在插入語句的末尾還有一個額外的)
:
String insertSQL = "....... ?, ?, ?, ?, ?, ?, ?, ?) )";
^
| here
應該是:
String insertSQL = "....... ?, ?, ?, ?, ?, ?, ?, ?)";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.