java - 如何解决此错误:参数索引超出范围
问题描述
我正在注册页面上工作,但我似乎无法解决有关 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);
}
解决方案
占位符参数不能有引号。必须是这样的:
"INSERT INTO Registration(ID, First Name, Second Name, email, account_type, Post code, Town, Address, Phone number, Organisation, Website, Username, Password, Payment Method) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
推荐阅读
- ansible - 使用 Ansible 的文件模块时如何防止 ToCToU 问题?
- django - Django QuerySet 用于通过多对多相关的对象
- python - Python Pyside2 暂停脚本,直到用户从滑块或 qlineedit 返回值
- android - Getting "Conflicting overloads" for every function in one Kotlin file whenever I change it
- apache - How to combine a http->https redirect with a 404 redirect to /
- java - How to replace the properties of the fragment
- java - Gradle build finished with 662 warnings(s) in 37s 879ms
- android - Can you extract the www folder from a cordova app downloaded from the app store
- .net - Entity Framework Core 2.0 选择多对多问题的查询
- linux - 如何通过 ssh 进入远程服务器、运行命令并留在服务器上