Java JDBC SQLException:参数索引超出范围

时间:2016-05-25 17:05:29

标签: java jdbc

我真的无法识别问题,也不知道我在这里错过了什么错误。这是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)

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