java.sql.SQLException Parameter index out of range (1 gt; number of parameters, which is 0)(java.sql.SQLException 参数索引超出范围(1 参数个数,即 0))
问题描述
在验证了我选择的选择组合框后,我无法将其插入我的数据库中.Tomcat 报错:
After validation of select combo box which I have selected and I am not able to insert it in my database. Tomcat gives following error:
java.sql.SQLException:参数索引超出范围(1>参数个数,为0).
java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
这是怎么引起的,我该如何解决?
How is this caused and how can I solve it?
推荐答案
当你在 PreparedStatement
上调用任何 setXxx()
方法时,你会得到这个错误,而SQL 查询字符串没有任何占位符 ?
.
You will get this error when you call any of the setXxx()
methods on PreparedStatement
, while the SQL query string does not have any placeholders ?
for this.
例如这是错误:
String sql = "INSERT INTO tablename (col1, col2, col3) VALUES (val1, val2, val3)";
// ...
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, val1); // Fail.
preparedStatement.setString(2, val2);
preparedStatement.setString(3, val3);
您需要相应地修复 SQL 查询字符串以指定占位符.
You need to fix the SQL query string accordingly to specify the placeholders.
String sql = "INSERT INTO tablename (col1, col2, col3) VALUES (?, ?, ?)";
// ...
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, val1);
preparedStatement.setString(2, val2);
preparedStatement.setString(3, val3);
注意参数索引以 1
开头,并且您确实不需要需要像这样引用这些占位符:
Note the parameter index starts with 1
and that you do not need to quote those placeholders like so:
String sql = "INSERT INTO tablename (col1, col2, col3) VALUES ('?', '?', '?')";
否则您仍然会得到相同的异常,因为 SQL 解析器会将它们解释为实际的字符串值,因此无法再找到占位符.
Otherwise you will still get the same exception, because the SQL parser will then interpret them as the actual string values and thus can't find the placeholders anymore.
- JDBC 教程 - 准备好的语句
这篇关于java.sql.SQLException 参数索引超出范围(1 > 参数个数,即 0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:java.sql.SQLException 参数索引超出范围(1 > 参数个数,即 0)


基础教程推荐
- 在螺旋中写一个字符串 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01