简体   繁体   中英

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:

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.

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.

EDIT : I get a similar error when using 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):

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)

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. 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); , 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 ? in the prepared statement and sets the value to prepared statement accordingly.

So in your case there is only 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

index 2 is out of range.

Try the same with index 1. as you have only 1 parameter to be set.

eg functionalCRsStatement.setString(1, x);

remember the value x will be stored to the ? 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). in this case it will not able to find first index os String and throw an error of index out of range.

The prepared statement is not recognizing any param, for is this query contains 0 params because of mal-written string; 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 = ?");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM