索引1在java应用程序中超出范围错误

时间:2021-09-02 16:08:48

When I run this code:

当我运行此代码时:

String s = "insert into PersonalInfo(Name, FamilyName, FatherName, BirthDate, GenderID, NationalId) values";

PreparedStatement pst = con.prepareStatement(s);
pst.setString(1,Name.getText());    // error is at this line!
pst.setString(2,FamilyName.getText());
pst.setString(3,FatherName.getText());
pst.setString(4,BirthDate.getText());
if (Male.isSelected()){
    GenderID=1;
} 
if (Female.isSelected()){
    GenderID=2;
}
pst.setInt(GenderID,5);
pst.setString(6,NationalId.getText());
pst.executeUpdate();

I get this error:

我收到此错误:

The index 1 is out of range

指数1超出范围

But I have no idea how to fix it.

但我不知道如何解决它。

2 个解决方案

#1


0  

Your query has no placeholders. Change it to this:

您的查询没有占位符。把它改成这个:

 String s = "insert into PersonalInfo (Name, FamilyName, FatherName, "
    + "BirthDate, GenderID, NationalId) values (?, ?, ?, ?, ?, ?)";

You also have a bug here:

你这里也有一个错误:

pst.setInt(GenderID, 5);  // your code
pst.setInt(5, GenderID);  // correct code

#2


0  

Perhaps definition of s should be

也许s的定义应该是

String s= "insert into PersonalInfo(Name, FamilyName, FatherName, BirthDate, GenderID, NationalId) values (?,?,?,?,?,?)" 

#1


0  

Your query has no placeholders. Change it to this:

您的查询没有占位符。把它改成这个:

 String s = "insert into PersonalInfo (Name, FamilyName, FatherName, "
    + "BirthDate, GenderID, NationalId) values (?, ?, ?, ?, ?, ?)";

You also have a bug here:

你这里也有一个错误:

pst.setInt(GenderID, 5);  // your code
pst.setInt(5, GenderID);  // correct code

#2


0  

Perhaps definition of s should be

也许s的定义应该是

String s= "insert into PersonalInfo(Name, FamilyName, FatherName, BirthDate, GenderID, NationalId) values (?,?,?,?,?,?)" 

相关文章