gpt4 book ai didi

mysql - 无法将数据插入我的数据库

转载 作者:行者123 更新时间:2023-11-29 22:09:40 26 4
gpt4 key购买 nike

我是 Struts2 的新手。我创建了一些类文件来将数据插入数据库(MySQL)。当我插入数据时出现错误。错误是

java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).

我无法理解它是如何发生以及如何解决它。

RegistrationService.java

 PreparedStatement p = con.prepareStatement(sql);   
sql = "insert into registration(first_name,last_name) value(?,?)";
p.setString(1,registration.first_name);
p.setString(2,registration.last_name);

注册.java

    public class Registration extends ActionSupport{
String first_name;
String last_name;
//getter and setter

RegistrationAction.java

public String execute() throws ClassNotFoundException, SQLException{
RegistrationService registrationservice= new RegistrationService();
if(registrationservice.filledRegistration(registration)){
return "registered";
}
return "failed";

最佳答案

我认为你错过了这一行:

PreparedStatement p = con.prepareStatement(sql);

sql = "insert into registration(first_name,last_name) value(?,?)";

要更正它,请参阅下文:

sql = "insert into registration(first_name,last_name) value(?,?)";

PreparedStatement p = con.prepareStatement(sql);

关于mysql - 无法将数据插入我的数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31801731/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com