<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 6,  comments - 4,  trackbacks - 0

    數(shù)據(jù)庫(kù)例子
    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------
    create database bbs;

    use bbs;

    create table article
    (
    id int primary key auto_increment,
    pid int,
    rootid int,
    title varchar(255),
    cont text,
    pdate datetime,
    isleaf int
    );

    insert into article values (null, 0, 1, '螞蟻大戰(zhàn)大象', '螞蟻大戰(zhàn)大象', now(), 1);
    insert into article values (null, 1, 1, '大象被打趴下了', '大象被打趴下了',now(), 1);
    insert into article values (null, 2, 1, '螞蟻也不好過(guò)','螞蟻也不好過(guò)', now(), 0);
    insert into article values (null, 2, 1, '瞎說(shuō)', '瞎說(shuō)', now(), 1);
    insert into article values (null, 4, 1, '沒(méi)有瞎說(shuō)', '沒(méi)有瞎說(shuō)', now(), 0);
    insert into article values (null, 1, 1, '怎么可能', '怎么可能', now(), 1);
    insert into article values (null, 6, 1, '怎么沒(méi)有可能', '怎么沒(méi)有可能', now(), 0);
    insert into article values (null, 6, 1, '可能性是很大的', '可能性是很大的', now(), 0);
    insert into article values (null, 2, 1, '大象進(jìn)醫(yī)院了', '大象進(jìn)醫(yī)院了', now(), 1);
    insert into article values (null, 9, 1, '護(hù)士是螞蟻', '護(hù)士是螞蟻', now(), 0);

    ------------------------------------------------------------------------------------------------------------------------------




    下面是用遞歸列出樹(shù)狀結(jié)構(gòu):

    import java.sql.*;



    public class ArticleTree {
        

        
    public static void main(String[] args) {
            ArticleTree at 
    = new ArticleTree();
            at.show();
        }

        
        
    public void show(){
            Connection conn 
    = null;
            Statement stmt 
    = null;
            ResultSet rs 
    = null;
            
    try{
                Class.forName(
    "com.mysql.jdbc.Driver");
                conn 
    = DriverManager.getConnection("jdbc:mysql://localhost/bbs?user=root&password=********");
                stmt 
    = conn.createStatement();
                rs 
    = stmt.executeQuery("select * from article where pid = 0");
                
    while (rs.next()){
                    System.out.println(rs.getString(
    "cont"));
                    tree(conn, rs.getInt(
    "id"),1);
                }

            }
    catch(SQLException e){
                e.printStackTrace();
            }
    catch(ClassNotFoundException e){
                e.printStackTrace();
            }
    finally{
                
    try{
                    
    if (rs!=null){
                        rs.close();
                        rs 
    = null;
                    }

                    
    if (stmt!=null){
                        stmt.close();
                        stmt 
    = null;
                    }

                    
    if (conn!=null){
                        conn.close();
                        conn 
    = null;
                    }

                }
    catch(SQLException e){
                    e.printStackTrace();
                }

            }

        }

        
        
    private void tree(Connection conn,int id, int level){
            Statement stmt 
    = null;
            ResultSet rs 
    = null;
            StringBuffer strPre 
    = new StringBuffer("");
            
    for(int i = 0; i < level ; i++){
                strPre.append(
    "    ");
            }

            
    try{
                stmt 
    = conn.createStatement();
                String sql 
    = "select * from article where pid =" + id;
                rs 
    = stmt.executeQuery(sql);
                
    while(rs.next()){
                    System.out.println(strPre 
    + rs.getString("cont"));
                    
    if(rs.getInt("isleaf")!=0){
                        tree(conn , rs.getInt(
    "id") , level+1);
                        
                    }

                }

                
            }
    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();
                }

            }

        }


    }



    輸出結(jié)果:
    --------------------------------------------------------------------------
    螞蟻大戰(zhàn)大象
        大象被打趴下了
            螞蟻也不好過(guò)
            瞎說(shuō)
                沒(méi)有瞎說(shuō)
            大象進(jìn)醫(yī)院了
                護(hù)士是螞蟻
        怎么可能
            怎么沒(méi)有可能
            可能性是很大的

    --------------------------------------------------------------------------

    復(fù)習(xí)以下遞歸,這個(gè)東西如果不熟練,自己寫起來(lái)是很麻煩的事情。腦子里老轉(zhuǎn)不過(guò)彎。
    posted on 2009-04-06 01:42 shivaree 閱讀(508) 評(píng)論(1)  編輯  收藏

    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 日本红怡院亚洲红怡院最新| 婷婷久久久亚洲欧洲日产国码AV | 免费国产成人高清视频网站| 亚洲一级毛片免费观看| 久久久久久成人毛片免费看| 亚洲VA成无码人在线观看天堂| 精品在线免费观看| 自拍偷自拍亚洲精品第1页| 黄桃AV无码免费一区二区三区| 久久精品夜色噜噜亚洲A∨| 久久成人18免费网站| 亚洲国产精品乱码一区二区| 日韩在线不卡免费视频一区| 亚洲黄色在线观看| 四虎永久在线精品免费网址| 亚洲国产精品成人AV在线| 国产精品免费看久久久无码| 一个人看的www免费在线视频| 亚洲中文久久精品无码ww16| 久久久久久一品道精品免费看| 亚洲第一精品电影网| 免费高清在线影片一区| 一级中文字幕乱码免费| 亚洲AV本道一区二区三区四区 | 一级全免费视频播放| 国产亚洲精品资源在线26u| 免费国产污网站在线观看15| 中文字幕 亚洲 有码 在线| 国产精品视_精品国产免费 | 成人免费无遮挡无码黄漫视频| 亚洲AV无码成人网站在线观看| 免费又黄又爽又猛的毛片| 久久青草国产免费观看| 亚洲中文字幕无码久久| 亚洲精品偷拍视频免费观看| 久久A级毛片免费观看| 色偷偷亚洲男人天堂| 亚洲AV日韩精品久久久久久| 日本免费福利视频| 香蕉免费一区二区三区| 国产精品亚洲五月天高清|