如何从jsp页面中的gridview等多行记录中删除单个记录

时间:2014-10-28 05:30:15

标签: java javascript jsp

我正在尝试编辑和删除多条记录中的单条记录。以下是我在jsp页面上显示的示例记录,

enter image description here

我可以使用下面的代码

轻松编辑每一行的记录
  <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
         <%@ page import="java.util.List" %>
          <%@ page import="java.util.Iterator" %>

     <%@page import="java.sql.ResultSet"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    <script src="jquery1.11.0.min.js"></script>

    </head>
    <body>

        <script type="text/javascript">


                function editRecording(task_id) {
                    //alert(task_id)

                            url = "EditRecord";
                            window.location="/RTTSchecking/"+url+"?task_id="+task_id;

                    }


        </script>

  <table align="center" border="1px" width="80%"  >
    <thead>
        <tr>
        <th>User_Id</th>
        <th>Class</th>
        <th>Subject</th>
        <th>Chapter</th>
        <th>Planned_Features</th>
        <th>Planned_Date</th>

    </thead>


            <%Iterator itr;%>       
            <%List data = (List) request.getAttribute("TaskData");

            for(itr = data.iterator(); itr.hasNext();)
            {
                %>
                <tr>
                <% String s = (String) itr.next(); 
                %>
                <td style="background-color: yellow;">
                <input type="text" name="tid" value="<%=s %>"></td>
                <td><input type="text" name="tid" value="<%=itr.next() %>"></td>

                <td><%=itr.next() %></td>
                <td><%=itr.next() %></td>
                <td><%=itr.next() %></td>
                <td><%=itr.next() %></td>


                <td><input type="button"  value="edit" name="<%=s%>" onclick="editRecording(this.name);"></td> 
             <td><input type="button"  value="delete" name="<%=s%>" onclick="deleteRecording(this.name);"></td> 



                <% } %>



                </tr>

                </table>


    </body>
    </html>

EditREcord(servlet的):

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        PrintWriter out = response.getWriter();

        try {

            String a=request.getParameter("task_id");

        convty = new Connectivity();
        con = convty.setConnection();
        st = con.createStatement();
        query = "select * from task_table where task_id='"+a+"'";
        rset = convty.getResultSet(query, con);


        }
        catch(Exception e) {

        }
        finally {
            request.setAttribute("EditData", rset);
            RequestDispatcher dd= request.getRequestDispatcher("editdata.jsp");
            dd.forward(request, response);
            out.close();
            //out.println(""+tid);
        }

    }

上面的代码工作正常。但我的问题是,我想删除基于User_id,Class,Subject,Chapter和Planned_Date的记录,那么如何从单行(想要删除行)中获取这些值?

如何获得以下代码?,

  <script type="text/javascript">


                        function deleteRecording(User_id,Class,Subject,Chapter,Date) {
                            //alert(task_id,Class,Subject,Chapter,Date)

                                    url = "EditRecord";
                                    window.location="/RTTSchecking/"+url+"?user_id="+User_id+"&Tclass="+Class+"&subject="+Subject+"&chapter="+Chapter+"&date="+Date;

                </script>

如果我实现了上面的代码,那么我可以使用下面的代码轻松删除多行中选定的单个记录,

try {

                String uid=request.getParameter("user_id");
String class=request.getParameter("Tclass");
String sub=request.getParameter("subject");
String chap=request.getParameter("chapter");
String date=request.getParameter("date");

            convty = new Connectivity();
            con = convty.setConnection();
            st = con.createStatement();
            query = "delete from task_table where User_id='"+uid+"' and class='"+class+"' and subject='"+sub+"' and chapter='"+chap+"' and date='"+date+"'";
           st.executeUpdate(query);


            }
            catch(Exception e) {

            }

注意:我可以使用从task_table删除来删除记录,其中User_id =&#39;&#34; + uid +&#34;&#39;;

但如何处理 query =&#34;从task_table中删除User_id =&#39;&#34; + uid +&#34;&#39;和班级=&#39;&#34; +班级+&#34;&#39;和subject =&#39;&#34; + sub +&#34;&#39;和章=&#39;&#34; + chap +&#34;&#39;和日期=&#39;&#34; +日期+&#34;&#39;&#34;;

我希望有人能帮助我。

2 个答案:

答案 0 :(得分:0)

从数据库中删除数据意味着将其删除。

您只需点击删除即可重新加载该页面。

答案 1 :(得分:0)

我希望你现在已经找到了问题的解决方案。但是如果你没有,我今天遇到了同样的问题,经过多次实验,我想出了以下语法,用于删除多个标准的行:

  " DELETE FROM table_name WHERE " +
  " first_db_field_name = '" + matching_variable_name + "'" +
  " and next_db_field_name = '" + next_matching_variable_name + "'" +
  " and last_db_field_name = '" + last_matching_variable_name + "'";