title: Java PreparedStatement: com.microsoft.sqlserver.jdbc.SQLServerException, index out of range
body: <p>I'm trying to execute a <code>SQL</code> query using a Java <code>PreparedStatement</code> in Java 7 using the code below:</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>I get the following error message: <code>com.microsoft.sqlserver.jdbc.SQLServerException: The index 2 is out of range.</code></p>
  
  <p>PS: I'm sure of the correctness of the SQL query because I successfully tested it without a <code>PreparedStatement</code>, I just replaced the real name of the columns in the query by fake ones (X, Y, Z) to hide potentially confidential information.</p>
  
  <p><strong>EDIT</strong>: I get a similar error when using <code>setString(1, x)</code> => <code>index 1 is out of range</code></p>