title: com.microsoft.sqlserver.jdbc.SQLServerException: The index 2 is out of range :(
body: <p>I have following code in java</p>
  
  <pre><code>qry="insert into usr_auth(usrid,username,password,email) values(?,?,?,?)";
              ps=con.prepareStatement(qry);
              ps.setString(1,getUniqueID());
              ps.setString(2,newUp.getUsrName());
              ps.setString(3,newUp.getPwd());
              ps.setString(4,newUp.getEmail());
              ps.executeUpdate();
  </code></pre>
  
  <p>this code is giving me an <code>IndexOutOfBoundsException</code> as:</p>
  
  <blockquote>
    <p>Database Exception Occured</p>
    
    <p>We are sorry for inconvenience</p>
    
    <p>Exception Details: com.microsoft.sqlserver.jdbc.SQLServerException:
    The index 2 is out of range. Details:</p>
    
    <p>The index 2 is out of range.</p>
    
    <p>Stack Trace:</p>
    
    <p>com.microsoft.sqlserver.jdbc.SQLServerException: The index 2 is out of
    range. at
    com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:171)
    at
    com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setterGetParam(SQLServerPreparedStatement.java:700)
    at
    com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setValue(SQLServerPreparedStatement.java:709)
    at
    com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setString(SQLServerPreparedStatement.java:1034)
    at DBOps.addUser(DBOps.java:55) at RegUser.doPost(RegUser.java:13) at
    javax.servlet.http.HttpServlet.service(HttpServlet.java:641) at
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722) at
    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at
    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at
    org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at
    org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at
    org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
    at
    org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:403)
    at
    org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:286)
    at
    org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:272)
    at
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1730)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown
    Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown
    Source) at java.lang.Thread.run(Unknown Source)</p>
  </blockquote>
  
  <p>In database I have 4 columns <code>usrid, username, password, email</code></p>
  
  <p>username is of <code>nvarchar(MAX)</code> type.</p>
  
  <p>Everything seems fine but I am still getting this exception why?? [The line 55 in stacktrace is the line ps.setString(2,newUp.getUsrName());]</p>