title: insert data into database using servlet and jsp in eclipse
body: <p>I am trying to add or insert values into table of database using servlet and jsp and MySQL Workbench as database. These are the following details:</p>
  
  <p>1.> <strong>Register.java</strong></p>
  
  <pre><code>package register.com;
  
  import java.io.IOException;
  import java.io.PrintWriter;
  
  import javax.servlet.ServletConfig;
  import javax.servlet.ServletException;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  import java.sql.*;
  import javax.servlet.*;
  /**
  * Servlet implementation class Register
  */
  @WebServlet("/register")
  public class Register extends HttpServlet {
  private static final long serialVersionUID = 1L;
  
  /**
   * @see HttpServlet#HttpServlet()
   */
  public Register() {
      super();
      // TODO Auto-generated constructor stub
  }
  
      /**
       * @see Servlet#init(ServletConfig)
       */
      public void init(ServletConfig config) throws ServletException {
          // TODO Auto-generated method stub
      }
  
      /**
       * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse  response)
       */
      protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          // TODO Auto-generated method stub
      }
  
      /**
       * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          // TODO Auto-generated method stub
      }
  
      /**
       * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          // TODO Auto-generated method stub
  
  
              response.setContentType("text/html");  
              PrintWriter pw = response.getWriter(); 
              //String connectionURL = "jdbc:mysql://127.0.0.1:3306/newData";// newData is the database  
              //Connection connection;  
              Connection conn=null;
              String url="jdbc:mysql://localhost:3306/";
              String dbName="userlogindb";
              String driver="com.mysql.jdbc.Driver";
          //String dbUserName="root";
          //String dbPassword="root";
  
          try{  
            String Fname = request.getParameter("fname");  
            String Mname = request.getParameter("mname");  
            String Lname = request.getParameter("lname");  
            String Uname = request.getParameter("username");  
            String Emailid = request.getParameter("emailid");  
            String Mobno = request.getParameter("mobno");  
            String Address = request.getParameter("address");  
            String Password1 = request.getParameter("password1");  
            String Password2 = request.getParameter("password2");  
  
            Class.forName(driver).newInstance();  
            conn = DriverManager.getConnection(url+dbName,"root", "root");
            PreparedStatement pst =(PreparedStatement) conn.prepareStatement("insert into 'userlogindb'.'registerutable'(fname,mname,lname,username,emailid,mobno,address,password1,password2) values(?,?,?,?,?,?,?,?,?)");//try2 is the name of the table  
  
            pst.setString(1,Fname);  
            pst.setString(2,Mname);        
            pst.setString(3,Lname);
            pst.setString(4,Uname);
            pst.setString(5,Emailid);
            pst.setString(6,Mobno);
            pst.setString(7,Address);
            pst.setString(8,Password1);
            pst.setString(9,Password2);
  
  
            int i = pst.executeUpdate();
            conn.commit(); 
            String msg=" ";
            if(i!=0){  
              msg="Record has been inserted";
              pw.println("&lt;font size='6' color=blue&gt;" + msg + "&lt;/font&gt;");  
  
  
            }  
            else{  
              msg="failed to insert the data";
              pw.println("&lt;font size='6' color=blue&gt;" + msg + "&lt;/font&gt;");
             }  
            pst.close();
          }  
          catch (Exception e){  
            pw.println(e);  
          }  
  
  }
  
  }
  </code></pre>
  
  <p>2.> <strong>index.jsp</strong></p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  pageEncoding="ISO-8859-1"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"                    "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;title&gt;Insert title here&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form name="registrationform" action="register" method="post"&gt;
  &lt;p&gt;
  Enter your first name: &lt;input type="text" name="fname"&gt;&lt;br&gt;
  Enter your middle name: &lt;input type="text" name="mname"&gt;&lt;br&gt;
  Enter your last name: &lt;input type="text" name="lname"&gt;&lt;br&gt;
  &lt;/p&gt;&lt;br&gt;
  &lt;p&gt;
  Enter username: &lt;input type="text" name="username"&gt;&lt;br&gt;
  &lt;/p&gt;&lt;br&gt;
  &lt;p&gt;
  Enter email id: &lt;input type="text" name="emailid"&gt;&lt;br&gt;
  Enter mobile number: &lt;input type="text" name="mobno"&gt;&lt;br&gt;
  Enter address: &lt;textarea rows="5" cols="5" name="address"&gt;&lt;/textarea&gt;&lt;br&gt;
  &lt;/p&gt;&lt;br&gt;
  &lt;p&gt;
  Enter the password: &lt;input type="password" name="password1"&gt;&lt;br&gt;
  Reenter the password: &lt;input type="password" name="password2"&gt;&lt;br&gt;
  &lt;/p&gt;&lt;br&gt;
  &lt;p&gt;
  &lt;input type="submit"&gt;
  &lt;/p&gt;&lt;br&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>3.> <strong>web.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"&gt;
  &lt;display-name&gt;RegisterExample&lt;/display-name&gt;
  &lt;welcome-file-list&gt;
  &lt;welcome-file&gt;index.html&lt;/welcome-file&gt;
  &lt;welcome-file&gt;index.htm&lt;/welcome-file&gt;
  &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
  &lt;welcome-file&gt;default.html&lt;/welcome-file&gt;
  &lt;welcome-file&gt;default.htm&lt;/welcome-file&gt;
  &lt;welcome-file&gt;default.jsp&lt;/welcome-file&gt;
  &lt;/welcome-file-list&gt;
  &lt;servlet&gt;
  &lt;description&gt;Register Servlet&lt;/description&gt;
  &lt;display-name&gt;Register&lt;/display-name&gt;
  &lt;servlet-name&gt;Register&lt;/servlet-name&gt;
  &lt;servlet-class&gt;register.com.Register&lt;/servlet-class&gt;  
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
  &lt;servlet-name&gt;Register&lt;/servlet-name&gt;
  &lt;url-pattern&gt;/register&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>4.> <em>I have added mysql-connector-java-5.0.8-bin.jar and servlet-api-3.0.jar</em> is anything else needed to add it. I have created a connection to database in Data Source Explorer.</p>
  
  <p>When I m compiling or debuging it shows no error. During runtime also there is no error.
  When I fill in the form and submit it shows only blank screen :(
  No output is displayed. :(
  Also in the database values are not updated. </p>
  
  <p>Please help me. I m getting hangover with this snippet.</p>