我试图将值插入表中,但是它总是抛出“ java.sql.SQLException:无效的列索引”。据我所知,我已经验证了列索引。下面是我尝试过的。
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");
答案 0 :(得分:0)
可能是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。某些PreparedStatement可能会受益于更长的范围,但使用固定的SQL,则名称不应为stmt
。
答案 1 :(得分:0)
如果该列不存在,或者您必须输入stmt.executeUpdate();。声明。
答案 2 :(得分:0)
我不知道您使用的是哪个数据库系统。 因此,请确认 CODE 不是保留字/关键字。
在这种情况下,请重命名第三列,然后重试。