executeUpdate()抛出一个ArrayIndexOutOfBoundsException

时间:2017-06-11 04:45:12

标签: 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

然后是另一个消息对话框,其中显示Please fill in the form correctly

知道我确实填写正确!

追踪:

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)

3 个答案:

答案 0 :(得分:2)

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);

答案 1 :(得分:1)

我相信你在addProject_statement中添加了6个值,但Insertquery中只有5个参数,这就是造成这个问题的原因......

答案 2 :(得分:1)

你的陈述没有订购。没有预算和部门在厨师面前。

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, departement);
        addProject_statement.setInt(5, chef);