谁能告诉我PrepareStatement语法有什么问题吗
public ResultSet readSubSet(int No, String name, String case) throws SQLException {
preparedStatement = connect.prepareStatement("SELECT * FROM target WHERE myName=? AND myCase=? LIMIT ?,10");
preparedStatement.setString(8, name);
preparedStatement.setString(6, case);
preparedStatement.setInt(1, No);
resultSet = preparedStatement.executeQuery();
return resultSet;
}
运行后我得到:java.sql.SQLException:参数索引超出范围(8>参数数量,即2)。
我的表定义myName
和myCase
是 TEXT
,我可以使用 prepareStatement.setString
public ResultSet readSubSet(int No, String name, String case) throws SQLException {
preparedStatement = connect.prepareStatement("SELECT * FROM target WHERE myName=? AND myCase=? LIMIT ?,10");
preparedStatement.setString(8, name);
preparedStatement.setString(6, case);
preparedStatement.setInt(1, No);
resultSet = preparedStatement.executeQuery();
return resultSet;
}
After run I got: java.sql.SQLException: Parameter index out of range (8 > number of parameters, which is 2).
my table define myName
and myCase
is TEXT
, is that ok I use prepareStatement.setString
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您调用 setString 时,您会传入两个参数,第一个是您要填写的参数的索引,第二个是您要填写的值。在您的情况下,您需要
prepareStatement.setString(1,name);
填写第一个参数,prepareStatement.setString(2,case);
填写第二个参数,依此类推。When you call
setString
you pass in two arguments, the first is the index of the parameter you are filling in and the second is the value you are filling it with. In your case, you would wantprepareStatement.setString(1,name);
to fill in the 1st parameter,prepareStatement.setString(2,case);
to fill in the 2nd parameter, and so on.在你的SQL中,你只有3个变量,并且你在preparedStatement.setString(8, name)中将变量8设置为名称...
看看@ http://download.oracle.com/javase/tutorial/jdbc/basics/prepared.html#supply_values_ps
In your SQL you only have 3 variables and you are setting variable 8 as name in preparedStatement.setString(8, name)...
Look @ http://download.oracle.com/javase/tutorial/jdbc/basics/prepared.html#supply_values_ps