Oracle试用PreparedStatement出现java.sql.SQLException: 无效的列索引

本文介绍了一种在Java程序中调用SQL语句时遇到的关于日期参数设置的问题及解决方案。通过调整SQL语句中日期参数的格式声明,成功解决了因无效的列索引引发的异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

sql语句如下:select bu.deptno,count(bu.hiredate) StartMonthCount from userinfo bu,dept d where bu.deptno=d.deptno and bu.hiredate<to_date(‘?’,'yyyy-mm-dd') group by bu.deptno

java程序中调用:pstmt.setString(1, firstDay);

出现如下异常

java.sql.SQLException: 无效的列索引

at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:208)
at oracle.jdbc.driver.OraclePreparedStatement.setStringInternal(OraclePreparedStatement.java:5329)
at oracle.jdbc.driver.OraclePreparedStatement.setString(OraclePreparedStatement.java:5321)
at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.setString(DelegatingPreparedStatement.java:135)
at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.setString(DelegatingPreparedStatement.java:135)
at com.neusoft.hr.model.dao.impl.ReportDaoImpl.findStartMonthCount(ReportDaoImpl.java:258)
at com.neusoft.hr.model.service.ReportService.findMonthReportList(ReportService.java:77)
at com.neusoft.hr.controller.ReportAction.monthReport(ReportAction.java:98)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:269)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:170)
at org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
at org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.neusoft.hr.utils.EncodingFilter.doFilter(EncodingFilter.java:29)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:861)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1584)

at java.lang.Thread.run(Thread.java:619)



经过仔细排查,原来是日期转换的时候出问题了

将select bu.deptno,count(bu.hiredate) StartMonthCount from userinfo bu,dept d where bu.deptno=d.deptno and bu.hiredate<to_date(‘?’,'yyyy-mm-dd') group by bu.deptno

改为select bu.deptno,count(bu.hiredate) StartMonthCount from userinfo bu,dept d where bu.deptno=d.deptno and bu.hiredate<to_date(?,'yyyy-mm-dd') group by bu.deptno

问题随之解决了。

public static void getconnect(String webName,String detailUrl,String title,String detailResult,String date,String currDate) throws Exception { // DriverManager 注册驱动 // Connection 数据库连接对象 url(指定连接的路径 语法:“jdbc:mysql://ip地址:端口号/数据库名称”) Class.forName("oracle.jdbc.driver.OracleDriver"); Connection com = DriverManager.getConnection("jdbc:oracle:thin:@192.168.2.42:1521:orcl", "bxkc", "bxkc"); System.out.println("successfully"); try { // 创建预编译的SQL插入语句 String sql = "inset into XIN_XI_INFO_TEST(SOURCE_NAME,DETAIL_LINK,DETAIL_TITLE,DETAIL_CONTENT,PAGE_TIME," + "CREATE_TIME,LIST_TITLE,CREATE_BY)" + "value (?,?,?,?,?,?,?)"; PreparedStatement preparedStatement = com.prepareStatement(sql); // 设置参数的值 preparedStatement.setString(1, webName);//网站名 preparedStatement.setString(2, detailUrl);//详情链接 preparedStatement.setString(3, title);//详情标题 preparedStatement.setString(4, detailResult);//正文HTML preparedStatement.setString(5, date);//列表时间 preparedStatement.setString(6, currDate);//创建时间 preparedStatement.setString(7, title);//列表标题 preparedStatement.setString(8, "zzh");//创建者名称 // 执行预编译的语句 int rowsAffected = preparedStatement.executeUpdate(); if (rowsAffected > 0) { System.out.println("Insertion successful. Rows affected: " + rowsAffected); } else { System.out.println("Insertion failed."); } // 关闭PreparedStatement preparedStatement.close(); } catch (SQLException e) { e.printStackTrace(); } // 执行 增删改查 (DML)语句用 int executeUpdate(Sting sql); // 执行 DQL 语句 ResultSet executeQuery(String sql); // 对象释放 void close(); com.close(); }无效列索引这是什么意思
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值