问题描述
2013-08-03 ==========================================================================================客户端更新语句:update cost_xjz set status='0',startime=to_date('2013-08-01 16:30:22','yyyy-mm-dd hh24:mi:ss') where id = 21;上面是客户端更新语句,使用完全正常无任何问题! =========================================================================================JDBC更新语句:"UPDATE COST_XJZ SET STATUS='0',STARTIME=TO_DATE('?','YYYY-MM-DD HH24:MI:SS') WHERE ID = ?"; =======================================================================================JDBC 更新方法:public void openCost(int id) throws SQLException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String t = sdf.format(new Date()); update(OPEN_COST, new Object[]{"0",t,id});}update是我自己封装的基于JDBC技术更新数据表的方法,已测试多种表,完全无任何问题,就不贴了。 ============================================================================================下面是建表语句:CREATE TABLE COST_XJZ(ID NUMBER(4) CONSTRAINT COST_ID_PK PRIMARY KEY,NAME VARCHAR(50) NOT NULL,BASE_DURATION NUMBER(11),BASE_COST NUMBER(7,2),UNIT_COST NUMBER(7,4), STATUS CHAR(1) CONSTRAINT COST_STATUS_CK CHECK (STATUS IN (0,1)),DESCR VARCHAR2(100),CREATIME DATE DEFAULT SYSDATE ,STARTIME DATE, COST_TYPE CHAR(1));=============================================================================下面是控制台打印的异常信息:java.sql.SQLException: 无效的列索引 at oracle.jdbc.driver.OraclePreparedStatement.setStringInternal(OraclePreparedStatement.java:5168) at oracle.jdbc.driver.OraclePreparedStatement.setObjectCritical(OraclePreparedStatement.java:8098) at oracle.jdbc.driver.OraclePreparedStatement.setObjectInternal(OraclePreparedStatement.java:8034) at oracle.jdbc.driver.OraclePreparedStatement.setObjectInternal(OraclePreparedStatement.java:8767) at oracle.jdbc.driver.OraclePreparedStatement.setObject(OraclePreparedStatement.java:8748) at oracle.jdbc.driver.OraclePreparedStatementWrapper.setObject(OraclePreparedStatementWrapper.java:230) at implement.BaseDAO.update(BaseDAO.java:57) at implement.CostDAOImpl.openCost(CostDAOImpl.java:125) at test.TestCostDAO.testOpen(TestCostDAO.java:85) 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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) =========================================================================================请各路大神指点下到底是什么地方出了问题!不胜感激!
解决方案
问题应该是在楼主将一个String类型的变量映射到数据看一个DATE类型的字段的问题,看你starttime字段的类型是DATE,但是你在JAVA中set的类型是String.两者映射不上,所以出现这个错误
解决方案二:
UPDATE COST_XJZ SET STATUS='0',STARTIME=TO_DATE(?,'YYYY-MM-DD HH24:MI:SS') WHERE ID = ?