gpt4 book ai didi

java - 错误: Index parameter out of range

转载 作者:行者123 更新时间:2023-11-30 00:13:49 26 4
gpt4 key购买 nike

我想插入组合框所选项目的主键

但我收到此错误参数索引超出范围(12 > 参数数量为 0)”。

我真的不知道如何解决这个问题。

这是我的代码:

String valueTrainer = "kosong";
try
{
String sql2 = "Insert into ahli (MemberID, TrainerID, Name, ICNumber, Address, Nationality,"
+ "PhoneNumber, Email, EmergencyPerson, EmergencyContact, DateReg, MemberTypeID) "
+ "values(?,?,?,?,?,?,?,?,?,?,?,?)";


pst = conn.prepareStatement(sql2);

pst.setString(1, MemberIDTextField.getText());
pst.setString(2, valueTrainer);
pst.setString(3, NameTextField.getText());
pst.setString(4, jTextField1.getText());
pst.setString(5, AddressTextArea.getText());

//Nationality combo box
String nationalityList = NationalityComboBox.getSelectedItem().toString();
pst.setString(6, nationalityList);

pst.setString(7, PhoneNumberTextField.getText());
pst.setString(8, EmailTextField.getText());
pst.setString(9, EmerContactPersonTextField.getText());
pst.setString(10, EmerContactNumberTextField.getText());

//Date Chooser
pst.setString(11, ((JTextField)MemberDateChooser.getDateEditor().getUiComponent()).getText());

//membertype combobox
// problem start from here, I think..
String memberTypeList = MemberTypeComboBox.getSelectedItem().toString();
String sql1 ="Select MemberTypeID from jeniskeahlian where Type = '"+memberTypeList+"' " ;
pst = conn.prepareStatement(sql1);

rs = pst.executeQuery();

while(rs.next()){

String memberType = rs.getString("MemberTypeID");
pst.setString(12, memberType);

}

pst.execute();

JOptionPane.showMessageDialog(null, "New member has been added");

} catch (SQLException | HeadlessException e) {
JOptionPane.showMessageDialog(null, e);
}

最佳答案

您没有正确使用 pst

准备好的语句上的相同命名引用不能在不同的 sql 语句上使用,除非正确使用并关闭了该引用。在一条语句上准备、设置、执行、关闭,然后在另一条语句上重复相同的操作。

按如下方式更改您的代码

try
{
String memberTypeList = MemberTypeComboBox.getSelectedItem().toString();
String sql1 ="Select MemberTypeID
from jeniskeahlian
where Type = ? " ;
pst = conn.prepareStatement(sql1);
pst.setString( 1, memberTypeList );

rs = pst.executeQuery();
String memberType = "";
while(rs.next()){
memberType = rs.getString("MemberTypeID");
}
rs.close();
pst.close();

String sql2 = "Insert into ahli (MemberID, TrainerID, Name,
ICNumber, Address, Nationality,
PhoneNumber, Email, EmergencyPerson,
EmergencyContact, DateReg, MemberTypeID)
values(?,?,?,?,?,?,?,?,?,?,?,?)";

pst = conn.prepareStatement(sql2);

pst.setString(1, MemberIDTextField.getText());
pst.setString(2, valueTrainer);
pst.setString(3, NameTextField.getText());
pst.setString(4, jTextField1.getText());
pst.setString(5, AddressTextArea.getText());

//Nationality combo box
String nationalityList = NationalityComboBox.getSelectedItem().toString();
pst.setString(6, nationalityList);

pst.setString(7, PhoneNumberTextField.getText());
pst.setString(8, EmailTextField.getText());
pst.setString(9, EmerContactPersonTextField.getText());
pst.setString(10, EmerContactNumberTextField.getText());

//Date Chooser
pst.setString(11, ((JTextField)MemberDateChooser
.getDateEditor()
.getUiComponent()).getText());
pst.setString(12, memberType);

pst.execute();

JOptionPane.showMessageDialog(null, "New member has been added");

} // try

关于java - 错误: Index parameter out of range,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23825909/

26 4 0