mari
mari

Reputation: 335

executeUpdate() throws an ArrayIndexOutOfBoundsException

I have been trying to execute the following query :

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

using:

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

I call addProject this way :

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

but everytime I try to execute i get a message dialogue that says 5

followed by another message dialogue that says Please fill in the form correctly

Knowing that i did fill it correctly!

Trace :

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)

Upvotes: 1

Views: 702

Answers (3)

c0der
c0der

Reputation: 18792

addProject_statement.setString(4, budget); has no matching argument in the SQL statement so number of values (and their type) do not match.


For

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

Use

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

Upvotes: 3

Ori Marko
Ori Marko

Reputation: 58774

You statement isn't ordered. there's no budget and departement is before chef.

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

Upvotes: 1

utkarsh31
utkarsh31

Reputation: 1539

I believe you are adding 6 values in the addProject_statement but there are only 5 params in the Insertquery, thats what is causing the issue...

Upvotes: 1

Related Questions