SQL Server com.microsoft.sqlserver.jdbc.SQLServerException:索引2超出范围:(

63lcw9qa  于 2022-12-28  发布在  其他
关注(0)|答案(1)|浏览(145)

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

q5iwbnjs

q5iwbnjs1#

与您的问题没有直接关系,但如果您在SQL中使用XQuery时在某处遗漏了一个单引号,则可能会出现此错误。

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

必须如此

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

(“HELLO WORLD!”后缺少单引号)
因此,在我的例子中,异常是SQL驱动程序抛出的错误异常。

相关问题