gpt4 book ai didi

java.sql.SQLException : Parameter index out of range (1 > number of parameters, 这是 0) 错误

转载 作者:行者123 更新时间:2023-11-29 11:52:21 30 4
gpt4 key购买 nike

我的项目是驾驶执照数据库。

当用户填写字段时,这是正常的,但是当用户按下“提交”按钮时,一切都变得一团糟。

显示错误参数索引超出范围错误,所以这是我的代码。

private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             

String sql = "INSERT INTO dr_record_table (Driver's ID, Last Name, First Name, Middle Name, Street, Barangay, City/Municipality, Province, Country, Month, Day, Year, Gender) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

try {
pst = conn.prepareStatement(sql);
pst.setString(1, idTxtFld.getText());
pst.setString(2, lnTxtFld.getText());
pst.setString(3, fnTxtFld.getText());
pst.setString(4, mnTxtFld.getText());
pst.setString(5, saTxtFld.getText());
pst.setString(6, baTxtFld.getText());
pst.setString(7, caTxtFld.getText());
pst.setString(8, paTxtFld.getText());
pst.setString(9, cntryTxtFld.getText());
pst.setString(10, String.valueOf(monthCBox.getSelectedItem()));
pst.setString(11, String.valueOf(dayCBox.getSelectedItem()));
pst.setString(12, String.valueOf(yearCBox.getSelectedItem()));
pst.setString(13, getSelectedButtonText(bg));
pst.execute();

JOptionPane.showMessageDialog(null, "Entry Saved!");

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

}

最佳答案

转义驱动程序 ID 中的单引号。比如,

String sql = "INSERT INTO dr_record_table (Driver''s ID, Last Name, "
+ "First Name, Middle Name, Street, Barangay, City/Municipality, "
+ "Province, Country, Month, Day, Year, Gender) VALUES "
+ "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

关于java.sql.SQLException : Parameter index out of range (1 > number of parameters, 这是 0) 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33838458/

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