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

J2EE 专栏收录该内容
7 篇文章 0 订阅

为解决网友的问题而写的实例,相关地址 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") %>' οnclick="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" οnclick="checkAll()" /> 全选
				<input type="button" value="删除" οnclick="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>




  • 1
    点赞
  • 3
    评论
  • 8
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

cgs1999

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值