Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java应用程序中的索引1超出范围错误_Java_Sql_Jdbc - Fatal编程技术网

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

java应用程序中的索引1超出范围错误,java,sql,jdbc,Java,Sql,Jdbc,当我运行此代码时: 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,Fam

当我运行此代码时:

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();
我得到这个错误:

索引1超出范围


但是我不知道如何修复它。

您的查询没有占位符。将其更改为:

 String s = "insert into PersonalInfo (Name, FamilyName, FatherName, "
    + "BirthDate, GenderID, NationalId) values (?, ?, ?, ?, ?, ?)";
这里还有一个bug:

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

也许
s
的定义应该是

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

欢迎来到SO。您提问的方式似乎暗示您对调试等基本编程活动一无所知。你应该在询问之前调查一下。另外请阅读:对不起,我会改进的