簡體   English   中英

參數索引超出范圍(4 > 參數個數,即 2)

[英]Parameter index out of range (4 > number of parameters, which is 2)

我正在制作一個簡單的 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)。 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974 ) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919) at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3813) at com.Z81C3B080DAD537DE7E10E09 87A4BF52EZ.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3795) at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3840) at com.mysql.jdbc.PreparedStatement.setInt(PreparedStatement.java:3784) at HistoryController .insertIntoHistory(HistoryController.java:37) at MultipleChoiseQuestion.jButton2ActionPerformed(MultipleChoiseQuestion.java:223) at MultipleChoiseQuestion.access$700(MultipleChoiseQuestion.java:20) at MultipleChoiseQuestion$7.actionPerformed(MultipleChoiseQuestion.java:141) at javax.swing.AbstractButton .fi reActionPerformed(AbstractButton.java:2022) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel .java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6533) at javax.swing.JComponent.processMouseEvent(JComponent. A t java.awt.Container.processEvent(Container.java:2237) at java.awt.Component.dispatchEventImpl(Component.java:4889) at java.awt.Container.dispatchEventImpl(Container.java:2295) at java.awt. Component.dispatchEvent(Component.java:4711) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4889) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4526) at java.awt.LightweightDispatcher.dispatchEvent(Container .java:4467) 在 java.awt.Container.dispatchEventImpl(Container.java:228) 5A07423FE1C889F448B33D21F46Z.awt.Component.dispatchEvent(Component.java:4711) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt. EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege( ProtectionDomain.java:80) 在 java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.Z93F725A07423331C889ZD548B) A07423FE1C889F448B33D21F46Z.awt.EventQueue$4.run(EventQueue.java:731) at java.awt.EventQueue$4.run(EventQueue.java:729) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80) at java.awt.EventQueue.dispatchEvent(EventQueue.java:728) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread .java:116) 在 java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.Z93F725A07423FE1C889F 448B33D21F46Z:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82) BUILD成功(總時間: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;
}

這是我的表結構在此處輸入圖像描述

這里發生了什么請幫助我

問題可能源於您嘗試將NULL插入id列。 由於它是自動遞增的非空值,因此您應該假裝它不存在以用於INSERT命令。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM