Com.microsoft.sqlserver.jdbc.SQLServerException: Index 2 is out of range :(

I have the 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 gives me IndexOutOfBoundsException

like:

Database Exception

We are sorry for the inconvenience

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

Index 2 is out of range.

Stack trace:

com.microsoft.sqlserver.jdbc.SQLServerException: index 2 missing assortment. at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError (SQLServerException.java:171) at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setterGetParam (SQLServerPreparedStatement) in com.microsoft.SQLServerPreparedStatement.setterGetParam (SQLServerPreparedStatement) (SQLServerPreparedStatement.java:709) at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setString (SQLServerPreparedStatement.java:1034) at DBOps.addUser (DBOps.java:55) at RegUser.do.javava (RegUser.do.javava) .servlet.http.HttpServlet.service (HttpServlet.java:641) at javax.servlet.http.HttpServlet.service (HttpServlet.java:722) at org.apache.catalina.core.ApplicationFilterDoChain.internalF:30 ) in 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 (Standard:164apalve.java) .catalina.authenticator.AuthenticatorBase.invoke (AuthenticatorBase.java:462) at org.apache.catalina.core.StandardHostValve.invoke (StandardHostValve.java:164) at org.apache.catalina.valves.ErrorinReportValve (ErrorValve. : 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 $ Http11.ConnectionConnectionprocess (Http11AprProtocol.java:272) in org.apache.tomcat.util.net.AprEndpoint $ SocketProcessor.run (AprEndpoint.java:1730) in java.util.concurrent.ThreadPoolExecutor $ Worker.runTask (Unknown Source) in java. util.concurrent.ThreadPoolExecutor $ Worker.run (Unknown Source) at java.lang.Thread.run (Unknown Source)

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

username is of type nvarchar(MAX)

.

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

+3


source to share


1 answer


Not directly related to your problem, but you can get this error if you miss a single quote somewhere when using XQuery in SQL. I had it with the following SQL:

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

      

It should be



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

      

(no single quote after "HELLO WORLD!")

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

0


source







All Articles