java - SQLException: Missing IN or OUT parameter at index:: 1 -
i've made jtextfield, jradiobuttons, , text areas. want them insert data entered in these sql database. i've made following code:
connection conn = null; //statement stm = null; preparedstatement pst = null; resultset rs = null; try{ class.forname("oracle.jdbc.oracledriver"); system.out.println("driver loaded."); conn = drivermanager.getconnection("jdbc:oracle:thin:@localhost:1521:orcl", "hr", "hr"); system.out.println("driver connected."); pst=conn.preparestatement("insert directorsdb (director_name, dob, din, pan, uin, birth_place, passport, driving_lic, nationality, occupation, occupation_company, designation, qualification, address_off, address_resi, contact_home, contact_work, contact_mobile, email_personal, email_work, email_other, secretary_name, secretary_contact, secretary_email, dsc, dsc_expiry, father_name, mother_name, appointment_date, position_board, cessation_date, cessation_reason, promoter, independent_director, about)" + "values("+jformattedtextfield1.gettext()+"','"+jdatechooser1.getdate()+"','"+din.gettext()+"','"+pan.gettext()+"','"+uin.gettext()+"','"+jformattedtextfield4.gettext()+"','"+passportno.gettext()+"','"+dlicense.gettext()+"','"+jformattedtextfield5.gettext()+"','"+jformattedtextfield6.gettext()+"','"+jformattedtextfield7.gettext()+"','"+jformattedtextfield8.gettext()+"','"+jformattedtextfield9.gettext()+"','"+officeadd.gettext()+"','"+residentialadd.gettext()+"','"+jformattedtextfield12.gettext()+"','"+jformattedtextfield13.gettext()+"','"+jformattedtextfield14.gettext()+"','"+jformattedtextfield15.gettext()+"','"+jformattedtextfield16.gettext()+"','"+jformattedtextfield17.gettext()+"','"+jformattedtextfield18.gettext()+"','"+jformattedtextfield19.gettext()+"','"+jformattedtextfield20.gettext()+"','"+buttongroup1.getselection().getactioncommand()+"','"+jdatechooser2.getdate()+"','"+jformattedtextfield22.gettext()+"','"+jformattedtextfield23.gettext()+"','"+jdatechooser3.getdate()+"','"+jformattedtextfield25.gettext()+"','"+jdatechooser4.getdate()+"','"+jtextarea2.gettext()+"','"+buttongroup2.getselection().getactioncommand()+"','"+buttongroup3.getselection().getactioncommand()+"','"+jtextarea1.gettext()+"',')"); pst.execute(); if(pst.execute()==true){ joptionpane.showmessagedialog( null, "saved successfully!!"); }else{ joptionpane.showmessagedialog( null, "saved successfully!!"); } }catch(sqlexception e){ system.err.println(e.fillinstacktrace()); } catch(classnotfoundexception ex){ system.err.println(ex); }
the exception i'm getting is:
sqlexception: missing in or out parameter @ index:: 1
help me in rectifying same.
you have more values provided column count in sql.
when using preparestatement, please use place holder in sql avoid sql injection.
string selectsql = "select user_id, username dbuser user_id = ? , name = ?"; preparedstatement preparedstatement = dbconnection.preparestatement(selectsql); preparedstatement.setint(1, 1001); preparedstatement.setstring(2, "test"); resultset rs = preparedstatement.executequery(selectsql);
when update:
string updatetablesql = "update dbuser set username = ? user_id = ?"; preparedstatement preparedstatement = dbconnection.preparestatement(updatetablesql); preparedstatement.setstring(1, "value"); preparedstatement.setint(2, 1001); preparedstatement .executeupdate();
Comments
Post a Comment