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
st? You don't appear to do anything with it. You also don't need to 'prettify' the SQL inside a prepared statement - make it easy to read from the point of view of your code instead of littering the query string with superfluous tab and newline characters.index 2 is out of rangeexplains it. You should use1instead.1first and I get the same erroerindex 1 out of range?is inside a quoted string, so the API doesn't see it as a placeholder.