英文:
Select count(*) from people where id =?;
问题
使用MacOS - Java - MySql 8.0 - jdbc 8.0 - Intellij。
当我运行这个语句(Select count(*) from people where id =?;
)将其中的?替换为数字1时,它可以正常运行。但是当我执行以下操作:
for (Person person: people) {
int id = person.getId();
checkStmt.setInt(1, id);
ResultSet checkResult = checkStmt.executeQuery(checkSql);
我会得到以下错误:
You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version
for the right syntax to use near '?' at line 1
通过运行调试器,从数组返回的id值是1。
英文:
Using MacOS - Java - MySql 8.0 - jdbc 8.0 - Intellij.
When I run the this (Select count(*) from people where id =?;
) replacing the ? With the number 1 it works fine. But when I do this:
for (Person person: people) {
int id = person.getId();
checkStmt.setInt(1, id);
ResultSet checkResult = checkStmt.executeQuery(checkSql);
I get this:
You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version
for the right syntax to use near '?' at line 1
Running the debugger the value of id returned from the array is 1.
答案1
得分: 2
你已经调用了setInt
,然后通过使用不同的查询将其丢弃。将代码从:
checkStmt.setInt(1, id);
ResultSet checkResult = checkStmt.executeQuery(checkSql);
修改为:
checkStmt.setInt(1, id);
ResultSet checkResult = checkStmt.executeQuery();
英文:
You have already called setInt
, you then throw it away by using a different query. Change
checkStmt.setInt(1, id);
ResultSet checkResult = checkStmt.executeQuery(checkSql);
to
checkStmt.setInt(1, id);
ResultSet checkResult = checkStmt.executeQuery();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论