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" onclick="checkAll(checkall)" /> <input
type="submit" value="批量删除" onclick="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);
}
}
}

 

posted @ 2018-04-06 21:27  瓶子xf  阅读(584)  评论(0编辑  收藏  举报