0

Hi Guys I'm having trouble w/ my Java codes. I'm a newbie when it comes to Java and I love java. as of now I'm developing a desktop applications in java using netbeans and I'm encountering a runtime error. Can anyone help me with this code. what seems to be the problem or issue.

Exception in thread "AWT-EventQueue-0" lang.ArrayIndexOutOfBoundsException: 4

            Student student = new Student();
            student.setFirstName("JOHN");
            student.setlastName("DOE");
            student.setAge(31);
            student.setGender("Male");
            student.setEmailAddress("xyz@xc.com");
            student.setStduent_Id(314);
            student.setLevel("Grade 5");

etc. Assuming Student object has correct values I already tested the student object with complete entries but still getting the issues. My suspect is in the PreParedStatement, can anyone point the exact that my code is having. Tnx and regards

            fname = student.getFirst_name();
            student.getLast_name();
            age = student.getAge();
            student.getGender();
            email_address = student.getEmail_add();
            student_id= student.getStudent_id();
            level=  student.getLevel();
            room = student.getRoom();
            date_birth = student.getDate_birth();
            home_phone_number = student.getHome_phone();
            mobile_number = student.getMobile_phone();
            street = student.getStreet();
            city = student.getCity();
            state_province = student.getState_province();
            zip_postal_codes = student.getZip_postal();
            notes = student.getNotes();


            String SQL = "UPDATE tbl_students SET first_name ='?', last_name = '?', age = 22, gender = '?', email_address='?',student_id = ?, level = '?', room = '?', date_of_birth='?', home_phone_number= ?, mobile_number = ?, street='?', city='?', state_province ='?', zip_postal_code=?, notes = '?' where student_id =" + student_id;



            String fname = student.getFirst_name();
            String  lname = student.getLast_name();
            int age = student.getAge();
            String  gender = student.getGender();
            String email_address = student.getEmail_add();
            int student_id= student.getStudent_id();
            String level=  student.getLevel();
            String room = student.getRoom();

            String date_birth = student.getDate_birth();
            int home_phone_number = student.getHome_phone();

            int mobile_number = student.getMobile_phone();
            String street = student.getStreet();
            String city = student.getCity();
            String state_province = student.getState_province();
            int zip_postal_codes = student.getZip_postal();
            String notes = student.getNotes();

            DBMS.CreateConn();

            Connection conn;
            conn =DBMS.CreateConn();

            PreparedStatement pst = conn.prepareStatement(SQL);

            pst.setString(1, fname);
            pst.setString(2, lname);
            pst.setInt(3, age);
            pst.setString(4, gender);
            pst.setString(5, email_address);
            pst.setInt(6, student_id);
            pst.setString(7,level);
            pst.setString(8, room);
            pst.setString(9, date_birth);
            pst.setInt(10, home_phone_number);
            pst.setInt(11, mobile_number);
            pst.setString(12, street);
            pst.setString(13, city);
            pst.setString(14, state_province);
            pst.setInt(15, zip_postal_codes);
            pst.setString(16, notes);


            int result = pst.executeUpdate();
kornben
  • 45
  • 1
  • 5
  • You should also make the call in a background thread. The exception happens in the eventthread (AWT-EventQueue-0). That's the thread Swing paints in. Never use it for long processing or DB-calls etc. – Just another Java programmer Aug 18 '17 at 09:53

1 Answers1

1

You have to drop all the ' around the ? in the query, otherwise they are not counted as parameters but actual strings of the question mark.

String SQL = "UPDATE tbl_students SET first_name =?, last_name = ?, age = 22, gender = ?, email_address=?,student_id = ?, level = ?, room = ?, date_of_birth=?, home_phone_number= ?, mobile_number = ?, street=?, city=?, state_province =?, zip_postal_code=?, notes = ? where student_id =" + student_id;

The error happens because you currently have exactly 4 non-quoted question marks, meaning four parameters, meaning the calls to set parameter 0 to 3 succeed, but setting the one at index 4 is out of bounds.

Btw why are you not using a parameter for the where condition value as well?

luk2302
  • 55,258
  • 23
  • 97
  • 137
  • Note that technically it is a bug here, because a driver should throw an SQLException in this situation. – Mark Rotteveel Aug 18 '17 at 09:56
  • @MarkRotteveel you mean it should throw an exception when calling `conn.prepareStatement`? – luk2302 Aug 18 '17 at 10:04
  • No, the `setXXX` method should throw an `SQLException` instead of an `ArrayIndexOutOfBoundsException` – Mark Rotteveel Aug 18 '17 at 10:48
  • @MarkRotteveel it should handle it better, I agree. The user of the lib should not have to know / understand the parameters are stored in an array, the bounds should be explicitly checked and handled accordingly. I do not think an `SQLException` would be much better, would prefer an `IllegalArgumentException` with a detailed error message "tried to set parameter 5 while only 4 parameters were specified in the statement" – luk2302 Aug 18 '17 at 13:19
  • It must be an `SQLException` (or one of its subclasses), because that is what the JDBC specification and API requires. See [`PreparedStatement#setString`](http://docs.oracle.com/javase/8/docs/api/java/sql/PreparedStatement.html#setString-int-java.lang.String-): _"SQLException - if parameterIndex does not correspond to a parameter marker in the SQL statement"_ – Mark Rotteveel Aug 18 '17 at 14:50