“`sql 选择 count(*) 从 people where id =?; “`

huangapple go评论81阅读模式
英文:

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();

huangapple
  • 本文由 发表于 2020年10月26日 03:59:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/64528162.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定