如何解决此错误:参数索引超出范围

时间:2019-05-07 03:08:02

标签: java sql database

我正在注册页面上,我似乎无法解决有关SQL的问题。

错误消息

  

参数索引超出范围(1 <参数数量,为0)

任何帮助将不胜感激

try{
    String query = "INSERT INTO `Registration`(`ID`, `First Name`, `Second Name`, `email`, `account_type`, `Post code`, `Town`, `Address`, `Phone number`, `Organisation`, `Website`, `Username`, `Password`, `Payment Method`) VALUES ('?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?')";

con = DriverManager.getConnection("jdbc:mysql://localhost/globalmusic","root", "");

pst = con.prepareStatement(query);              
pst.setString(1, txtAnumber.getText());              
pst.setString(2, txtFname.getText());               
pst.setString(3, txtLname.getText());               
pst.setString(4, txtEaddress.getText());                
pst.setString(5, cmbAtype.getSelectedItem().toString());               
pst.setString(6, txtPcode.getText());                
pst.setString(7, txtTown.getText());                
pst.setString(8, txtAddress.getText());                
pst.setString(9, txtPnumber.getText());               
pst.setString(10, txtOrg.getText());                
pst.setString(11, txtWaddress.getText());              
pst.setString(12, txtUname.getText());               
pst.setString(13, txtPass.getText());             
pst.setString(14, cmbPmethod.getSelectedItem().toString());          
pst.executeUpdate();

JOptionPane.showMessageDialog(null, "Registration successful");

} catch(Exception ex) {
    JOptionPane.showMessageDialog(null, ex);
}

1 个答案:

答案 0 :(得分:0)

您不能在占位符参数中使用引号。必须是这样的:

"INSERT INTO Registration(ID, First Name, Second Name, email, account_type, Post code, Town, Address, Phone number, Organisation, Website, Username, Password, Payment Method) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"