如何获取选定的行详细信息并传递到servlet

时间:2019-04-04 05:33:02

标签: jsp servlets

Servlet:

try {
  PrintWriter out = response.getWriter();
  String tokn = request.getParameter("user");
  HttpSession session=request.getSession(false);
  String sesuser = (String)session.getAttribute("Member");
  String url = "jdbc:mysql://localhost:3306/library";
  Connection con = null;
  Statement st = null;
  ResultSet rs = null;
  String tkname = null;

  Class.forName("com.mysql.jdbc.Driver");
  con = DriverManager.getConnection(url, "root", "root12345");
  st = con.createStatement();
  String sql = "select * from tokenlist where token='"+tokn+"'";
  rs = st.executeQuery(sql);  
  while(rs.next())
  {
    tkname = rs.getString(1);
  }

  if(tkname.equalsIgnoreCase(sesuser)){
    out.print("session "+sesuser+" "+tkname+" "+tokn);
    PreparedStatement pst1 = con.prepareStatement("delete from tokenlist where token='"+tokn+"'");
    pst1.executeUpdate();
  }
  else {
    out.print("else session "+sesuser+" "+tkname+" "+tokn);
  }
  //response.sendRedirect("formlib.jsp");

} catch (Exception ex) {
  System.out.println(ex);
}

JSP:

<div class="table-list">
  <form action="delete" method="POST"> 

    <table id="table">
      <tr>
        <th style="color:black; text-align: center;" >Name</th>
        <th style="color:black;text-align: center;">Token</th>
        <th style="color:black;text-align: center;">Date</th>
        <th style="color:black;text-align: center;">Action</th>
      </tr>
      <%
        while (rs.next()) {
      %>
        <TR>
          <TD style="color:black;text-align: center;"><%=rs.getString(1)%></TD>
          <TD style="color:black;text-align: center;"><%=rs.getInt(2)%></TD>
          <TD style="color:black;text-align: center;"><%=rs.getDate(3)%></TD>
          <td style="text-align:center;">
            <input type="hidden" name="user" value="<%=rs.getString(2)%>" />
            <button type="submit" class="btn-dlte">X</button>
          </td>
        </TR>
      <% } %>
    </table>
  </form>
</div>

我正在从数据库中检索数据,并使用html和jsp将其显示在表中。有一个列可删除所选行。当用户选择特定行时,应从表和数据库中删除整行。不幸的是,当用户选择表中的一行时,第一行将被删除。 注意:我要删除用户选择的表行。

0 个答案:

没有答案