2017-03-01 86 views
0

我有我的代码中的错误数据库,PostgreSQL的PSQL异常在插入使用Java

java.lang.RuntimeException: org.postgresql.util.PSQLException: The column index is out of range: 12, number of columns: 11 

我花了一些时间寻找不同的解决方案,但都无济于事。

这是我的代码,我有我的SQL字符串,

public void insertToDB(Active active) { 

     String sql = "INSERT INTO active (branchname, branchcode, sidnew, sidold, accountno, openingbalance, " 
       + "paymentreceived, currentcharges, monthlyrecrate, invoiceno, category)" 
       + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; 

     Connection conn = null; 

     try { 

      conn = dataSource.getConnection(); 
      PreparedStatement ps = conn.prepareStatement(sql); 

      ps.setString(3, active.getBranchname()); 
      ps.setString(4, active.getBranchcode()); 
      ps.setString(5, active.getSidnew()); 
      ps.setString(6, active.getSidold()); 
      ps.setString(7, active.getAccountno()); 
      ps.setString(8, active.getOpeningbalance()); 
      ps.setString(9, active.getPaymentreceived()); 
      ps.setString(10, active.getCurrentcharges()); 
      ps.setString(11, active.getMonthlyrecrate()); 
      ps.setString(12, active.getInvoiceno()); 
      ps.setString(13, active.getCategory()); 

      ps.executeUpdate(); 
      ps.close(); 

     } catch (SQLException e) { 
      throw new RuntimeException(e); 

     } finally { 
      if (conn != null) { 
       try { 
        conn.close(); 
       } catch (SQLException e) {} 
      } 
     } 

    } 

下面是从我的Eclipse控制台日志。

了java.lang.RuntimeException:org.postgresql.util.PSQLException:列索引超出范围:12,列数:11在 com.joven.dao.ActiveDAOImpl.insertToDB(ActiveDAOImpl。的java:74) 在com.joven.core.ActiveAccountsReadExcel.getExcelInfo(ActiveAccountsReadExcel.java:138) 在com.joven.core.ActiveAccountsReadExcel.ValidateExcelFormat(ActiveAccountsReadExcel.java:56) 在com.joven.core.ActiveAccountsReadExcel。 buttonChooseFile(ActiveAccountsReadExcel.java:41) at com.joven.ui.MainView $ 3.handle(MainView.java:110) at com.joven.ui.MainView $ 3.handle(MainView.java:1) at com。 sun.jav afx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86) 在com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) 在com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager。 java:191) at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) at com.sun。 javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 在com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl。 java:114) 在com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 在com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在com.sun.javafx.event。 EventUtil.fireEventImpl(EventUtil.java:74) at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49) at javafx.event.Event.fireEvent(Event.java:198) at javafx。 scene.Node.fireEvent(Node.java:8413) at javafx.scene.control.Button.fire(Button.java:185) at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior。 java:182) at com.sun.javafx.scene.control.skin.BehaviorSkinBase $ 1.handle(BehaviorSkinBase.java:96) at com.sun.javafx.scene.contr ol.skin.BehaviorSkinBase $ 1.handle(BehaviorSkinBase.java:89) at com.sun.javafx.event.CompositeEventHandler $ NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218) at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent (CompositeEventHandler.java:80) 在com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) 在com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) 在玉米.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) 在com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) 在com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent (EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher。的java:56) 在com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 在的com.sun。 javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) at com.sun.javafx.event.EventUtil.fireEvent(EventUtil。 java:54) at javafx.event.Event.fireEvent(Event.java:198) at javafx.scene.Scene $ MouseHandler.process(Scene.java:3757) at javafx.scene.Scene $ MouseHandler.access $ 1500 (Scene.java:3485) at javafx.swf.scene.Scene.impl_processMouseEvent(Scene.java:1762) at javafx。 scene.Scene $ ScenePeerListener.mouseEvent(Scene.java:2494) 在com.sun.javafx.tk.quantum.GlassViewEventHandler $ MouseEventNotification.run(GlassViewEventHandler.java:394) 在com.sun.javafx.tk.quantum。 GlassViewEventHandler $ MouseEventNotification.run(GlassViewEventHandler.java:295) 在java.security.AccessController.doPrivileged(本机方法) 在com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda $ handleMouseEvent $ 2(GlassViewEventHandler.java:432) 在com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389) 在com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431) 在的com.sun。 glass.ui.View.handleMouseEvent(View.java:555) at com.sun.glass.ui.View.notifyM乌斯(View.java:937) 所致:org.postgresql.util.PSQLException:列索引超出范围:12,列数:11在 org.postgresql.core.v3.SimpleParameterList.bind( SimpleParameterList.java:56) 在org.postgresql.core.v3.SimpleParameterList.setStringParameter(SimpleParameterList.java:118) 在org.postgresql.jdbc2.AbstractJdbc2Statement.bindString(AbstractJdbc2Statement.java:2310) 在org.postgresql。 jdbc2.AbstractJdbc2Statement.setString(AbstractJdbc2Statement.java:1398) 在org.postgresql.jdbc2.AbstractJdbc2Statement.setString(AbstractJdbc2Statement.java:1384) 在org.apache.commons.dbcp.DelegatingPreparedStatement.setString(DelegatingPreparedStatement.java:135) at org.apache.commons.dbcp.DelegatingPreparedState ment.setString(DelegatingPreparedStatement.java:135) 在com.joven.dao.ActiveDAOImpl.insertToDB(ActiveDAOImpl.java:67) ...... 49多个

任何帮助表示赞赏。谢谢!

+3

您所查询的只有11'?'占位符,你不能叫'ps.setString'与指数> 11。 – Berger

+0

棒极了!我认为带索引的'ps.setString'是PostgreSQL表中的位置。谢谢!我如何接受它作为答案? – ashlrem

+0

我说这下面的答案。 – Berger

回答

1

您所查询的只有11 ?占位符,你不能叫ps.setString与指数大于11

可能值的范围从1到你的情况11