使用JSP中的复选框删除多行

时间:2017-04-15 16:28:40

标签: html sql jsp servlets checkbox

我是编码和JSP语言的新手,我想知道如何使用复选框删除HTML表中的多个记录,我设法删除一行,无论如何这里是我的代码,并提前感谢!< / p>

•解释我想做什么的图片:

Screenshot

•完整的JSP代码:

<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <style>
            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;
            }
        </style>
    </head>
    <%!
        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;
         }
    %>

    <%
           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();


    %>   
    <body>
    <script>
            alert("Deleted successfully!");
    </script>
    <% }

            }catch(SQLException se){
                se.printStackTrace();
            }

    %>

    <%
        String fetchQuery = "SELECT * FROM persons ORDER BY person_id";
            try{
                stmt = getCon().createStatement();
                rs = stmt.executeQuery(fetchQuery);

    %>

        <div align="center">

            <form name="myform" action="newjsp.jsp" method="POST">
 <table border="1" cellpadding="9" style="border-collapse: collapse">
   <tbody>
                <tr>
     <th>#</th>
     <th>Employee ID</th>
     <th>First Name</th>
     <th>Job</th>
                </tr>

    <%  while(rs.next()){
    %>

                <tr>
     <td><input type="checkbox" name="ckbox" value="<%= rs.getString("person_id") %>" /></td>
     <td><%= rs.getString("person_id")%> </td>
     <td> <%= rs.getString("first_name") %> </td>
     <td> <%= rs.getString("job_title") %> </td>
                </tr>

    <%          }
    %>
   </tbody>
  </table>
    <%
            }catch(Exception ex){
                ex.printStackTrace();
            }
    %>
    <br><br>
                <input type="submit" value="Delete" name="submit" id="btn"/>
            </form>


        </div>
    </body>
</html>

•我猜的重要部分是:

<%
           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();


    %>
            <tr>
 <td><input type="checkbox" name="ckbox" value="<%= rs.getString("person_id") %>" /></td>
 <td><%= rs.getString("person_id")%> </td>
 <td> <%= rs.getString("first_name") %> </td>
 <td> <%= rs.getString("job_title") %> </td>
            </tr>

<%          }
%>   

1 个答案:

答案 0 :(得分:1)

我有一些建议给你。不要使用scriptlet。由于缺乏可重用性和安全性问题,它们已被弃用。 幸运的是,还有其他选择。您可以使用JSTL库插入Java代码或表达式语言(${...})以插入属性的值。 代码的问题在于while循环。 jsp中的while循环不起作用。您可以尝试使用JSTL for标记迭代结果集的所有元素并创建所有复选框。 如果这可以解决您的问题,请告诉我。

相关问题