[英]Java PreparedStatement: com.microsoft.sqlserver.jdbc.SQLServerException, index out of range
I'm trying to execute a SQL
query using a Java PreparedStatement
in Java 7 using the code below: 我正在尝试使用以下代码在Java 7中使用Java
PreparedStatement
执行SQL
查询:
PreparedStatement functionalCRsStatement = con.prepareStatement(
"select * from openquery(SERVER,\n" +
"\t'Select X , Y, Z, A from D r\n" +
"\tINNER JOIN E c\n" +
"\tON r.RNID = c.RNID\n" +
"\twhere c.Y = ?')\n");
functionalCRsStatement.setString(2, x);
I get the following error message: com.microsoft.sqlserver.jdbc.SQLServerException: The index 2 is out of range.
我收到以下错误消息:
com.microsoft.sqlserver.jdbc.SQLServerException: The index 2 is out of range.
PS: I'm sure of the correctness of the SQL query because I successfully tested it without a PreparedStatement
, I just replaced the real name of the columns in the query by fake ones (X, Y, Z) to hide potentially confidential information. PS:我确信SQL查询的正确性,因为我在没有
PreparedStatement
情况下成功地对其进行了测试,我只是用伪造的(X,Y,Z)替换了查询中列的真实名称,以隐藏潜在的机密信息。
EDIT : I get a similar error when using setString(1, x)
=> index 1 is out of range
编辑 :使用
setString(1, x)
=> index 1 is out of range
时出现类似错误
As @JonK commented, you have apostrophes in your query, which means your parameter is actually inside a string where the SQL engine won't bind a value (whether you use 1 or 2 as the index): 正如@JonK所评论的那样,查询中带有撇号,这意味着您的参数实际上位于SQL引擎将不绑定值的字符串内(无论您使用1还是2作为索引):
PreparedStatement functionalCRsStatement = con.prepareStatement(
"select * from openquery(APRPRD,\n" +
"\t'Select X , Y, Z, A from D r\n" +
"\tINNER JOIN E c\n" +
"\tON r.RNID = c.RNID\n" +
"\twhere c.Y = ?')\n");
contains this query (with SQL syntax highlighting, which shows the whole string) 包含此查询(突出显示SQL语法,显示整个字符串)
select * from openquery(APRPRD,
'Select X , Y, Z, A from D r
INNER JOIN E c
ON r.RNID = c.RNID
where c.Y = ?')
A SQL engine never inspects the inside of a string. SQL引擎从不检查字符串的内部。 How would you insert a string containing a question mark otherwise?
否则如何插入包含问号的字符串?
It seems you only have one ?
看来你只有一个
?
in your statement, so you can't make a reference to the second index (2) in the functionalCRsStatement.setString(2, x);
在您的陈述中,因此您无法在
functionalCRsStatement.setString(2, x);
引用第二个索引(2 functionalCRsStatement.setString(2, x);
, because as it says, it's out of range. ,因为正如它所说,超出了范围。
you should use 你应该使用
functionalCRsStatement.setString(1, x);
您的查询中只有一个绑定变量占位符( ?
)-因此应将其绑定到索引为1
而不是2
的索引中:
functionalCRsStatement.setString(1, x); // Was 2 in the OP
You have only one parameter to set in your prepared statement. 在准备好的语句中只能设置一个参数。 the set method to set parameter in prepared statement checks index of the
?
在准备好的语句中设置参数的set方法检查
?
索引 in the prepared statement and sets the value to prepared statement accordingly. 在准备好的语句中,然后将值设置为准备好的语句。
So in your case there is only 1 ?
因此,在您的情况下只有1个
?
so in an array of values to be passed for prepared statement is 1. and you are trying to pass the value at the index 2 hence it says The 因此在准备好的语句要传递的值数组中为1,而您试图在索引2处传递值,因此它说
index 2 is out of range.
索引2超出范围。
Try the same with index 1. as you have only 1 parameter to be set. 尝试对索引1进行相同的操作,因为您只能设置1个参数。
eg functionalCRsStatement.setString(1, x); 例如functionalCRsStatement.setString(1,x);
remember the value x will be stored to the ?
还记得值x将被存储到
?
at 1st index in the prepared statement. 在准备好的语句中的第一个索引处。
EDIT : Also remember the type to the value to be passed. 编辑 :还记得要传递的值的类型。 if you are setting value of X as int you need to call setInt(1,x).
如果将X的值设置为int,则需要调用setInt(1,x)。 in this case it will not able to find first index os String and throw an error of index out of range.
在这种情况下,它将无法找到第一个索引os String并抛出索引错误超出范围。
The prepared statement is not recognizing any param, for is this query contains 0 params because of mal-written string; 准备好的语句无法识别任何参数,因为该查询是否包含0个参数,原因是字符串写错了; try this :
尝试这个 :
PreparedStatement functionalCRsStatement = con.prepareStatement(
"select * from openquery(APRPRD," +
"'Select X , Y, Z, A from D r" +
"INNER JOIN E c" +
"ON r.RNID = c.RNID ')" +
"where c.Y = ?");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.