简体   繁体   中英

com.microsoft.sqlserver.jdbc.SQLServerException: The index 2 is out of range :(

I have following code in java

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();

this code is giving me an IndexOutOfBoundsException as:

Database Exception Occured

We are sorry for inconvenience

Exception Details: com.microsoft.sqlserver.jdbc.SQLServerException: The index 2 is out of range. Details:

The index 2 is out of range.

Stack Trace:

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)

In database I have 4 columns usrid, username, password, email

username is of nvarchar(MAX) type.

Everything seems fine but I am still getting this exception why?? [The line 55 in stacktrace is the line ps.setString(2,newUp.getUsrName());]

Not directly related to your issue but it is possible to get this error if you miss a single quote somewhere when using XQuery within SQL. I had this with the following SQL:

UPDATE TABLE SET CustomFields.modify('replace value of (/CustomFields/Field[@ID=1]/Value)[1] with "HELLO WORLD!") WHERE ID=?

It needed to be

UPDATE TABLE SET CustomFields.modify('replace value of (/CustomFields/Field[@ID=1]/Value)[1] with "HELLO WORLD!"') WHERE ID=?

(single quote missing after with "HELLO WORLD!")

So the exception in my case was the wrong exception thrown by the SQL driver.

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