You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

传递数据时SQL语法错误排查:JDBC更新语句参数缺失问题

JDBC更新SQL报错"No value specified for parameter 12"问题解决

你的SQL语句

private static final String UPDATE_QUESTION = "UPDATE Quiz SET type=?, questionIndex=?, choiceNum=?, question=?, choiceA=?, choiceB=?, choiceC=?, choiceD=?, correct=?, hint=? WHERE type=? AND questionIndex=?";

传入的JSON数据

{"id": 84,"type":"epidemics","questionIndex": 1,"choiceNum":2,"question":"updated question3","choiceA": "no3","choiceB":"yes2","choiceC":"no3","choiceD":"yes4","correct":"no3","hint":"second answer"}

错误信息

"timestamp": "2022-11-26T11:52:16.431+00:00",    
"status": 500,    
"error": "Internal Server Error",    
"trace": "org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [UPDATE Quiz SET type=?, questionIndex=?, choiceNum=?, question=?, choiceA=?, choiceB=?, choiceC=?, choiceD=?, correct=?, hint=? WHERE (type=?) AND (questionIndex=?)]; nested exception is java.sql.SQLException: No value specified for parameter 12"

问题原因

你的SQL语句里一共有12个占位符:SET子句里10个(type、questionIndex、choiceNum等),WHERE子句里2个(type、questionIndex)。报错的核心是你在JDBC代码中只给前10个占位符赋了值,漏掉了WHERE子句的第11、12个参数。

另外还有个逻辑隐患:你在SET里更新type和questionIndex,同时又用这两个字段的旧值作为WHERE条件。虽然语法合法,但更新后这条数据的type和questionIndex会被修改,后续如果再用旧值查询就找不到这条数据了,这大概率不是你想要的逻辑。

解决方法

  1. 补全参数赋值:
    检查你的JDBC代码,按顺序给所有12个占位符赋值:

    • 第1-10位:对应SET子句的type(新值)、questionIndex(新值)、choiceNum、question、choiceA、choiceB、choiceC、choiceD、correct、hint
    • 第11-12位:对应WHERE子句的type(旧值)、questionIndex(旧值)——注意这里要用更新前的原始值,否则可能匹配不到数据
  2. 优化SQL语句(推荐):
    如果你不需要更新type和questionIndex,直接把这两个字段从SET子句中移除,减少占位符数量,避免逻辑混淆:

    private static final String UPDATE_QUESTION = "UPDATE Quiz SET choiceNum=?, question=?, choiceA=?, choiceB=?, choiceC=?, choiceD=?, correct=?, hint=? WHERE type=? AND questionIndex=?";
    

    此时总共有10个占位符,赋值时对应8个SET字段+2个WHERE条件即可。


内容的提问来源于stack exchange,提问作者SFleevo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 22:45:38