Reputation: 5
i am getting this error in dynamic java project.
java.sql.SQLException: Parameter index out of range (39 > number of parameters, which is 38).
I checked db column count is 38, "?" charachter count is 38, field count is 38, myStmt.set count is 38 but still i am getting this error. I am trying to write 3 tables in mysql, i suppose my method is wrong, can you help me, please?
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);
}
Upvotes: 0
Views: 414
Reputation: 65054
Your code contains the line
myStmt2 = myConn2.prepareStatement(sql);
I think you wanted to write
myStmt2 = myConn2.prepareStatement(sql2); // note: sql2 not sql
Otherwise you are preparing the first SQL statement again and setting parameters into it as if it were the second.
The same thing happens with
myStmt3 = myConn3.prepareStatement(sql);
You want sql3
instead of sql
here.
Try extracting each SQL operation into separate methods. For one thing, that would reduce the size of your method. For another, you'd quickly find the error: the SQL string for the first statement would be out of scope in the second method. Also, I'm not sure what you're writing your code in: any IDE worth its salt would warn you that your variables sql2
and sql3
are unused.
Upvotes: 1