java - Creating a Student passing it to SQL Object and Getting an Exception in thread "AWT-EventQueue-0" lang.ArrayIndexOutOfBoundsException: 4 -


hi guys i'm having trouble w/ java codes. i'm newbie when comes java , love java. of i'm developing desktop applications in java using netbeans , i'm encountering runtime error. can me code. seems 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 tested student object complete entries still getting issues. suspect in preparedstatement, can point exact code having. tnx , 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 = '?' 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(); 

you have drop ' around ? in query, otherwise not counted parameters actual strings of 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 = ? student_id =" + student_id; 

the error happens because have 4 non-quoted question marks, meaning 4 parameters, meaning calls set parameter 0 3 succeed, setting 1 @ index 4 out of bounds.

btw why not using parameter where condition value well?


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -