JSP+SERVLET+MYSQL-----------批量删除

jsp:

<div class="result-wrap">
<form action="${pageContext.request.contextPath }/DeleteServlet"
method="post">
<div class="result-title">
<div class="result-list">
<!-- <a id="batchDel" href="javascript:void(0)"><input name="uid[]"
type="checkbox">批量删除</a> -->

<input type="checkbox" id="checkall"
name="checkall" οnclick="checkAll(checkall)" /> <input
type="submit" value="批量删除" οnclick="return confirm('确定删除选择项?')"
align="left">

</div>
</div>
<div class="result-content">
<table class="result-tab" width="100%">
<tr>
<th class="tc" width="5%"></th>
<th>行号</th>
<th>用户编号</th>
<!-- 此处用户编号相当于学生学号,固定不变 -->
<th>用户名</th>
<th>注册人</th>
<th>年龄</th>
<th>性别</th>
<th>编辑</th>
</tr>
<c:forEach items="${list }" var="user" varStatus="stat">
<%-- <c:forEach items="${list }" var="user" > --%>
<!-- varStatus="stat" 用于始终按行号排列 -->
<tr>
<td align="center" class="tc"><input id="${user.uid }"
name="info" value="${user.uid }" type="checkbox"></td>
<td>${stat.count }</td>
<td>${user.uid }</td>
<td>${user.uname }</td>
<td>${user.urealname }</td>
<td>${user.uage }</td>
<td>${user.usex==1?"男":"女" }</td>
<td><a href="edituser?do=editbefore&uid=${user.uid }">修改</a>&nbsp;&nbsp;<a
href="deleteUser?uid=${user.uid }">删除</a></td>
</tr>
</c:forEach>
</table>
<div class="list-page">2 条 1/1 页</div>
</div>
</form>
</div>

js代码:

function checkAll(checkall) {
arr = document.getElementsByName("info");
if (checkall.checked == true) {
for (var i = 0; i < arr.length; i++) {

arr[i].checked = true;
}

} else {
for (var i = 0; i < arr.length; i++) {
if ((arr[i]).checked == false) {
arr[i].checked = true;
} else {
arr[i].checked = false;
}
}
}
}

 

servlet部分:收集数据,跳转连接

/**

*/
package com.qingmang.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qingmang.dao.UserDao;
import com.qingmang.service.UserService;

/**
* @author administrator

*/
public class DeleteServlet extends HttpServlet {

UserDao dao = new UserDao();
UserService service = new UserService();

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

request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String[] str = request.getParameterValues("info");

/* 如何加非空判断? */
if (str == null) {
request.getRequestDispatcher("user").forward(request, response);
} else {
String ids = "";
for (int i = 0; i < str.length; i++) {
if (i != str.length - 1) {
ids += str[i] + ",";
} else {
ids += str[i];
}
}

service.remove(ids);
out.print("<script>alert('删除成功!');window.location.href='user';</script>");

out.flush();
out.close();

}
}

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

doGet(request, response);
}

}

 

dao层:对数据库的处理

/**
* 根据用户id删除用户
* */
public void remove(String uid){
try {
conn = JdbcUtil.getConnection();
String sql="delete from users where uid in (" + uid + ")"; 
pstmt = conn.prepareStatement(sql);
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally{
try {
JdbcUtil.close(conn, pstmt);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值