gpt4 book ai didi

java - executeUpdate() 抛出 ArrayIndexOutOfBoundsException

转载 作者:行者123 更新时间:2023-12-01 17:56:22 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com