参数索引超出范围(4>参数数量,即2)

时间:2019-10-25 13:52:44

标签: java mysql sql jdbc

我正在制作一个简单的java jdbc程序,其中我必须在表上插入用户历史记录,但是那里有很多库存。这是我的代码

public void insertIntoHistory(ArrayList<QuestionAnswer> questionAnswer) throws Exception {
    try {
        conn.setAutoCommit(false);
        int maxId = getMaxSets() + 1;

        for (QuestionAnswer qa : questionAnswer) {
            String getSql = "INSERT INTO userhist(id, questionid, "
                    + "givenanswerid, isCorrect, questionSet) "
                    + "VALUES (NULL,?,?,?,?)";

            pst = conn.prepareStatement(getSql);
            pst.setInt(1, qa.getQuestionId());
            pst.setInt(2, qa.getAnswerId());
            pst.setBoolean(3, (isCorrect(qa.getQuestionId(), qa.getAnswerId())));
            pst.setInt(4, maxId);

            pst.executeUpdate();
        }

        conn.commit();
    } catch (SQLException e) {
        System.out.println(e.getMessage());
        conn.rollback();
        throw e;
    }
}

错误是:

  

参数索引超出范围(4>参数数,为0)。   java.sql.SQLException:参数索引超出范围(4>个   参数,即0)。在   com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)在   com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)位于   com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)在   com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)位于   com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3813)     在   com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3795)     在   com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3840)     在   com.mysql.jdbc.PreparedStatement.setInt(PreparedStatement.java:3784)     在HistoryController.insertIntoHistory(HistoryController.java:37)处   MultipleChoiseQuestion.jButton2ActionPerformed(MultipleChoiseQuestion.java:223)     在MultipleChoiseQuestion.access处获得700美元(MultipleChoiseQuestion.java:20)     在   MultipleChoiseQuestion $ 7.actionPerformed(MultipleChoiseQuestion.java:141)     在   javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)     在   javax.swing.AbstractButton $ Handler.actionPerformed(AbstractButton.java:2348)     在   javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)     在   javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)     在   javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)     在java.awt.Component.processMouseEvent(Component.java:6533)在   javax.swing.JComponent.processMouseEvent(JComponent.java:3324)在   java.awt.Component.processEvent(Component.java:6298)在   java.awt.Container.processEvent(Container.java:2237)在   java.awt.Component.dispatchEventImpl(Component.java:4889)在   java.awt.Container.dispatchEventImpl(Container.java:2295)在   java.awt.Component.dispatchEvent(Component.java:4711)在   java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4889)     在   java.awt.LightweightDispatcher.processMouseEvent(Container.java:4526)     在java.awt.LightweightDispatcher.dispatchEvent(Container.java:4467)     在java.awt.Container.dispatchEventImpl(Container.java:2281)在   java.awt.Component.dispatchEvent(Component.java:4711)在   java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)在   java.awt.EventQueue.access $ 500(EventQueue.java:97)在   java.awt.EventQueue $ 3.run(EventQueue.java:709)在   java.awt.EventQueue $ 3.run(EventQueue.java:703)在   java.security.AccessController.doPrivileged(本机方法),位于   java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)     在   java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)     在java.awt.EventQueue $ 4.run(EventQueue.java:731)在   java.awt.EventQueue $ 4.run(EventQueue.java:729)在   java.security.AccessController.doPrivileged(本机方法),位于   java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)     在java.awt.EventQueue.dispatchEvent(EventQueue.java:728)在   java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)     在   java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)     在   java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)     在   java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)     在   java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)     在java.awt.EventDispatchThread.run(EventDispatchThread.java:82)   建立成功(总时间:6秒)

我认为问题来自这种方法

private boolean isCorrect(int questionId, int ansId) throws SQLException {
    String sql = "SELECT COUNT(*) AS Total FROM correctanswer where questionid = ? and answerid = ?";
    boolean isCorrect = false;

    try {
        pst = conn.prepareStatement(sql);
        pst.setInt(1, questionId);
        pst.setInt(2, ansId);

        ResultSet res = pst.executeQuery();
        int count = 0;
        if (res.next()) {
            count = res.getInt(1);
        }

        isCorrect = count > 1;

    } catch (SQLException e) {
        System.out.println(e.getMessage());
        throw e;
    }

    return isCorrect;
}

这是我的表结构 enter image description here

这里发生了什么,请帮助我

1 个答案:

答案 0 :(得分:0)

问题可能源于您尝试在Desk Fan *列中插入NULL。由于它是自动递增非空值,因此您应该假装它不存在,以用于id命令。