java - I cannot insert value in the table using textfield -
i need create program in values can inserted using textfield in java.
database getting connected ...
bt cannot insert values using jtextfield ....need help..
shows error ..
java.sql.sqlexception: parameter index out of range (1 > number of parameters, 0).
package youtubetest; import java.sql.*; import java.awt.event.*; import javax.swing.*; public class youtubetest extends jframe{ private static final string username = "root"; private static final string password = "****"; private static final string conn_string = "jdbc:mysql://localhost:3306/youtube"; connection conn; preparedstatement stmt; jbutton b1,b2; jtextfield t1,t2; jlabel l1,l2; string fname; string lname; public youtubetest() { setsize(600,600); setdefaultcloseoperation(jframe.exit_on_close); setresizable(false); setlayout(null); add(t1 = new jtextfield(30)); t1.setbounds(10,10,200,50); add(t2 = new jtextfield(30)); t2.setbounds(10, 100, 200, 50); add(l1 = new jlabel("first name : ")); l1.setbounds(220, 10, 200, 50); add(l2 = new jlabel("last name : ")); l2.setbounds(220, 100, 200, 50); add(b1 = new jbutton("submit")); b1.setbounds(10, 400, 60, 60); add(b2 = new jbutton("clear")); b2.setbounds(100, 400, 60, 60); try{ class.forname("com.mysql.jdbc.driver"); conn = drivermanager.getconnection(conn_string,username,password); system.out.println("database connected succesfully ..... "); stmt = conn.preparestatement("insert user(fname,lname) values('"+fname+"','"+lname+"')"); }catch(exception e){ e.printstacktrace(); } b1.addactionlistener(new actionlistener() { public void actionperformed(actionevent ae){ try{ fname = t1.gettext(); lname = t2.gettext(); stmt.setstring(1, fname); stmt.setstring(2, lname); stmt.executeupdate(); }catch(exception e){ e.printstacktrace(); }} }); b2.addactionlistener(new actionlistener() { public void actionperformed(actionevent ae) { t1.settext(""); t2.settext(""); } }); addwindowlistener(new windowadapter() { public void windowclosing(windowevent we) { try{ stmt.close(); conn.close(); }catch(exception e){ e.printstacktrace(); } } }); setvisible(true); } public static void main(string[] args)throws exception { new youtubetest(); } };
Comments
Post a Comment