title: Delete multiple rows using checkboxes in JSP
body: <p>I'm new to coding and JSP language and i want to know how to delete multiple records in an HTML table using checkboxes, i manage to delete one single row, anyway here is my code and thanks in advance!</p>
  
  <hr>
  
  <p>• An picture which explain what i want to do :</p>
  
  <p><a href="https://i.stack.imgur.com/03jYl.png" rel="nofollow noreferrer">Screenshot</a></p>
  
  <p>• Full JSP code :</p>
  
  <pre><code>&lt;%@page import="java.sql.PreparedStatement"%&gt;
  &lt;%@page import="java.sql.SQLException"%&gt;
  &lt;%@page import="java.sql.DriverManager"%&gt;
  &lt;%@page import="java.sql.ResultSet"%&gt;
  &lt;%@page import="java.sql.Statement"%&gt;
  &lt;%@page import="java.sql.Connection"%&gt;
  &lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
          &lt;title&gt;JSP Page&lt;/title&gt;
          &lt;style&gt;
              th,tr,td
              {
                  font-family: Lucida Sans;
              }
              th{
                  background-color: aquamarine;
              }
              #btn{
                  font-family: Lucida Grande;
                  border-radius: 5px;
                  font-size: 100%;
                  width: 100px;
                  height: 25px;
                  font-weight: bold;
              }
          &lt;/style&gt;
      &lt;/head&gt;
      &lt;%!
          String driver = "oracle.jdbc.OracleDriver";
          String url = "jdbc:oracle:thin:@localhost:1521:orcl";
          String user = "root";
          String pass = "1122";
          Connection conn = null;
          Statement stmt = null;
          ResultSet rs = null;
  
  
  
           public Connection getCon(){
               try{
                   Class.forName(driver);
                   conn = DriverManager.getConnection(url, user, pass);
               }catch(Exception e){
                   e.printStackTrace();
               }
               return conn;
           }
      %&gt;
  
      &lt;%
             String deleteQuery = "DELETE FROM persons WHERE person_id = ?";
             PreparedStatement psDelete = null;
  
  
             String id = request.getParameter("ckbox");
             String clickDelete = request.getParameter("submit");
  
              try{
                  psDelete = getCon().prepareStatement(deleteQuery);
  
  
                      psDelete.setString(1, id);
  
  
                  if(clickDelete !=null){
  
                      psDelete.executeUpdate();
  
  
      %&gt;   
      &lt;body&gt;
      &lt;script&gt;
              alert("Deleted successfully!");
      &lt;/script&gt;
      &lt;% }
  
              }catch(SQLException se){
                  se.printStackTrace();
              }
  
      %&gt;
  
      &lt;%
          String fetchQuery = "SELECT * FROM persons ORDER BY person_id";
              try{
                  stmt = getCon().createStatement();
                  rs = stmt.executeQuery(fetchQuery);
  
      %&gt;
  
          &lt;div align="center"&gt;
  
              &lt;form name="myform" action="newjsp.jsp" method="POST"&gt;
   &lt;table border="1" cellpadding="9" style="border-collapse: collapse"&gt;
     &lt;tbody&gt;
                  &lt;tr&gt;
       &lt;th&gt;#&lt;/th&gt;
       &lt;th&gt;Employee ID&lt;/th&gt;
       &lt;th&gt;First Name&lt;/th&gt;
       &lt;th&gt;Job&lt;/th&gt;
                  &lt;/tr&gt;
  
      &lt;%  while(rs.next()){
      %&gt;
  
                  &lt;tr&gt;
       &lt;td&gt;&lt;input type="checkbox" name="ckbox" value="&lt;%= rs.getString("person_id") %&gt;" /&gt;&lt;/td&gt;
       &lt;td&gt;&lt;%= rs.getString("person_id")%&gt; &lt;/td&gt;
       &lt;td&gt; &lt;%= rs.getString("first_name") %&gt; &lt;/td&gt;
       &lt;td&gt; &lt;%= rs.getString("job_title") %&gt; &lt;/td&gt;
                  &lt;/tr&gt;
  
      &lt;%          }
      %&gt;
     &lt;/tbody&gt;
    &lt;/table&gt;
      &lt;%
              }catch(Exception ex){
                  ex.printStackTrace();
              }
      %&gt;
      &lt;br&gt;&lt;br&gt;
                  &lt;input type="submit" value="Delete" name="submit" id="btn"/&gt;
              &lt;/form&gt;
  
  
          &lt;/div&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>• The important part i guess :</p>
  
  <pre><code>&lt;%
             String deleteQuery = "DELETE FROM persons WHERE person_id = ?";
             PreparedStatement psDelete = null;
  
  
             String id = request.getParameter("ckbox");
             String clickDelete = request.getParameter("submit");
  
              try{
                  psDelete = getCon().prepareStatement(deleteQuery);
  
  
                      psDelete.setString(1, id);
  
                      psDelete.executeUpdate();
  
  
      %&gt;
  </code></pre>
  
  
  
  <pre><code>            &lt;tr&gt;
   &lt;td&gt;&lt;input type="checkbox" name="ckbox" value="&lt;%= rs.getString("person_id") %&gt;" /&gt;&lt;/td&gt;
   &lt;td&gt;&lt;%= rs.getString("person_id")%&gt; &lt;/td&gt;
   &lt;td&gt; &lt;%= rs.getString("first_name") %&gt; &lt;/td&gt;
   &lt;td&gt; &lt;%= rs.getString("job_title") %&gt; &lt;/td&gt;
              &lt;/tr&gt;
  
  &lt;%          }
  %&gt;   
  </code></pre>