我的项目是一个驾驶执照数据库。
当用户反复填写字段时,这是正常的,但当用户按下"提交"按钮时,一切都一团糟。
显示了The error Parameter index out of range
错误,所以这是我的代码。
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);
}
}
转义Driver's 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 "
+ "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";