首页 > 解决方案 > 如何解决此错误:参数索引超出范围

问题描述

我正在注册页面上工作,但我似乎无法解决有关 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);
}

标签: javasqldatabase

解决方案


占位符参数不能有引号。必须是这样的:

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

推荐阅读