java.sql.SQLException:参数索引超出范围(1 > 参数数量,即 0)。
if(agreementCbx.isSelected() == false){
JOptionPane.showMessageDialog(null,
"Klik Centang CheckBox Untuk Simpan Data", "Error",
JOptionPane.ERROR_MESSAGE);
}else {
String typeHouse;
if (t36RadioButton.isSelected()) {
typeHouse = "TIPE 36";
}else if (t45RadioButton.isSelected()){
typeHouse = "TIPE 45";
}else {
typeHouse = "TIPE 54";
}
try{
String sql = "INSERT INTO rumah (nama1, area2, tipe3, luas4, harga5, jumlah_cicilan6, cicilan_bulan7) VALUES ('"
+ orderNameTxt.getText() + "''"
+ areacb.getSelectedItem() + "''"
+ typeHouse + ","
+ largeLandTxt.getText()+"','"
+ totalPriceTxt.getText() + "','"
+ instalmentAmountTxt.getText() + "','"
+ instalmentMonthTxt.getText() + "')'";
Connection conn = (Connection)ConnectionDB.connectDatabase();
PreparedStatement pst = conn.prepareStatement(sql);
pst.setString(1,orderNameTxt.getText());
pst.setString(2, (String) areacb.getSelectedItem());
pst.setString(3,typeHouse());
pst.setString(4, largeLandTxt.getText());
pst.setString(5, totalPriceTxt.getText());
pst.setString(6, instalmentAmountTxt.getText());
pst.setString(7, instalmentMonthTxt.getText());
pst.execute();
} catch (SQLException ex) {
Logger.getLogger(PaymentForm.class.getName()).log(Level. SEVERE, null, ex);
}
OptionMenu optionMenu = new OptionMenu();
optionMenu.setVisible(true);
this.getDesktopPane().add(optionMenu);
this.dispose();
}
}
好的,所以你似乎有一个代码的混搭,它通过 string-bashing 创建 SQL 字符串(不好!)并使用
PreparedStatement
(好......如果你做得正确的话)。实际上,您创建的语句没有任何 ?
占位符,然后尝试为它们提供参数值。
我强烈建议您阅读这个 Java 教程,了解如何使用 JDBC 准备好的语句。重要的是,当您使用
PreparedStatement
时,您要了解自己在做什么,以及 为什么这样做。如果您不理解您在做什么,您可能会编写不安全代码。
修复方法是将 SQL 更改为如下所示:
"INSERT INTO rumah (nama1, area2, tipe3, luas4, harga5, " +
"jumlah_cicilan6, cicilan_bulan7) VALUES (?, ?, ?, ?, ?, ?, ?)"
然后确保使用下面的
setString
调用注入每个参数的正确值。
并阅读教程;见上文。