CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Java >  Web 开发

根据数据库如何建树,各位有没有这方面的代码或提示!谢谢!

楼主june37(june)2003-06-03 10:56:28 在 Java / Web 开发 提问

根据数据库如何建树,各位有没有这方面的代码或提示!谢谢!  
  june37@163.net 问题点数:50、回复次数:3Top

1 楼d80(今天没事做)回复于 2003-06-03 11:17:05 得分 50

这是我写的关于用递归形成数装的文件,你看看吧  
  import   java.sql.*;  
  import   java.io.*;  
  import   java.util.*;  
   
  public   class   TreeTest   {  
          int   layer=-1;//\u8BBE置\u5C42\u6570,默任\u4E3A-1  
          String   space="";  
          public   Connection   getConnection()   throws   Exception{  
                  Class.forName("oracle.jdbc.driver.OracleDriver");  
                  Connection   conn   =   DriverManager.getConnection("jdbc:oracle:thin:@10.216.0.2:1521:ORCL","dms2","dms2");  
                  return   conn;  
          }  
   
          /**  
            *   \u4F20入自己的id\u53F7,\u9012\u5F52\u663E示出\u6811  
            *   @param   id  
            *   @throws   Exception  
            */  
          public   void   tree(String   id)   throws   Exception{  
                  boolean   hasChild=false;  
                  boolean   hasNextBrother=false;  
                  layer=-1;  
                  space="";  
                  try{  
                          Connection   conn   =getConnection();  
                          Statement   stmt=conn.createStatement();  
                          String   sql1="select   id,fid,name   from   test_tree   where   fid='"+id+"'";  
                          ResultSet   rs1=stmt.executeQuery(sql1);  
                          while(rs1.next()){  
                                  //得到自己所在的\u5C42\u6570  
                                  getLayer(rs1.getString("id"));  
   
                                  //判\u65AD是否有子\u8282\u70B9  
                                  hasChild=hasChild(rs1.getString("id"));  
   
                                  //判\u65AD是否有下一\u4E2A兄弟\u8282\u70B9  
                                  hasNextBrother=hasNextBrother(rs1.getString("id"));  
                                  //\u753B出子\u8282\u70B9的\u56FE片  
                                  drawNode(hasChild,hasNextBrother);  
   
                                  //\u753B出子\u8282\u70B9  
                                  System.out.println(space+rs1.getString("name"));  
   
                                  //\u9012\u5F52\u8C03用  
                                  tree(rs1.getString("id"));  
                          }  
                          stmt.close();  
                          conn.close();  
                  }catch(Exception   ex){  
                          ex.printStackTrace();  
                  }  
          }  
          /**  
            *   返回自己所在的\u5C42\u6570  
            *   @param   id   自己的id\u53F7  
            *   @throws   Exception  
            */  
          public   void   getLayer(String   id)   throws   Exception{  
                  Connection   conn   =getConnection();  
                  Statement   stmt=conn.createStatement();  
                  String   sql="select   id,fid   from   test_tree   where   id='"+id+"'";  
                  ResultSet   rs=stmt.executeQuery(sql);  
                  while(rs.next()){  
                          layer++;  
                          getLayer(rs.getString("fid"));  
                  }  
                  stmt.close();  
                  conn.close();  
          }  
   
          /**  
            *  
            *   @param   id  
            *   @return   判\u65AD是否有子\u8282\u70B9,有返回true,\u6CA1有有返回false  
            *   @throws   Exception  
            */  
          public   boolean   hasChild(String   id)   throws   Exception{  
                  Connection   conn   =getConnection();  
                  Statement   stmt=conn.createStatement();  
                  String   sql="select   id,fid   from   test_tree   where   fid='"+id+"'";  
                  ResultSet   rs=stmt.executeQuery(sql);  
                  if(rs.next()){  
                          return   true;  
                  }  
                  stmt.close();  
                  conn.close();  
                  return   false;  
          }  
   
          /**  
            *  
            *   @param   id  
            *   @return   判\u65AD是否有下一\u4E2A兄弟  
            *   @throws   Exception  
            */  
          public   boolean   hasNextBrother(String   id)   throws   Exception{  
                  Connection   conn   =getConnection();  
                  Statement   stmt=conn.createStatement();  
                  String   sql="select   id   from   test_tree   where   fid   in   (select   fid   from   test_tree   where   id='"+id+"')";  
                  ResultSet   rs=stmt.executeQuery(sql);  
                  while(rs.next()){  
                          if(rs.getString("id").equals(id))  
                                  break;  
                  }  
                  if(rs.next()){  
                          return   true;  
                  }  
                  stmt.close();  
                  conn.close();  
                  return   false;  
          }  
          /**  
            *  
            *   @param   hasChild  
            *   @param   layer  
            *   @return  
            */  
          public   String   drawNode(boolean   hasChild,boolean   hasNextBrother){  
                  for(int   i=0;i<layer-1;i++){  
                          space+="     ";/*  
                          if(hasChild==true&&hasNextBrother==true){  
                                  space+="|   ";  
                          }  
                          if(hasChild==true&&hasNextBrother==false){  
                                  space+="|   ";  
                          }  
                          if(hasChild==false&&hasNextBrother==true){  
                                  space+="|   ";  
                          }  
                          if(hasChild==false&&hasNextBrother==false){  
                                  space+="|   ";  
                          }*/  
                  }  
                  if(hasChild){//hasChild  
                          if(hasNextBrother){//hasNextBrother  
                                  space+="★";  
                          }else{  
                                  space+="※";  
                          }  
                          //space+="-";  
                  }else{//hasnochild  
                          if(hasNextBrother){//hasNextBrother  
                                  space+="   ";  
                          }else{  
                                  space+="■";  
                          }  
                          //space   +=   "+";  
                  }  
                  return   space;  
          }  
   
   
          public   static   void   main(String[]   args){  
                  try   {  
                          TreeTest   treeTest=new   TreeTest();  
                          Connection   conn=treeTest.getConnection();  
                          Statement   stmt=conn.createStatement();  
                          String   sql2   ="select   id,fid,name   from   test_tree   where   id='0'";  
                          ResultSet   rs2   =   stmt.executeQuery(sql2);  
                          if   (rs2.next())   {//\u753B出根\u8282\u70B9  
                                  System.out.println(rs2.getString("name"));  
                          }  
                          treeTest.tree(rs2.getString("id"));  
                          stmt.close();  
                          conn.close();  
                  }  
                  catch   (Exception   ex)   {  
                          ex.printStackTrace();  
                  }  
          }  
  }Top

2 楼rgbahnh(秋风)回复于 2003-06-04 10:59:16 得分 0

厉害,佩服Top

3 楼june37(june)回复于 2003-06-06 15:28:01 得分 0

楼上的兄台,我看得不是很明白,可以分析一下流程吗?表结构是什么样子的啊!  
  你这个树是什么样子的啊!谢谢!Top

相关问题

  • 代码创建数据库
  • 谁有用ODBC联制作数据库的bcb源代码,或给点提示啊
  • 怎样打开代密码的ACCESS97数据库,总是提示“独占”,代码怎么写
  • 怎样打开代密码的ACCESS97数据库,总是提示“独占”,代码怎么写
  • 关于数据库连接,请给个提示马上给分,帮看看我代码
  • intraweb 用ado连接数据库 ,调试执行时提示如下错误:access violation 错误。代码如下:
  • 请大霞 给一段打开vf数据库的代码 我自己写的代码总提示出错,说未找到数据源odbc
  • 如何用代码把access2000数据库转成access97数据库
  • 如何用代码压缩数据库。
  • vb代码里访问sql数据库

关键词

  • sql
  • getconnection
  • conn
  • throws exception
  • rs
  • connection
  • import java

得分解答快速导航

  • 帖主:june37
  • d80

相关链接

  • CSDN Java频道
  • Java类图书
  • Java类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo