java实现选中删除功能

分析:
通过form表单传递数据,删除数据根据id编号删除。
前台

 <a class="btn btn-primary" href="javascript:void(0);" id="delSelected">删除选中</a>

</div>
<form id="form" action="${pageContext.request.contextPath}/delSelectedServlet" method="post">
<table border="1" class="table table-bordered table-hover">
    <tr class="success">
        <th><input type="checkbox" id="firstCb"></th>
        <th>编号</th>
        <th>姓名</th>
        <th>性别</th>
        <th>年龄</th>
        <th>籍贯</th>
        <th>QQ</th>
        <th>邮箱</th>
        <th>操作</th>
    </tr>
    <c:forEach items="${users}" var="user" varStatus="s">
        <tr>
            <td><input type="checkbox" name="uid" value="${user.id}"></td>
            <td>${s.count}</td>
            <td>${user.name}</td>
            <td>${user.gender}</td>
            <td>${user.age}</td>
            <td>${user.address}</td>
            <td>${user.qq}</td>
            <td>${user.email}</td>
            <td><a class="btn btn-default btn-sm" href="${pageContext.request.contextPath}/findUserServlet?id=${user.id}">修改</a>&nbsp;
                <a class="btn btn-default btn-sm" href="javascript:deleteUser(${user.id});">删除</a></td>
        </tr>

    </c:forEach>
</table>
</form>
Js
<script>
    function deleteUser(id) {
        //用户安全提示
        if (confirm("您确定要删除吗?")){
          //访问的路径
            location.href=" ${pageContext.request.contextPath}/delUserServlet?id="+id;
        }
    }
    window.οnlοad=function () {
        //给删除选中按钮添加单击事件
        document.getElementById("delSelected").οnclick=function () {
            if (confirm("您确定要删除选中条目吗?"))
                //表单提交
                document.getElementById("form").submit();
        }
   /********点击实现全选或全不选*************/
        //1.获取第一个cb
        document.getElementById("firstCb").οnclick=function () {
            //获取下表所有的cb
            var cbs=document.getElementsByName("uid");
            //遍历
            for (var i=0;i<cbs.length;i++){
                //设置这些cbs[i]的checked状态=firstCb.checked
                cbs[i].checked =this.checked;
            }
        }
    }


</script>

后台

Servlet

 @WebServlet("/delSelectedServlet")
 public class DelSelectedServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    //获取所有id
    String[] ids=request.getParameterValues("uid");

    //调用service删除
    UserService service=new UserServiceImpl();
    service.delSelectedUser(ids);
    
    //跳转查询Servlet
    response.sendRedirect(request.getContextPath()+"/userListServlet");
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    this.doPost(request, response);
}

}

Service

 public void delSelectedUser(String[] ids) {
 
 //遍历数组
for (String id:ids){
     //调用dao删除
    dao.deleteUser(Integer.parseInt(id));
  }

}

Dao

public void deleteUser(int uid) {
  String sql="delete from user where uid = ? ";
  template.update(sql,uid);
}
  • 5
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现选中用户的删除功能,一般需要以下步骤: 1. 在前端页面中,添加一个“删除按钮,并添加一个事件处理程序,当用户点击该按钮时,获取该用户的唯一标识符(例如ID),并将其发送到后端API。 2. 在后端API中,接收到前端发送的删除请求,根据用户的唯一标识符,从数据库中删除该用户的记录。 3. 在前端页面中,根据删除请求的结果,更新用户列表,例如重新加载用户列表或从页面中删除该用户的行。 下面是一个例子,用Java语言实现选中用户的删除功能: 前端页面代码: ```html <table id="userTable"> <thead> <tr> <th></th> <th>ID</th> <th>姓名</th> <th>操作</th> </tr> </thead> <tbody> <tr> <td><input type="checkbox" class="userCheckbox" value="1"></td> <td>1</td> <td>张三</td> <td><button class="deleteButton" data-id="1">删除</button></td> </tr> <tr> <td><input type="checkbox" class="userCheckbox" value="2"></td> <td>2</td> <td>李四</td> <td><button class="deleteButton" data-id="2">删除</button></td> </tr> </tbody> </table> <script> // 绑定删除按钮的点击事件处理程序 const deleteButtons = document.querySelectorAll('.deleteButton'); deleteButtons.forEach(button => { button.addEventListener('click', () => { const userId = button.dataset.id; deleteUser(userId); }); }); // 发送删除请求到后端API function deleteUser(userId) { fetch(`/api/users/${userId}`, { method: 'DELETE' }) .then(response => { if (response.ok) { // 删除成功,更新用户列表 const userRow = document.querySelector(`tr[data-id="${userId}"]`); userRow.remove(); } else { // 删除失败,提示错误信息 alert('删除失败'); } }); } </script> ``` 后端API代码: ```java import java.sql.*; public class UserApi { private Connection conn; public UserApi() { // 初始化数据库连接 try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "username", "password"); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } } public void deleteUser(int userId) throws SQLException { String sql = "DELETE FROM users WHERE id = ?"; PreparedStatement stmt = conn.prepareStatement(sql); stmt.setInt(1, userId); stmt.executeUpdate(); } } ``` 请注意,为了简化代码,这里的示例中使用了纯手写的JDBC操作数据库,实际项目中,我们可能会使用ORM框架(例如Hibernate)来操作数据库,或者使用Spring Boot等框架提供的数据库访问功能
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值