java - Data is not being inserted in MySQL database -
i have been trying develop webpage using java.
this index.html
:
<!doctype html> <html > <head> <meta charset="utf-8"> <title>simple dark form</title> <link rel="stylesheet" href="css/style.css"> </head> <body> <form action="../patientregistrationservlet" method="post"> <link href='http://fonts.googleapis.com/css?family=open+sans:400,300,700' rel='stylesheet' type='text/css'> <link href='http://fonts.googleapis.com/css?family=sofia' rel='stylesheet' type='text/css'> <div class='login'> <h2>register</h2> <input name='first name' placeholder='first name' type='text'> <input name='last name' placeholder='last name' type='text'> <input name='ad1' placeholder='address line 1' type='text'> <input name='birthdate' placeholder='birt date' type='text'> <input name='ad2' placeholder='address line 2' type='text'> <input name='country' placeholder='country' type='text'> <input name='state' placeholder='state' type='text'> <input name='city' placeholder='city' type='text'> <input name='pcode' placeholder='pincode' type='text'> <input name='blood group' placeholder='blood group' type='text'> <input name='email' placeholder='e-mail' type='text'> <input name='gender' placeholder='gender' type='text' > <input name='mobilno' placeholder='mobile no' type='text'> <input id='pw' name='pw' placeholder='password' type='password'> <div class='agree'> <input id='agree' name='agree' type='checkbox'> <label for='agree'></label>accept rules , conditions </div> <input class='animated' type='submit' value='register' "> <a class='forgot' href="../cpaneliclinix/login.jsp">already have account?</a> </div> </form> </body> </html>
this java servlet page code:
package com.iclinix.controller; import java.io.ioexception; import java.sql.connection; import java.sql.drivermanager; import java.sql.sqlexception; import java.sql.statement; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; @webservlet("/patientregistrationservlet") public class patientregistrationservlet extends httpservlet { private static final long serialversionuid = 1l; public patientregistrationservlet() { super(); // todo auto-generated constructor stub } protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { } protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { string fname = request.getparameter("first name"); string lname = request.getparameter("last name"); string addr1 = request.getparameter("ad1"); string bd = request.getparameter("birthdate"); string addr2 = request.getparameter("ad2"); string cntry = request.getparameter("country"); string st1 = request.getparameter("state"); string ct = request.getparameter("city"); string pc = request.getparameter("pcode"); string bg = request.getparameter("blood group"); string mail= request.getparameter("email"); string gen = request.getparameter("gender"); string mno = request.getparameter("mobilno"); string pass = request.getparameter("pw"); try { class.forname("com.mysql.jdbc.driver"); connection con = drivermanager.getconnection("jdbc:mysql://localhost:3306/iclinix","root","root"); statement st = con.createstatement(); st.executeupdate("insert tbl_patient_registration (first_name, last_name, add1, birth_date, add2, country, state, city, pincode, bloodgroup, email, gender, mobileno, password)"+ "values('"+fname+"','"+lname+"','"+addr1+"','"+bd+"','"+addr2+"','"+cntry+"','"+st1+"','"+ct+"','"+pc+"','"+bg+"','"+mail+"','"+gen+"','"+mno+"','"+pass+"')"); } catch (classnotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } } }
i have been trying store details of registration page given user in form in mysql database. mysql database connected it.
image link everthing works fine when try go 3 fields first name, last name , password, when try add more fields , run it, nothing gets updated in database. there no error in servlet.
it works first name , last name. please can me?
you might try following:
string insertstatement = "insert tbl_patient_registration (first_name, last_name, add1, birth_date, add2, country, state, city, pincode, bloodgroup, email, gender, mobileno, password) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; preparedstatement pst = con.preparestatement(insertstatement); pst.setstring(1, fname); pst.setstring(2, lname); pst.setstring(3, addr1); pst.setdate(4, bd); // here guessing type of column date; if wrong use setstring instead ... // set rest of parameters here pst.setstring(14, pass); pst.executeupdate(); // execute statement after setting params
this better string concatenation because handles
- sql injection and,
- is readable (my taste) , less error prone.
if want more information preparedstatement
@ jdbc tutorial.
Comments
Post a Comment