单个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));
}
%>


会议信息



	
<%
ResultSet rs = stmt.executeQuery(listSql);
String ids = "";
while (rs.next()){
	ids += "," + rs.getInt("id");
%>
		
<%
}
if(!"".equals(ids)) {
	ids = ids.substring(1);
}
%>
		
<%=rs.getInt("id") %> <%=rs.getString("title") %> <%=rs.getString("from_email").replaceAll("<","<").replaceAll(">",">") %> <%=rs.getString("to_email") %>
全选




你可能感兴趣的:(J2EE)