zoukankan      html  css  js  c++  java
  • [BBS] Delete.jsp 05 递归删除

    删除 本帖 和 它的所有孩子帖。 然后判断 本帖 的父亲帖子是否还有孩子。没有则设置为叶子帖子
    <%@ page language="java" contentType="text/html; charset=GBK"
        pageEncoding="GBK"%>
    <%@ page import="java.sql.*"%>
    
    <%!private void del(Connection conn, int id) {
            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=root";
        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();
        conn.setAutoCommit(true);
        conn.close();
        response.sendRedirect("ShowArticleTree.jsp");
    %>
    

  • 相关阅读:
    python程序打包,来源于知乎(已验证)
    登录窗体界面设计
    窗体应用常见操作
    MDI窗体应用
    第四单元
    Test3_3——3_20
    TEST3_2
    ff文字省略号
    jQuery 浏览器高度宽度获取
    CSS 单行溢出文本显示省略号...的方法(兼容IE FF)(转)
  • 原文地址:https://www.cnblogs.com/robbychan/p/3786847.html
Copyright © 2011-2022 走看看