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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9759319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:18:50  来源:igfitidea点击:

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

javasql-serverexception

提问by Aishwarya Shiva

I have following code in java

我在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 IndexOutOfBoundsExceptionas:

这段代码给了我一个IndexOutOfBoundsException

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)

发生数据库异常

给您带来不便,我们深表歉意

异常详细信息:com.microsoft.sqlserver.jdbc.SQLServerException:索引 2 超出范围。细节:

索引 2 超出范围。

堆栈跟踪:

com.microsoft.sqlserver.jdbc.SQLServerException:索引 2 超出范围。在 com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:171) 在 com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setterGetParam(SQLServerPreparedStatement.java:700) 在 com.microsoft.sqlserver.jdbc.SQLServerValuePreparedStatement.set (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) 在 javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304) ) 在 org.apache.catalina.core。

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

在数据库中我有 4 列 usrid, username, password, email

username is of nvarchar(MAX)type.

用户名是nvarchar(MAX)类型。

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

一切似乎都很好,但我仍然收到此异常,为什么?[stacktrace 中的第 55 行是 ps.setString(2,newUp.getUsrName());]

回答by sproketboy

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:

与您的问题没有直接关系,但如果在 SQL 中使用 XQuery 时在某处遗漏了单引号,则可能会出现此错误。我使用以下 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!")

(“HELLO WORLD!”后缺少单引号)

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

所以在我的情况下,异常是 SQL 驱动程序抛出的错误异常。