递归显示数据库目录树

1.数据库: 

SET FOREIGN_KEY_CHECKS=0; 
-- ---------------------------- 
-- Table structure for article 
-- ---------------------------- 
CREATE TABLE `article` ( 
  `id` int(11) NOT NULL auto_increment, 
  `pid` int(11) default NULL, 
  `rootid` int(11) default NULL, 
  `title` varchar(255) default NULL, 
  `cont` text, 
  `pdate` datetime default NULL, 
  `isleaf` int(11) default NULL, 
  PRIMARY KEY  (`id`) 
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8; 

-- ---------------------------- 
-- Records 
-- ---------------------------- 
INSERT INTO `article` VALUES ('1', '0', '1', '蚂蚁大战大象', '蚂蚁大战大象', '2009-07-27 01:44:54', '1'); 
INSERT INTO `article` VALUES ('2', '1', '1', '大象被打趴下了', '大象被打趴下了', '2009-07-27 01:44:54', '1'); 
INSERT INTO `article` VALUES ('3', '2', '1', '蚂蚁也不好过', '蚂蚁也不好过', '2009-07-27 01:44:54', '0'); 
INSERT INTO `article` VALUES ('4', '2', '1', '瞎说', '瞎说', '2009-07-27 01:44:54', '1'); 
INSERT INTO `article` VALUES ('5', '4', '1', '没有瞎说', '没有瞎说', '2009-07-27 01:44:54', '0'); 
INSERT INTO `article` VALUES ('6', '1', '1', '怎么可能', '怎么可能', '2009-07-27 01:44:54', '1'); 
INSERT INTO `article` VALUES ('7', '6', '1', '怎么没有可能', '怎么没有可能', '2009-07-27 01:44:54', '0'); 
INSERT INTO `article` VALUES ('8', '6', '1', '可能性是很大的', '可能性是很大的', '2009-07-27 01:44:54', '0'); 
INSERT INTO `article` VALUES ('9', '2', '1', '大象进医院了', '大象进医院了', '2009-07-27 01:44:54', '1'); 
INSERT INTO `article` VALUES ('10', '9', '1', '护士是蚂蚁', '护士是蚂蚁', '2009-07-27 01:44:54', '0'); 

2.源代码: 
Java代码   收藏代码
  1. package com.bjtu.jdbc;  
  2.   
  3. import java.sql.*;  
  4.   
  5. public class ArticleTree {  
  6.     public static void main(String[] args) {  
  7.         new ArticleTree().show();  
  8.     }  
  9.       
  10.     public void show() {  
  11.         Connection conn = null;  
  12.         Statement stmt = null;  
  13.         ResultSet rs = null;  
  14.         try {  
  15.             Class.forName("com.mysql.jdbc.Driver");  
  16.             conn = DriverManager  
  17.                     .getConnection("jdbc:mysql://localhost/bbs?user=root&password=admin");  
  18.             stmt = conn.createStatement();  
  19.             rs = stmt.executeQuery("select * from article where pid = 0"); //根节点  
  20.             while(rs.next()){  
  21.                 System.out.println(rs.getString("cont"));  
  22.                 tree(conn, rs.getInt("id"), 1);  
  23.             }  
  24.         } catch(ClassNotFoundException e) {  
  25.             e.printStackTrace();  
  26.         } catch (SQLException e) {  
  27.             e.printStackTrace();  
  28.         } finally {  
  29.             try {  
  30.                 if(rs != null) {  
  31.                     rs.close();  
  32.                     rs = null;  
  33.                 }  
  34.                 if(stmt != null) {  
  35.                     stmt.close();  
  36.                     stmt = null;  
  37.                 }  
  38.                 if(conn != null) {  
  39.                     conn.close();  
  40.                     conn = null;  
  41.                 }  
  42.             } catch (SQLException e) {  
  43.                 e.printStackTrace();  
  44.             }  
  45.         }  
  46.     }  
  47.       
  48.     private void tree(Connection conn, int id, int level) {  
  49.         Statement stmt = null;  
  50.         ResultSet rs = null;  
  51.           
  52.         StringBuffer strPre = new StringBuffer("");  
  53.         for(int i=0; i<level; i++) {  
  54.             strPre.append("    ");  
  55.         }  
  56.           
  57.         try {  
  58.             stmt = conn.createStatement();  
  59.             String sql = "select * from article where pid = " + id;//父节点ID  
  60.             rs = stmt.executeQuery(sql);  
  61.             while(rs.next()) {  
  62.                 System.out.println(strPre + rs.getString("cont"));  
  63.                 if(rs.getInt("isleaf") != 0)  
  64.                     tree(conn, rs.getInt("id"), level + 1);  
  65.             }  
  66.               
  67.         } catch (SQLException e) {  
  68.             e.printStackTrace();  
  69.         } finally {  
  70.             try {  
  71.                 if(rs != null) {  
  72.                     rs.close();  
  73.                     rs = null;  
  74.                 }  
  75.                 if(stmt != null) {  
  76.                     stmt.close();  
  77.                     stmt = null;  
  78.                 }  
  79.             } catch (SQLException e) {  
  80.                 e.printStackTrace();  
  81.             }  
  82.         }  
  83.     }  
  84. }  

3.运行结果: 
Java代码   收藏代码
  1. 蚂蚁大战大象  
  2.     大象被打趴下了  
  3.         蚂蚁也不好过  
  4.         瞎说  
  5.             没有瞎说  
  6.         大象进医院了  
  7.             护士是蚂蚁  
  8.     怎么可能  
  9.         怎么没有可能  
  10.         可能性是很大的  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值