java.sql.SQLException: 插入数据时列索引无效

java.sql.SQLException: Invalid column index while inserting data

我试图将值插入 table,但它总是抛出 "java.sql.SQLException: Invalid column index"。据我所知,我已经验证了列索引。下面是我试过的。

stmt = con.prepareStatement("insert into test_table(ASSESMENT_YEAR,ASSESMENT_MON,CODE,KRA_CODE,BASE_VALUE,ACTUAL_VALUE,USERNAME,CHANGE_DATE)"
                + "values (?,?,?,?,?,?,?,SYSDATE)");

        stmt.setInt(1, Calendar.getInstance().get(Calendar.YEAR));
        stmt.setInt(2, Calendar.getInstance().get(Calendar.MONTH));
        stmt.setString(3, getcode(dm.getStr1(), dm.getStr2())); 
        stmt.setString(4, dm.getStr4());
        stmt.setInt(5, dm.getNum1());
        stmt.setInt(6, dm.getNum2());
        stmt.setString(7, "admin");

以上是在第 4 个 stmt 中抛出错误。但是如果我硬编码如下值,数据就会正确插入。

        stmt.setInt(1, 2018);
        stmt.setInt(2, 12);
        stmt.setString(3, "1111"); 
        stmt.setString(4, "0000");
        stmt.setInt(5, 12);
        stmt.setInt(6, 1);
        stmt.setString(7, "admin");

如有任何帮助,我们将不胜感激!!

编辑: 下面是堆栈跟踪

java.sql.SQLException: Invalid column index
    at oracle.jdbc.driver.OraclePreparedStatement.setStringInternal(OraclePreparedStatement.java:5386)
    at oracle.jdbc.driver.OraclePreparedStatement.setString(OraclePreparedStatement.java:5374)
    at oracle.jdbc.driver.OraclePreparedStatementWrapper.setString(OraclePreparedStatementWrapper.java:282)
    at com.dao.KRADataDao.addData(KRADataDao.java:59)
    at com.service.Controller.service(Controller.java:56)
    at com.service.Dispatcher.service(Dispatcher.java:25)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:475)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:625)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:498)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:796)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1372)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

下面的部分也不起作用:

        stmt.setInt(1, Calendar.getInstance().get(Calendar.YEAR));
        stmt.setInt(2, Calendar.getInstance().get(Calendar.MONTH));
        stmt.setString(3, getcode(dm.getStr1(), dm.getStr2())); 
        stmt.setString(4, "0000");
        stmt.setInt(5, dm.getNum1());
        stmt.setInt(6, dm.getNum2());
        stmt.setString(7, "admin");

它可能是 stmt 的重用。对本地实例使用 try-with-resources 语法,即使出现异常也会自动关闭:

try (PreparedStatement stmt = con.prepareStatement("insert into test_table"
            + "(ASSESMENT_YEAR,ASSESMENT_MON,CODE,KRA_CODE,BASE_VALUE,ACTUAL_VALUE,"
            + "USERNAME,CHANGE_DATE)"
            + "values (?,?,?,?,?,?,?,SYSDATE)")) {
    stmt.setInt(1, Calendar.getInstance().get(Calendar.YEAR));
    stmt.setInt(2, Calendar.getInstance().get(Calendar.MONTH));
    stmt.setString(3, getcode(dm.getStr1(), dm.getStr2())); 
    stmt.setString(4, dm.getStr4());
    stmt.setInt(5, dm.getNum1());
    stmt.setInt(6, dm.getNum2());
    stmt.setString(7, "admin");

    stmt.executeUpdate();
}

我的猜测是在 getcode 中发生了 stmt 的重用。

一般总是使用局部变量,try-with-resources。一些 PreparedStatements 可能受益于更长的范围,但固定 SQL,然后名称不应该是 stmt

如果该列不存在,或者必须输入stmt.executeUpdate();声明。

不知道你用的是什么数据库系统。 所以请确认 CODE 不是保留字/关键字。

如果是这样,请重命名第三列,然后重试。