Java JDBC SQLException:参数索引超出范围[重复]
Posted
技术标签:
【中文标题】Java JDBC SQLException:参数索引超出范围[重复]【英文标题】:Java JDBC SQLException: Parameter index out of range [duplicate] 【发布时间】:2016-05-25 17:05:28 【问题描述】:我真的无法确定问题,也不知道我在此错误中缺少什么。这是 servlet 代码,我所有的表列都是整数。我尝试了 setString、setInt,但没有运气。
Connection dbConnection = null;
try
dbConnection = makeNewConnection();
log.info("0:" + request.getParameter("textBox0"));
log.info("1:" + request.getParameter("textBox1"));
log.info("2:" + request.getParameter("textBox2"));
log.info("3:" + request.getParameter("textBox3"));
log.info("4:" + request.getParameter("textBox4"));
log.info("5:" + request.getParameter("textBox5"));
int applicationName = Integer.parseInt(request.getParameter("textBox0"));
String fiscalValue = request.getParameter("textBox1");
int totalTestCases = Integer.parseInt(request.getParameter("textBox2"));
int automatedTestCases = Integer.parseInt(request.getParameter("textBox3"));
int uiTestCases = Integer.parseInt(request.getParameter("textBox4"));
int apiTestCases = Integer.parseInt(request.getParameter("textBox5"));
String sqlQuery = "INSERT INTO DASHBOARD.AUTOMATION(id, fiscal, quarter, total, automated, coverage, ui,"
+ " service, target, increase, growth) VALUES(?,?,?,?,?,?,?,?,?,?,?)";
log.info("DB Connection : " + dbConnection == null);
PreparedStatement preparedStatement = dbConnection.prepareStatement(sqlQuery);
log.info("Executed the database object creation");
preparedStatement.setInt(0, applicationName);
int fiscalYear = Integer.parseInt(fiscalValue.split(".")[0]);
int fiscalQuarter = Integer.parseInt(fiscalValue.split(".")[1]);
preparedStatement.setInt(1,fiscalYear);
preparedStatement.setInt(2,fiscalQuarter);
preparedStatement.setInt(3,totalTestCases);
preparedStatement.setInt(4,automatedTestCases);
int automationCoverage = automatedTestCases / totalTestCases;
preparedStatement.setInt(5,automationCoverage);
preparedStatement.setInt(6,uiTestCases);
preparedStatement.setInt(7,apiTestCases);
preparedStatement.setInt(8,0);
preparedStatement.setInt(9,0);
preparedStatement.setInt(10,0);
int rowsAffected = preparedStatement.executeUpdate();
if(rowsAffected == 0)
consoleWriter.write("Insert failed. No of Rows Affeceted : " + rowsAffected);
else
consoleWriter.write("Insert Success. No of Rows Affeceted : " + rowsAffected);
preparedStatement.close();
consoleWriter.write("Reached Servlet. Success");
Java.sql.SQLException: Parameter index out of range (0 < 1 ).
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:2791)
at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:2827)
at com.mysql.jdbc.PreparedStatement.setInt(PreparedStatement.java:2778)
at com.reports.automation.scorecard.ScoreCardInformation.doPost(ScoreCardInformation.java:51)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2500)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2489)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
【问题讨论】:
第 51 行是哪一行?在 com.reports.automation.scorecard.ScoreCardInformation.doPost(ScoreCardInformation.java:51) preparedStatement.setInt(1, applicationName); 请在此处查看minimal reproducible example 的构成要素。 JDBC中的参数索引是从1开始的,所以需要从1开始,而不是0。 【参考方案1】:使用以下片段
preparedStatement.setInt(1, applicationName);
int fiscalYear = Integer.parseInt(fiscalValue.split(".")[0]);
int fiscalQuarter = Integer.parseInt(fiscalValue.split(".")[1]);
preparedStatement.setInt(2,fiscalYear);
preparedStatement.setInt(3,fiscalQuarter);
preparedStatement.setInt(4,totalTestCases);
preparedStatement.setInt(5,automatedTestCases);
int automationCoverage = automatedTestCases / totalTestCases;
preparedStatement.setInt(6,automationCoverage);
preparedStatement.setInt(7,uiTestCases);
preparedStatement.setInt(8,apiTestCases);
preparedStatement.setInt(9,0);
preparedStatement.setInt(10,0);
preparedStatement.setInt(11,0);
【讨论】:
以上是关于Java JDBC SQLException:参数索引超出范围[重复]的主要内容,如果未能解决你的问题,请参考以下文章
java.sql.SQLException:索引处缺少 IN 或 OUT 参数:: 1
以下错误是啥意思:java.sql.sqlexception 在索引处缺少输入或输出参数
java.sql.SQLException: SQL 语句在 org.hsqldb.jdbc.JDBCUtil.sqlException 处关闭
java.sql.SQLException: 索引中丢失 IN或OUT 参数::x
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Invalid parameter binding(s)
无法获得 JDBC 连接;嵌套异常是 java.sql.SQLException:无法加载 JDBC 驱动程序类 'org.hsql.jdbcDriver'