Error: Parameter index out of range (39 > number of parameters, which is 38)
Error: Parameter index out of range (39 > number of parameters, which is 38)
我在动态 java 项目中遇到此错误。
java.sql.SQLException:参数索引超出范围(39 > 参数个数,即38)。
我检查了数据库列数是 38,“?”字符数为 38,字段数为 38,myStmt.set 数为 38,但我仍然收到此错误。我正在尝试在 mysql 中写 3 个表,我想我的方法是错误的,你能帮帮我吗?
try {
myConn = getConnection();
String sql = "insert into hodo (find_registry, title, name, surname, first_surname, start_date, structure_start_date, termination_date, mission_code, mission, "
+ "profession_code, account_code, group_code, faculty_institute_hospital, department_unit, work_status, sgk_status, sgk_document_type, sgk_law, sgk_no, "
+ "detail_code, tc_no, father_name, mother_name, nationality, birth_place, birth_date, gender, blood_group, city, "
+ "county, district, marital_status, address, gsm_no, email, agi_information, lesson_load)"
+ " values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
myStmt = myConn.prepareStatement(sql);
myStmt.setString(1, thePerson.getFind_registry());
myStmt.setString(2, thePerson.getTitle());
myStmt.setString(3, thePerson.getName());
myStmt.setString(4, thePerson.getSurname());
myStmt.setString(5, thePerson.getFirst_surname());
myStmt.setDate(6, new java.sql.Date(thePerson.getStart_date().getTime()));
myStmt.setDate(7, new java.sql.Date(thePerson.getStructure_start_date().getTime()));
myStmt.setDate(8, new java.sql.Date(thePerson.getTermination_date().getTime()));
myStmt.setString(9, thePerson.getMission_code());
myStmt.setString(10, thePerson.getMission());
myStmt.setString(11, thePerson.getProfession_code());
myStmt.setString(12, thePerson.getAccount_code());
myStmt.setString(13, thePerson.getGroup_code());
myStmt.setString(14, thePerson.getFaculty_institute_hospital());
myStmt.setString(15, thePerson.getDepartment_unit());
myStmt.setString(16, thePerson.getWork_status());
myStmt.setString(17, thePerson.getSgk_status());
myStmt.setString(18, thePerson.getSgk_document_type());
myStmt.setString(19, thePerson.getSgk_law());
myStmt.setString(20, thePerson.getSgk_no());
myStmt.setString(21, thePerson.getDetail_code());
myStmt.setString(22, thePerson.getTc_no());
myStmt.setString(23, thePerson.getFather_name());
myStmt.setString(24, thePerson.getMother_name());
myStmt.setString(25, thePerson.getNationality());
myStmt.setString(26, thePerson.getBirth_place());
myStmt.setDate(27, new java.sql.Date(thePerson.getBirth_date().getTime()));
myStmt.setString(28, thePerson.getGender());
myStmt.setString(29, thePerson.getBlood_group());
myStmt.setString(30, thePerson.getCity());
myStmt.setString(31, thePerson.getCounty());
myStmt.setString(32, thePerson.getDistrict());
myStmt.setString(33, thePerson.getMarital_status());
myStmt.setString(34, thePerson.getAddress());
myStmt.setString(35, thePerson.getGsm_no());
myStmt.setString(36, thePerson.getEmail());
myStmt.setString(37, thePerson.getAgi_information());
myStmt.setString(38, thePerson.getLesson_load());
myStmt.execute();
myConn2 = getConnection();
String sql2 = "insert into modo (name, surname, tc_no, person_type, nationality, birth_date, structure_start_date, termination_date, father_name, mother_name, "
+ "marital_status, blood_group, birth_place, city, county, district, registry_place, first_surname, registry_no, work_status, work_manner, title, "
+ " employment_situation, mission_code, profession_code, payroll_expense_center, wage_account_code, wage_type, mandatory_lesson_load, work_period_start_date, "
+ "start_date, agi, mandatory_bes_rate, health_insurance_status, document_type, bank_account_no, bank_no, branch_no, bounded_place, staff_status, phone, email, "
+ "address_type, post_code, county_name, address_detail, title_given_corporation, readership_field, readership_date, upsize_designation_date, "
+ "upsize_designation_status, mission_name, mission_place, is_noble, mission_status, ssk_registry_no, position, attending_insurance_branch, "
+ "item30_working_type, fraction_defective, closeness_degree, gender, is_agi)"
+ "values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
myStmt2 = myConn2.prepareStatement(sql);
myStmt2.setString(1, thePerson.getName());
myStmt2.setString(2, thePerson.getSurname());
myStmt2.setString(3, thePerson.getTc_no());
myStmt2.setString(4, thePerson.getPerson_type());
myStmt2.setString(5, thePerson.getNationality());
myStmt2.setDate(6, new java.sql.Date(thePerson.getBirth_date().getTime()));
myStmt2.setDate(7, new java.sql.Date(thePerson.getStructure_start_date().getTime()));
myStmt2.setDate(8, new java.sql.Date(thePerson.getTermination_date().getTime()));
myStmt2.setString(9, thePerson.getFather_name());
myStmt2.setString(10, thePerson.getMother_name());
myStmt2.setString(11, thePerson.getMarital_status());
myStmt2.setString(12, thePerson.getBlood_group());
myStmt2.setString(13, thePerson.getBirth_place());
myStmt2.setString(14, thePerson.getCity());
myStmt2.setString(15, thePerson.getCounty());
myStmt2.setString(16, thePerson.getDistrict());
myStmt2.setString(17, thePerson.getRegistry_place());
myStmt2.setString(18, thePerson.getFirst_surname());
myStmt2.setString(19, thePerson.getRegistry_no());
myStmt2.setString(20, thePerson.getWork_status());
myStmt2.setString(21, thePerson.getWork_manner());
myStmt2.setString(22, thePerson.getTitle());
myStmt2.setString(23, thePerson.getEmployment_situation());
myStmt2.setString(24, thePerson.getMission_code());
myStmt2.setString(25, thePerson.getProfession_code());
myStmt2.setString(26, thePerson.getPayroll_expense_center());
myStmt2.setString(27, thePerson.getWage_account_code());
myStmt2.setString(28, thePerson.getWage_type());
myStmt2.setString(29, thePerson.getMandatory_lesson_load());
myStmt2.setDate(30, new java.sql.Date(thePerson.getWork_period_start_date().getTime()));
myStmt2.setDate(31, new java.sql.Date(thePerson.getStart_date().getTime()));
myStmt2.setString(32, thePerson.getAgi());
myStmt2.setString(33, thePerson.getMandatory_bes_rate());
myStmt2.setString(34, thePerson.getHealth_insurance_status());
myStmt2.setString(35, thePerson.getDocument_type());
myStmt2.setString(36, thePerson.getBank_account_no());
myStmt2.setString(37, thePerson.getBank_no());
myStmt2.setString(38, thePerson.getBranch_no());
myStmt2.setString(39, thePerson.getBounded_place());
myStmt2.setString(40, thePerson.getStaff_status());
myStmt2.setString(41, thePerson.getPhone());
myStmt2.setString(42, thePerson.getEmail());
myStmt2.setString(43, thePerson.getAddress_type());
myStmt2.setString(44, thePerson.getPost_code());
myStmt2.setString(45, thePerson.getCounty_name());
myStmt2.setString(46, thePerson.getAddress_detail());
myStmt2.setString(47, thePerson.getTitle_given_corporation());
myStmt2.setString(48, thePerson.getReadership_field());
myStmt2.setDate(49, new java.sql.Date(thePerson.getReadership_date().getTime()));
myStmt2.setDate(50, new java.sql.Date(thePerson.getUpsize_designation_date().getTime()));
myStmt2.setString(51, thePerson.getUpsize_designation_status());
myStmt2.setString(52, thePerson.getMission_name());
myStmt2.setString(53, thePerson.getMission_place());
myStmt2.setString(54, thePerson.getIs_noble());
myStmt2.setString(55, thePerson.getMission_status());
myStmt2.setString(56, thePerson.getSsk_registry_no());
myStmt2.setString(57, thePerson.getPosition());
myStmt2.setString(58, thePerson.getAttending_insurance_branch());
myStmt2.setString(59, thePerson.getItem30_working_type());
myStmt2.setString(60, thePerson.getFraction_defective());
myStmt2.setString(61, thePerson.getCloseness_degree());
myStmt2.setString(62, thePerson.getGender());
myStmt2.setString(63, thePerson.getIs_agi());
myStmt2.execute();
myConn3 = getConnection();
String sql3 = "insert into kodo (name, surname, nationality, person_type, sgk_type, title, gender, marital_status, blood_group, trainer_type, registry_no, "
+ "corporation, sub_unit, department, working_type, continuation_type, start_date, end_date, active, mernis_no, country_code, city_code, county, district, birth_place, "
+ "birth_date, father_name, mother_name) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
myStmt3 = myConn3.prepareStatement(sql);
myStmt3.setString(1, thePerson.getName());
myStmt3.setString(2, thePerson.getSurname());
myStmt3.setString(3, thePerson.getNationality());
myStmt3.setString(4, thePerson.getPerson_type());
myStmt3.setString(5, thePerson.getSgk_type());
myStmt3.setString(6, thePerson.getTitle());
myStmt3.setString(7, thePerson.getGender());
myStmt3.setString(8, thePerson.getMarital_status());
myStmt3.setString(9, thePerson.getBlood_group());
myStmt3.setString(10, thePerson.getTrainer_type());
myStmt3.setString(11, thePerson.getRegistry_no());
myStmt3.setString(12, thePerson.getCorporation());
myStmt3.setString(13, thePerson.getSub_unit());
myStmt3.setString(14, thePerson.getDepartment());
myStmt3.setString(15, thePerson.getWorking_type());
myStmt3.setString(16, thePerson.getContinuation_type());
myStmt3.setDate(17, new java.sql.Date(thePerson.getStart_date().getTime()));
myStmt3.setDate(18, new java.sql.Date(thePerson.getEnd_date().getTime()));
myStmt3.setString(19, thePerson.getActive());
myStmt3.setString(20, thePerson.getMernis_no());
myStmt3.setString(21, thePerson.getCountry_code());
myStmt3.setString(22, thePerson.getCity_code());
myStmt3.setString(23, thePerson.getCounty());
myStmt3.setString(24, thePerson.getDistrict());
myStmt3.setString(25, thePerson.getBirth_place());
myStmt3.setDate(26, new java.sql.Date(thePerson.getBirth_date().getTime()));
myStmt3.setString(27, thePerson.getFather_name());
myStmt3.setString(28, thePerson.getMother_name());
myStmt3.execute();
}
finally {
close (myConn, myStmt);
close (myConn2, myStmt2);
close (myConn3, myStmt3);
}
您的代码包含行
myStmt2 = myConn2.prepareStatement(sql);
我想你想写
myStmt2 = myConn2.prepareStatement(sql2); // note: sql2 not sql
否则您将再次准备第一个 SQL 语句并将参数设置到其中,就好像它是第二个一样。
同样的事情发生在
myStmt3 = myConn3.prepareStatement(sql);
你想要 sql3
而不是这里的 sql
。
尝试将每个 SQL 操作提取到单独的方法中。一方面,这会减少您的方法的大小。另一方面,您会很快发现错误:第一个语句的 SQL 字符串在第二个方法中超出范围。另外,我不确定你在用什么编写代码:任何值得称道的 IDE 都会警告你你的变量 sql2
和 sql3
未被使用。
我在动态 java 项目中遇到此错误。
java.sql.SQLException:参数索引超出范围(39 > 参数个数,即38)。
我检查了数据库列数是 38,“?”字符数为 38,字段数为 38,myStmt.set 数为 38,但我仍然收到此错误。我正在尝试在 mysql 中写 3 个表,我想我的方法是错误的,你能帮帮我吗?
try {
myConn = getConnection();
String sql = "insert into hodo (find_registry, title, name, surname, first_surname, start_date, structure_start_date, termination_date, mission_code, mission, "
+ "profession_code, account_code, group_code, faculty_institute_hospital, department_unit, work_status, sgk_status, sgk_document_type, sgk_law, sgk_no, "
+ "detail_code, tc_no, father_name, mother_name, nationality, birth_place, birth_date, gender, blood_group, city, "
+ "county, district, marital_status, address, gsm_no, email, agi_information, lesson_load)"
+ " values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
myStmt = myConn.prepareStatement(sql);
myStmt.setString(1, thePerson.getFind_registry());
myStmt.setString(2, thePerson.getTitle());
myStmt.setString(3, thePerson.getName());
myStmt.setString(4, thePerson.getSurname());
myStmt.setString(5, thePerson.getFirst_surname());
myStmt.setDate(6, new java.sql.Date(thePerson.getStart_date().getTime()));
myStmt.setDate(7, new java.sql.Date(thePerson.getStructure_start_date().getTime()));
myStmt.setDate(8, new java.sql.Date(thePerson.getTermination_date().getTime()));
myStmt.setString(9, thePerson.getMission_code());
myStmt.setString(10, thePerson.getMission());
myStmt.setString(11, thePerson.getProfession_code());
myStmt.setString(12, thePerson.getAccount_code());
myStmt.setString(13, thePerson.getGroup_code());
myStmt.setString(14, thePerson.getFaculty_institute_hospital());
myStmt.setString(15, thePerson.getDepartment_unit());
myStmt.setString(16, thePerson.getWork_status());
myStmt.setString(17, thePerson.getSgk_status());
myStmt.setString(18, thePerson.getSgk_document_type());
myStmt.setString(19, thePerson.getSgk_law());
myStmt.setString(20, thePerson.getSgk_no());
myStmt.setString(21, thePerson.getDetail_code());
myStmt.setString(22, thePerson.getTc_no());
myStmt.setString(23, thePerson.getFather_name());
myStmt.setString(24, thePerson.getMother_name());
myStmt.setString(25, thePerson.getNationality());
myStmt.setString(26, thePerson.getBirth_place());
myStmt.setDate(27, new java.sql.Date(thePerson.getBirth_date().getTime()));
myStmt.setString(28, thePerson.getGender());
myStmt.setString(29, thePerson.getBlood_group());
myStmt.setString(30, thePerson.getCity());
myStmt.setString(31, thePerson.getCounty());
myStmt.setString(32, thePerson.getDistrict());
myStmt.setString(33, thePerson.getMarital_status());
myStmt.setString(34, thePerson.getAddress());
myStmt.setString(35, thePerson.getGsm_no());
myStmt.setString(36, thePerson.getEmail());
myStmt.setString(37, thePerson.getAgi_information());
myStmt.setString(38, thePerson.getLesson_load());
myStmt.execute();
myConn2 = getConnection();
String sql2 = "insert into modo (name, surname, tc_no, person_type, nationality, birth_date, structure_start_date, termination_date, father_name, mother_name, "
+ "marital_status, blood_group, birth_place, city, county, district, registry_place, first_surname, registry_no, work_status, work_manner, title, "
+ " employment_situation, mission_code, profession_code, payroll_expense_center, wage_account_code, wage_type, mandatory_lesson_load, work_period_start_date, "
+ "start_date, agi, mandatory_bes_rate, health_insurance_status, document_type, bank_account_no, bank_no, branch_no, bounded_place, staff_status, phone, email, "
+ "address_type, post_code, county_name, address_detail, title_given_corporation, readership_field, readership_date, upsize_designation_date, "
+ "upsize_designation_status, mission_name, mission_place, is_noble, mission_status, ssk_registry_no, position, attending_insurance_branch, "
+ "item30_working_type, fraction_defective, closeness_degree, gender, is_agi)"
+ "values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
myStmt2 = myConn2.prepareStatement(sql);
myStmt2.setString(1, thePerson.getName());
myStmt2.setString(2, thePerson.getSurname());
myStmt2.setString(3, thePerson.getTc_no());
myStmt2.setString(4, thePerson.getPerson_type());
myStmt2.setString(5, thePerson.getNationality());
myStmt2.setDate(6, new java.sql.Date(thePerson.getBirth_date().getTime()));
myStmt2.setDate(7, new java.sql.Date(thePerson.getStructure_start_date().getTime()));
myStmt2.setDate(8, new java.sql.Date(thePerson.getTermination_date().getTime()));
myStmt2.setString(9, thePerson.getFather_name());
myStmt2.setString(10, thePerson.getMother_name());
myStmt2.setString(11, thePerson.getMarital_status());
myStmt2.setString(12, thePerson.getBlood_group());
myStmt2.setString(13, thePerson.getBirth_place());
myStmt2.setString(14, thePerson.getCity());
myStmt2.setString(15, thePerson.getCounty());
myStmt2.setString(16, thePerson.getDistrict());
myStmt2.setString(17, thePerson.getRegistry_place());
myStmt2.setString(18, thePerson.getFirst_surname());
myStmt2.setString(19, thePerson.getRegistry_no());
myStmt2.setString(20, thePerson.getWork_status());
myStmt2.setString(21, thePerson.getWork_manner());
myStmt2.setString(22, thePerson.getTitle());
myStmt2.setString(23, thePerson.getEmployment_situation());
myStmt2.setString(24, thePerson.getMission_code());
myStmt2.setString(25, thePerson.getProfession_code());
myStmt2.setString(26, thePerson.getPayroll_expense_center());
myStmt2.setString(27, thePerson.getWage_account_code());
myStmt2.setString(28, thePerson.getWage_type());
myStmt2.setString(29, thePerson.getMandatory_lesson_load());
myStmt2.setDate(30, new java.sql.Date(thePerson.getWork_period_start_date().getTime()));
myStmt2.setDate(31, new java.sql.Date(thePerson.getStart_date().getTime()));
myStmt2.setString(32, thePerson.getAgi());
myStmt2.setString(33, thePerson.getMandatory_bes_rate());
myStmt2.setString(34, thePerson.getHealth_insurance_status());
myStmt2.setString(35, thePerson.getDocument_type());
myStmt2.setString(36, thePerson.getBank_account_no());
myStmt2.setString(37, thePerson.getBank_no());
myStmt2.setString(38, thePerson.getBranch_no());
myStmt2.setString(39, thePerson.getBounded_place());
myStmt2.setString(40, thePerson.getStaff_status());
myStmt2.setString(41, thePerson.getPhone());
myStmt2.setString(42, thePerson.getEmail());
myStmt2.setString(43, thePerson.getAddress_type());
myStmt2.setString(44, thePerson.getPost_code());
myStmt2.setString(45, thePerson.getCounty_name());
myStmt2.setString(46, thePerson.getAddress_detail());
myStmt2.setString(47, thePerson.getTitle_given_corporation());
myStmt2.setString(48, thePerson.getReadership_field());
myStmt2.setDate(49, new java.sql.Date(thePerson.getReadership_date().getTime()));
myStmt2.setDate(50, new java.sql.Date(thePerson.getUpsize_designation_date().getTime()));
myStmt2.setString(51, thePerson.getUpsize_designation_status());
myStmt2.setString(52, thePerson.getMission_name());
myStmt2.setString(53, thePerson.getMission_place());
myStmt2.setString(54, thePerson.getIs_noble());
myStmt2.setString(55, thePerson.getMission_status());
myStmt2.setString(56, thePerson.getSsk_registry_no());
myStmt2.setString(57, thePerson.getPosition());
myStmt2.setString(58, thePerson.getAttending_insurance_branch());
myStmt2.setString(59, thePerson.getItem30_working_type());
myStmt2.setString(60, thePerson.getFraction_defective());
myStmt2.setString(61, thePerson.getCloseness_degree());
myStmt2.setString(62, thePerson.getGender());
myStmt2.setString(63, thePerson.getIs_agi());
myStmt2.execute();
myConn3 = getConnection();
String sql3 = "insert into kodo (name, surname, nationality, person_type, sgk_type, title, gender, marital_status, blood_group, trainer_type, registry_no, "
+ "corporation, sub_unit, department, working_type, continuation_type, start_date, end_date, active, mernis_no, country_code, city_code, county, district, birth_place, "
+ "birth_date, father_name, mother_name) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
myStmt3 = myConn3.prepareStatement(sql);
myStmt3.setString(1, thePerson.getName());
myStmt3.setString(2, thePerson.getSurname());
myStmt3.setString(3, thePerson.getNationality());
myStmt3.setString(4, thePerson.getPerson_type());
myStmt3.setString(5, thePerson.getSgk_type());
myStmt3.setString(6, thePerson.getTitle());
myStmt3.setString(7, thePerson.getGender());
myStmt3.setString(8, thePerson.getMarital_status());
myStmt3.setString(9, thePerson.getBlood_group());
myStmt3.setString(10, thePerson.getTrainer_type());
myStmt3.setString(11, thePerson.getRegistry_no());
myStmt3.setString(12, thePerson.getCorporation());
myStmt3.setString(13, thePerson.getSub_unit());
myStmt3.setString(14, thePerson.getDepartment());
myStmt3.setString(15, thePerson.getWorking_type());
myStmt3.setString(16, thePerson.getContinuation_type());
myStmt3.setDate(17, new java.sql.Date(thePerson.getStart_date().getTime()));
myStmt3.setDate(18, new java.sql.Date(thePerson.getEnd_date().getTime()));
myStmt3.setString(19, thePerson.getActive());
myStmt3.setString(20, thePerson.getMernis_no());
myStmt3.setString(21, thePerson.getCountry_code());
myStmt3.setString(22, thePerson.getCity_code());
myStmt3.setString(23, thePerson.getCounty());
myStmt3.setString(24, thePerson.getDistrict());
myStmt3.setString(25, thePerson.getBirth_place());
myStmt3.setDate(26, new java.sql.Date(thePerson.getBirth_date().getTime()));
myStmt3.setString(27, thePerson.getFather_name());
myStmt3.setString(28, thePerson.getMother_name());
myStmt3.execute();
}
finally {
close (myConn, myStmt);
close (myConn2, myStmt2);
close (myConn3, myStmt3);
}
您的代码包含行
myStmt2 = myConn2.prepareStatement(sql);
我想你想写
myStmt2 = myConn2.prepareStatement(sql2); // note: sql2 not sql
否则您将再次准备第一个 SQL 语句并将参数设置到其中,就好像它是第二个一样。
同样的事情发生在
myStmt3 = myConn3.prepareStatement(sql);
你想要 sql3
而不是这里的 sql
。
尝试将每个 SQL 操作提取到单独的方法中。一方面,这会减少您的方法的大小。另一方面,您会很快发现错误:第一个语句的 SQL 字符串在第二个方法中超出范围。另外,我不确定你在用什么编写代码:任何值得称道的 IDE 都会警告你你的变量 sql2
和 sql3
未被使用。