sql - Saving fields to database using Java -
i have button inside application should saving database. can tell me if see wrong code? not saving @ all.
savebutton.addactionlistener( new actionlistener() { public void actionperformed(actionevent e) { //gets text texfields , saves instance variables string fname = fnametextbox.gettext(); string lname = lnametextbox.gettext(); string email = emailtextbox.gettext(); string signupdate = signuptextbox.gettext(); try { //moves cursor new row //rs.movetoinsertrow(); //statement checks if user enters letters if(fname.matches("[a-za-z]+")) { //statement checks if user enters letters if(lname.matches("[a-za-z]+")) { //statement , actions if user enters '.' if(email.contains(".")) { //gets last period in email int emaildotcheck = email.lastindexof("."); //substring period in variable "emaildotcheck" string extensioncheck = email.substring(emaildotcheck); //statement , actions if user doesn't enter email correctly if(!email.contains("@") || !extensioncheck.matches("\\.[a-z]{3}")) { joptionpane.showmessagedialog(null, "please enter email in correct format!"); emailtextbox.settext(""); } else { //instance variables int month = 100; int day = 100; int year = 10000; //statement , actions if user enters 'signupdate' in correct format if(signupdate.matches("\\d{2}/\\d{2}/\\d{4}")) { //gets substring of instance variables string monthstr = signupdate.substring(0,2); string daystr = signupdate.substring(3,5); string yearstr = signupdate.substring(6); //parsing intstance variables integers month = integer.parseint(monthstr); day = integer.parseint(daystr); year = integer.parseint(yearstr); //statements , actions if user doesn't enter date in correct format if(month > 12 || day > 31 || year > 2100) { joptionpane.showmessagedialog(null, "please enter date in correct format! (dd/mm/yyyy)"); signuptextbox.settext(""); } else { //string sql4 = "insert table1 (fname, lname, [e_mail], [sign_up_date]) values (fname, lname, email, signupdate)"; //execute query, assigning specified record in db 'rs4' //rs4 = st.executequery(sql4); rs.movetoinsertrow(); //inserts record db rs.updatestring("fname", fname); rs.updatestring("lname", lname); rs.updatestring("e-mail", email); rs.updatestring("sign_up_date", signupdate); //inserts data db rs.insertrow(); //closes statement variable there won't gap in db st.close(); //closes result set variable there won't gap in db rs.close(); //create new statement gain access table in db st = con.createstatement(rs.type_scroll_insensitive, rs.concur_updatable); //statement selects our table string sql = "select * table1"; //execute query, assigning records in db 'rs' rs = st.executequery(sql); //gets next row in db rs.next(); //sets text in text fields specified fields in db fnametextbox.settext(rs.getstring("fname")); lnametextbox.settext(rs.getstring("lname")); emailtextbox.settext(rs.getstring("e_mail")); fnametextbox.settext(rs.getstring("sign_up_date")); } } //statement , actions if user enter date in correct format else { joptionpane.showmessagedialog(null, "please enter date in correct format! (dd/mm/yyyy)"); signuptextbox.settext(""); } } } //statement , actions if user doesn't enter email in correct format else { joptionpane.showmessagedialog(null, "please enter email in correct format!"); emailtextbox.settext(""); } } //statement , actions if user doesnt enter last name in correct format else { joptionpane.showmessagedialog(null, "please enter last name in correct format!"); lnametextbox.settext(""); } } //statement , actions if user doesn't enter first name in correct format else { joptionpane.showmessagedialog(null, "please enter first name in correct format!"); fnametextbox.settext(""); } } catch(exception ex) { } } });
start reporting errors encountered
catch(exception ex) { }
a catch block silently consumes errors bad idea.
second, use debugger, step through code , see what's going on. attempting solve problem desk-checking, ie. reading code inefficient. here don't have key information, such whether have confidence
rs.movetoinsertrow();
can expected work - can't see how rs gets value. many database problems occur @ runtime, there's misconfiguration of database connection or attempted insert collides existing data. hence it's understanding code doing in test run problem can solved.
here i'd guess printing out diagnistics in catch helpful, , otherwise stepping in debugger (or adding trace statements) there.
Comments
Post a Comment