java - executeUpdate() 抛出 ArrayIndexOutOfBoundsException

标签 java sql prepared-statement indexoutofboundsexception

我一直在尝试执行以下查询:

SQL_ADDPROJECT = "INSERT INTO project (name, description, duration, departement, chef) VALUES (?, ?, ?, ?, ?)";

使用:

public boolean addProject(String name, String description, String duration, String budget, int chef, int departement) {
    try {
        addProject_statement = connection.prepareStatement(SQL_ADDPROJECT);
        addProject_statement.setString(1, name);
        addProject_statement.setString(2, description);
        addProject_statement.setString(3, duration);
        addProject_statement.setString(4, budget);
        addProject_statement.setInt(5, chef);
        addProject_statement.setInt(6, departement);

        int res = addProject_statement.executeUpdate();
        if ( res != 0 ) {
            addProject_statement.close();
            return true;
        } 
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e.getMessage());
    }
    return false;               
}

我这样调用addProject:

save_button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        String name = nameField.getText();
        String description = descField.getText();
        String duration = durationField.getText();
        String budget = budgetField.getText();
        int chef = User.userId;
        int departement = User.departementId;
        if(name != null && duration != null) {
            if ( dao.addProject(name, description, duration, budget, chef, departement) ) {
                JOptionPane.showMessageDialog(null, "Project " + name + " created successfully!");
                MainMenu menu = new MainMenu();
                menu.setVisible(true);
                frame.dispose();
            } else {
                JOptionPane.showMessageDialog(null, "Please fill in the form correctly!");
            }
            return;
        }
    }
});

但每次我尝试执行时,我都会收到一个消息对话框,上面写着 5

随后是另一个消息对话框,内容为请正确填写表格

知道我确实填写正确了!

追踪:

java.lang.ArrayIndexOutOfBoundsException: 5
    at org.sqlite.core.CorePreparedStatement.batch(CorePreparedStatement.java:121)
    at org.sqlite.jdbc3.JDBC3PreparedStatement.setInt(JDBC3PreparedStatement.java:324)
    at database.SqlConnection.addProject(SqlConnection.java:75)
    at project.addProject$4.actionPerformed(addProject.java:196)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

最佳答案

addProject_statement.setString(4,budget);SQL 语句中没有匹配的参数,因此值的数量(及其类型)不匹配。


对于

SQL_ADDPROJECT = "INSERT INTO project (name, description, duration, budget, departement, chef) VALUES (?, ?, ?, ?, ?, ?)";

使用

 addProject_statement.setString(1, name);
 addProject_statement.setString(2, description);
 addProject_statement.setString(3, duration);
 addProject_statement.setString(4, budget);
 addProject_statement.setInt(5, departement);
 addProject_statement.setInt(6, chef);

关于java - executeUpdate() 抛出 ArrayIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44480449/

相关文章:

java - ArrayIndexOutOfBoundsException 试图访问数据

Java socketChannel 检测无序关闭连接

java - 在Java中获取和修改集合中的对象

sql - CONTAINS 与条件逻辑组合时的效率问题

java - PreparedStatement 导致 OOM 错误

java - 迭代计算任意数量集合的笛卡尔积

sql - 使用 jsonb_set() 进行更新仅影响嵌套数组中的一个对象

mysql - JOIN 或 LEFT JOIN 是否持续检查 SELECT 查询?

php - PDO 准备/执行 SQL 问题

php - 准备好的陈述不存在