问题描述
我正面临 java.sql.SQLException:参数索引超出范围(3>参数数量,即2)。同时更新'reset_info'的一列或两列有五列的表(id,mobile_tower_id,reset_value,date_time,clientip)。
'id'是自动生成的主键。
我想要特定行的更新'reset_value'
I am facing java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2). while updating one or two columns of 'reset_info' table which has five columns (id, mobile_tower_id, reset_value, date_time, clientip).'id' is auto-generated primary key.I want to UPDATE 'reset_value' of a particular row
现在以下是源代码:
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
String sql = "UPDATE reset_info SET reset_value = ? WHERE id = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, 100);
ps.setInt(3, 1000);
ps.executeUpdate();
conn.close();
以下是我的堆栈跟踪:
Connecting to database...
java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2).
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:975)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920)
at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3813)
at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3795)
at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3840)
at com.mysql.jdbc.PreparedStatement.setInt(PreparedStatement.java:3784)
at cdot.dsa.cfms.dbconnect.NewClass.main(NewClass.java:38)
是否必须为表的每一列添加占位符('?')?
Is it mandatory for to put placeholder('?') for each column of the table?
通过java.sql.Statement更新单个列是有效的,但我在java.sql.preparedStatement中遇到异常。
Updating a single column by java.sql.Statement is working but I am getting exception with java.sql.preparedStatement.
请帮忙。概念性的想法将受到高度赞赏。
Please help. Conceptual ideas will be highly appreciated.
推荐答案
这个 ps.setInt(3,1000);
必须是 ps.setInt(2,1000);
因为你只需要占位符。
This ps.setInt(3, 1000);
must be ps.setInt(2, 1000);
because you only have to placeholder.
占位符用于不是列的参数!
The placeholder is for a parameter not for a column!
这篇关于JDBC UPDATE使用preparedStatement导致java.sql.SQLException:参数索引超出范围(3>参数个数,即2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!