title: Delete row from database using Servlet
body: <p>I had a problem in my servlet regarding deleting record from my database. Please look over my servlet code and please do correct me. </p>
  
  <p>ViewBooksServlet.java servlet displays the records in database along with a "Delete" hyperLink in every row. But problem is whenever i try to press delete button.. the elseif loop runs and it show deleted from  DeleteBooksServlet.java servlet but the data is not deleted from the database.. Please guide me how can i delete the record from databse either from id or from book_name.</p>
  
  <p>ViewBooksServlet.java </p>
  
  <pre><code>package books;
  import java.io.IOException;
  import java.io.PrintWriter;
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  import java.sql.ResultSetMetaData;
  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;
  import javax.servlet.http.HttpSession;
  
  @WebServlet("/viewbook")
  public class ViewBooksServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;
  
  protected void doGet(HttpServletRequest request, HttpServletResponse  response) throws ServletException, IOException 
  {
      PrintWriter out=response.getWriter();
      Connection con=null;
      try {
          Class.forName("com.mysql.jdbc.Driver"); 
          con=DriverManager.getConnection("jdbc:mysql://localhost:3306/books_online","root","");
          Statement stmt=con.createStatement();
          ResultSet rs=stmt.executeQuery("Select * from books");
          out.println("&lt;html&gt;&lt;table width='50' border='1'&gt;");
          out.println("&lt;tr&gt;&lt;td&gt;Id&lt;/td&gt;&lt;td&gt;Name&lt;/td&gt;&lt;td&gt;Author&lt;/td&gt;&lt;td&gt;Price&lt;/td&gt;&lt;/tr&gt;");
  
          while(rs.next())
          {
              out.println("&lt;tr&gt;&lt;td&gt;"+rs.getInt(1)+"&lt;/td&gt;&lt;td&gt;"+rs.getString(2)+"&lt;/td&gt;&lt;td&gt;"+rs.getString(3)+"&lt;/td&gt;&lt;td&gt;"+rs.getString(4)+"&lt;/td&gt;&lt;td&gt;&lt;a href='deletebook'&gt;Delete&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;");
          }
          out.write("&lt;/table&gt;&lt;/html&gt;");
  
      } catch (ClassNotFoundException | SQLException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  }
  }
  </code></pre>
  
  <p>DeleteBooksServlet.java</p>
  
  <pre><code>package books;
  
  import java.io.IOException;
  import java.io.PrintWriter;
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  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;
  import javax.servlet.http.HttpSession;
  
  @WebServlet("/deletebook")
  public class DeleteBooksServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;
  
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
  {       
      PrintWriter out=response.getWriter();   
      Connection con=null;
      try {
  
          String id=request.getParameter("id");
          Class.forName("com.mysql.jdbc.Driver");
          con=DriverManager.getConnection("jdbc:mysql://localhost:3306/books_online","root","root");
          PreparedStatement ps=con.prepareStatement("delete from books where id=?");
          ps.setString(1, id);
          int i=ps.executeUpdate();
          if(i!=0)
          {
              out.println("Deleting rows");
          }
          else if(i==0)
          {
              out.print("deleted");
          }
      } catch (ClassNotFoundException | SQLException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  }
  
  }
  </code></pre>