title: Delete a record from database using servlet
body: <p>The Remove.java is the servlet and the index.jsp is the jsp file. I am trying to delete the file using email,since it is unique.</p>
  
  <p>Remove.java</p>
  
  <pre><code>package servletPool;
  
  import java.io.IOException;
  import java.io.PrintWriter;
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.PreparedStatement;
  
  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("/Remove")
  public class Remove extends HttpServlet {
  private static final long serialVersionUID = 1L;
  
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
  
      String uEmail = request.getParameter("email");
  
      try {
          Class.forName("com.mysql.jdbc.Driver");
          String user = "root";
          String pass = "root";
          String query = "delete from user_details where email=?";
          Connection con = DriverManager.getConnection("jdbc:mysql://locahost:3306/dbname", user, pass);
          PreparedStatement ps = con.prepareStatement("delete from user_details where email=?");
          ps.setString(1, uEmail);
  
          int i = ps.executeUpdate();
  
          if(i &gt; 0) {
              out.println("User successfully removed...");
          }
  
      } catch (Exception e) {
          System.out.println(e);
      }
  }
  
  }
  </code></pre>
  
  <p>and here is the index.jsp file from which the record will be deleted.</p>
  
  <p>index.jsp</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;Registration Page.&lt;/title&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div&gt;
  
  
             &lt;div style="padding : 10px;"&gt;
              &lt;center&gt;
  
                  &lt;form action="Remove.servlet" method="get"&gt;
                  &lt;h5&gt;If you  want to remove users,&lt;/h5&gt;
                  &lt;input type="email" placeHolder="User's Email" name="email"/&gt;
                  &lt;input type="button" value="Click" name="remove"/&gt;
                  &lt;/form&gt;
  
              &lt;/center&gt;
          &lt;/div&gt;
  
      &lt;/center&gt;
  
      &lt;/div&gt;
     &lt;/body&gt;
     &lt;/html&gt;
  </code></pre>