单个JSP页面中实现查询和删除数据实例

为解决网友的问题而写的实例,相关地址 http://bbs.csdn.net/topics/390383104?page=1#post-393839610


在同一个JSP页面处理删除操作,主要是将要删除的记录id作为当前jsp页面的参数传入,若参数不为空则获取deleteIds参数信息执行删除操作


index.jsp文件内容:

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page import="java.sql.*"%>
<%
//驱动类名
String className = "org.gjt.mm.mysql.Driver";
// 数据库URL
String url = "jdbc:mysql://localhost/win";
// 用户名
String userName = "root";
// 密码
String password = "";

String listSql = "select * from email_history order by id limit 1,100";
String deleteSql = "delete from email_history where id in (%s)";

Class.forName(className);
Connection conn = DriverManager.getConnection(url, userName, password);
Statement stmt = conn.createStatement();

String deleteIds = request.getParameter("deleteIds");
if(deleteIds!=null && !"".equals(deleteIds)) {
	stmt.execute(String.format(deleteSql, deleteIds));
}
%>
<html>
<head>
<title>会议信息</title>
</head>

<body>
	<table border="1">
<%
ResultSet rs = stmt.executeQuery(listSql);
String ids = "";
while (rs.next()){
	ids += "," + rs.getInt("id");
%>
		<tr>
			<td><input type="checkbox" id='checkbox_<%=rs.getInt("id") %>' name='checkbox_<%=rs.getInt("id") %>' onclick="doCheck()" /></td>
			<td><%=rs.getInt("id") %></td>
			<td><%=rs.getString("title") %></td>
			<td><%=rs.getString("from_email").replaceAll("<","<").replaceAll(">",">") %></td>
			<td><%=rs.getString("to_email") %></td>
		</tr>
<%
}
if(!"".equals(ids)) {
	ids = ids.substring(1);
}
%>
		<tr>
			<td colspan="5">
				<input type="checkbox" id="checkall" name="checkall" onclick="checkAll()" /> 全选
				<input type="button" value="删除" onclick="doDelete()" />
			</td>
		</tr>
	</table>
	
<script type="text/javascript">
var ids = '<%=ids %>';
function doDelete() {
	if(ids=='') {
		return;
	}
	
	var deleteIds = "";
	var idList = ids.split(",");
	var len = idList.length;
	for(var i=0; i<len; i++) {
		if($("checkbox_" + idList[i]).checked) {
			deleteIds += "," + idList[i];
		}
	}
	if(deleteIds=="") {
		alert("请选择要删除的记录!");
		return;
	}
	deleteIds = deleteIds.substr(1);
	location.href = "./index.jsp?deleteIds=" + deleteIds;
}
function doCheck() {
	if(ids=='') {
		return;
	}
	
	var deleteIds = "";
	var idList = ids.split(",");
	var len = idList.length;
	var checkall = $("checkall");
	for(var i=0; i<len; i++) {
		if(!$("checkbox_" + idList[i]).checked) {
			checkall.checked = false;
			return;
		}
	}
	checkall.checked = true;
}
function checkAll() {
	if(ids=='') {
		return;
	}

	var idList = ids.split(",");
	var len = idList.length;
	var checkall = $("checkall");
	for(var i=0; i<len; i++) {
		$("checkbox_" + idList[i]).checked = checkall.checked;
	}
}
function $(id) {
	return document.getElementById(id);
}
</script>
</body>
</html>




猜你喜欢

转载自blog.csdn.net/cgs1999/article/details/8642748