title: inserting data into database using JSP
body: <p>I am trying to insert data into my database using following JSP code. I have created database named music and table named tbl_user. After I have entered all the related fields in <code>register.jsp</code>, the control goes to <code>Insertdata.java</code> but data is not going into database.</p>
  
  <p><strong>This is my JSP page:</strong></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
   &lt;html&gt;
      &lt;head&gt;
          &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
          &lt;!-- Bootstrap core CSS --&gt;
          &lt;link href="assets/css/bootstrap.min.css" rel="stylesheet"&gt;
  
          &lt;!-- Custom styles for this template --&gt;
          &lt;link href="assets/css/signin.css" rel="stylesheet"&gt;
          &lt;link href="assets/css/login.css" rel="stylesheet"&gt;
  
          &lt;script src="assets/js/bootstrap.js"&gt;&lt;/script&gt;
          &lt;title&gt;Sign UP&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;div class="container"&gt;
  
              &lt;div class="clearfix"&gt;&lt;/div&gt;
              &lt;div class="col-md-6 col-md-offset-3"&gt;
                  &lt;div class="panel panel-default login"&gt;
                      &lt;div class="panel-heading"&gt;Register Here&lt;/div&gt;
                      &lt;div class="panel-body"&gt;
                          &lt;form class="form-horizontal" role="form" method="post" action="Insertdata"&gt;
                              &lt;div class="form-group"&gt;
                                  &lt;label for="firstname" class="col-sm-3 control-label"&gt; First Name&lt;/label&gt;
                                  &lt;div class="col-sm-8"&gt;
                                      &lt;input type="text" class="form-control" id="firstname"  name="fname" placeholder="First Name"&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                              &lt;div class="form-group"&gt;
                                  &lt;label for="lastname" class="col-sm-3 control-label"&gt; Last Name&lt;/label&gt;
                                  &lt;div class="col-sm-8"&gt;
                                      &lt;input type="text" class="form-control" id="lastname"  name="lname" placeholder="Last Name"&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                              &lt;div class="form-group"&gt;
                                  &lt;label for="adress" class="col-sm-3 control-label"&gt;Address&lt;/label&gt;
                                  &lt;div class="col-sm-8"&gt;
                                      &lt;input type="text" class="form-control" id="address"  name="address" placeholder="Address"&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt; 
                              &lt;div class="form-group"&gt;
                                  &lt;label for="inputEmail3" class="col-sm-3 control-label"&gt;Email&lt;/label&gt;
                                  &lt;div class="col-sm-8"&gt;
                                      &lt;input type="email" class="form-control" id="inputEmail3"  name="email" placeholder="Email"&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                              &lt;div class="form-group"&gt;
                                  &lt;label for="inputPassword3" class="col-sm-3 control-label"&gt;Password&lt;/label&gt;
                                  &lt;div class="col-sm-8"&gt;
                                      &lt;input type="password" class="form-control" id="inputPassword3" name="password" placeholder="Password"&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                              &lt;div class="form-group"&gt;
                                  &lt;div class="col-sm-offset-3 col-sm-10"&gt;
                                      &lt;button type="submit" class="btn btn-default""&gt;Register&lt;/button&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/form&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
  
              &lt;/div&gt;     
          &lt;/div&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>This is my servlet for database connection <strong>DatabaseConnection.java</strong></p>
  
  <pre><code>package mypackage;
  
  import java.sql.Connection;  
  import java.sql.DriverManager;  
  import java.sql.ResultSet;  
  import java.sql.Statement;  
  
  
  public class DatabaseConnection {  
  Connection conn;  
  Statement stmt;  
  ResultSet res;  
  
  public DatabaseConnection(){  
  
  }  
  
  public Connection setConnection(){  
  try{  
      System.out.println("sdsadasd");  
  Class.forName("com.mysql.jdbc.Driver");  
  conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/music","root","");  
   System.out.println("Connected to the database");  
  }catch(Exception e){  
  
  }  
  return conn;  
  }  
  
  public ResultSet getResult(String sql,Connection conn){  
  this.conn=conn;  
  try{  
  stmt=conn.createStatement();  
  res=stmt.executeQuery(sql);  
  }catch(Exception e){  
  
  }  
  return res;  
  }  
  }
  </code></pre>
  
  <p>This is my servlet to insert data <strong>Insertdata.java</strong></p>
  
  <pre><code>package mypackage;
  
  import java.io.IOException;  
  import java.io.PrintWriter;  
  import java.sql.Connection;  
  import java.sql.ResultSet;  
  import java.sql.Statement;  
  import javax.servlet.RequestDispatcher;  
  import javax.servlet.ServletException;  
  import javax.servlet.http.HttpServlet;  
  import javax.servlet.http.HttpServletRequest;  
  import javax.servlet.http.HttpServletResponse;  
  
  
  public class Insertdata extends HttpServlet {  
  
  String fname,lname,address,email,password;  
  String query;  
  Connection conn;  
  Statement stmt;  
  ResultSet res;  
  DatabaseConnection dbconn;  
  
  
  protected void processRequest(HttpServletRequest request, HttpServletResponse response)  
  throws ServletException, IOException {  
  response.setContentType("text/html;charset=UTF-8");  
  PrintWriter out = response.getWriter();  
  try {  
  
  dbconn=new DatabaseConnection();  
  
  //Af_Scheme_Number=request.getParameter("Af_Scheme_Number");  
  fname=request.getParameter("fname");  
  lname=request.getParameter("lname");  
  address=request.getParameter("address");  
  email=request.getParameter("email");  
  password=request.getParameter("password");  
  
  conn=dbconn.setConnection();  
  
  stmt=conn.createStatement();  
  query= "insert into tbl_user(first_name,last_name,address,email,password)"+  
  " values('"+fname+"','"+lname+"','"+address+"',"+email+",'"+ password+"')";  
  int i=stmt.executeUpdate(query);  
  
  } catch(Exception e){  
  
      System.out.println("Error");  
  
  }finally {  
  
  out.close();  
  }  
  }  
  
  
  @Override  
  protected void doGet(HttpServletRequest request, HttpServletResponse response)  
  throws ServletException, IOException {  
  processRequest(request, response);  
  }  
  
  
  @Override  
  protected void doPost(HttpServletRequest request, HttpServletResponse response)  
  throws ServletException, IOException {  
  processRequest(request, response);  
  }  
  
  
  @Override  
  public String getServletInfo() {  
  return "Short description";  
  }  
  }  
  </code></pre>