运用递归 删除父节点同事删除子节点
2011-08-07 21:00
295 查看
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page import="java.sql.*" %>
<%!
private void del(Connection conn,int id){ //定义了del函数
Statement stmt=null;
ResultSet rs=null;
try{
stmt=conn.createStatement();
String sql="select * from article where pid=" +id ;
rs=stmt.executeQuery(sql);
while(rs.next()){
del(conn,rs.getInt("id"));
}
stmt.executeUpdate("delete from article where id="+id); //递归删除子条目
}catch(SQLException e){
e.printStackTrace();
}finally{
try {
if(rs != null) {
rs.close();
rs = null;
}
if(stmt != null) {
stmt.close();
stmt = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
%>
<%
String admin=(String)session.getAttribute("admin");
if(admin==null||!admin.equals("true")){
out.println("请登陆");
return;
}
%>
<%
int id= Integer.parseInt(request.getParameter("id"));
int pid= Integer.parseInt(request.getParameter("pid"));
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/bbs?user=root&password=admin";
Connection conn = DriverManager.getConnection(url);
conn.setAutoCommit(false); //设置不要自动提交
del(conn,id);
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select count(*) from article where pid="+pid);
rs.next();
int count=rs.getInt(1);
rs.close();
stmt.close();
if(count<=0){ // 判断要删除的节点的父节点是否为变成叶子节点
Statement stmtUpdate=conn.createStatement();
stmtUpdate.executeUpdate("update article set isleaf=0 where id ="+pid);
stmtUpdate.close();
}
conn.commit(); //提交SQL语句
conn.setAutoCommit(true); //回复conn的提交方式为自动提交
conn.close();
%>
<html>
<head>
<title>My JSP 'Delete.jsp' starting page</title>
</head>
<body>
<a href="ShowArticleTree.jsp"><font size="7" color="#ff0080">删除成功,点击返回 </font><br></a>
</body>
</html>
<%@ page import="java.sql.*" %>
<%!
private void del(Connection conn,int id){ //定义了del函数
Statement stmt=null;
ResultSet rs=null;
try{
stmt=conn.createStatement();
String sql="select * from article where pid=" +id ;
rs=stmt.executeQuery(sql);
while(rs.next()){
del(conn,rs.getInt("id"));
}
stmt.executeUpdate("delete from article where id="+id); //递归删除子条目
}catch(SQLException e){
e.printStackTrace();
}finally{
try {
if(rs != null) {
rs.close();
rs = null;
}
if(stmt != null) {
stmt.close();
stmt = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
%>
<%
String admin=(String)session.getAttribute("admin");
if(admin==null||!admin.equals("true")){
out.println("请登陆");
return;
}
%>
<%
int id= Integer.parseInt(request.getParameter("id"));
int pid= Integer.parseInt(request.getParameter("pid"));
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/bbs?user=root&password=admin";
Connection conn = DriverManager.getConnection(url);
conn.setAutoCommit(false); //设置不要自动提交
del(conn,id);
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select count(*) from article where pid="+pid);
rs.next();
int count=rs.getInt(1);
rs.close();
stmt.close();
if(count<=0){ // 判断要删除的节点的父节点是否为变成叶子节点
Statement stmtUpdate=conn.createStatement();
stmtUpdate.executeUpdate("update article set isleaf=0 where id ="+pid);
stmtUpdate.close();
}
conn.commit(); //提交SQL语句
conn.setAutoCommit(true); //回复conn的提交方式为自动提交
conn.close();
%>
<html>
<head>
<title>My JSP 'Delete.jsp' starting page</title>
</head>
<body>
<a href="ShowArticleTree.jsp"><font size="7" color="#ff0080">删除成功,点击返回 </font><br></a>
</body>
</html>
相关文章推荐
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- Android中删除节点下的所有子节点,包括子节点的子节点,用递归方法
- 递归删除一个节点以及该节点下的所有节点示例
- 递归删除一个节点以及该节点下的所有节点
- 递归删除所有叶子节点
- javascript实现二叉树的创建,遍历,添加,查找最大值最小值和指定值的寻找及删除功能——递归的多次运用
- ccflow 删除附件bug&审核时节点字段数据丢失bug&递归查找签核人bug
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- C++非递归遍历删除树节点——广度优先,逐层删除
- 二叉排序树节点的删除(C++,算法导论),前中后序遍历(递归/非递归,栈实现),按层次遍历(队列实现)
- 二分搜索树非递归节点计数删除
- 递归删除树形结构的所有子节点(java和mysql实现)
- 递归删除一个节点以及该节点下的所有节点示例
- 【基础备忘】关于递归删除链表节点为什么不会断链问题解释
- Java递归删除树形结构父节点
- Java递归实现删除树形结构的任一节点
- Mysql中的递归层次查询(根据父节点查找所有的子节点和根据子节点查询所有的父节点)的两种运用
- 高性能删除树形结构的空节点(递归遍历树形结构) Delphi
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- (C语言)BinarySrearchTree二叉搜索树 --- 标准插入(递归,非递归)、遍历(前,中,后序)、查找(递归,非递归)、根插入递归(左旋,右旋)、最小最大值、删除节点