浅谈Java JDBC中的递归查询树(oracle数据库)

Java JDBC中的递归查询树代码说明:

程序主要是用Java JDBC连接Oracle数据库,并用递归的方式查询树状数据。可以无限级查询数据。

数据表图如下  

 
 
  1. create table FILE_FILES  
  2. (  
  3.   FILE_ID     INTEGER not null,  
  4.   NAME        VARCHAR2(500),  
  5.   PARENT_ID   INTEGER,  
  6.   FILE_TITLE  VARCHAR2(500),  
  7.   FILE_TYPE   VARCHAR2(150),  
  8.   FILE_PATH   VARCHAR2(4000),  
  9. )  

java jdbc递归查询树数据表图

第三列是父文件标志,0为最root节点,1代表文件名为资料库的数据,如18代表它的父文件名其id 为18为规章制度,最后生成树状的path路径到第6列。

 
 
  1. import java.sql.Connection;  
  2. import java.sql.DriverManager;  
  3. import java.sql.PreparedStatement;  
  4. import java.sql.ResultSet;  
  5. import java.sql.SQLException;  
  6. import java.text.SimpleDateFormat;  
  7.  
  8. /**   
  9.  * @author 作者姓名  wangyongfei   
  10.  * @version 创建时间:Jun 16, 2009 3:01:07 AM   
  11.  * 类说明:   
  12.  */  
  13.  
  14. public class OtherConnection {  
  15.    
  16.  private static Connection conn;  
  17.  
  18.     private static PreparedStatement stmt;  
  19.       
  20.     private static ResultSet rs ;  
  21.  
  22.     public static String driver = "oracle.jdbc.driver.OracleDriver";  
  23.  
  24.     public static String url = "jdbc:oracle:thin:@192.168.0.23:1521:arsystem";  
  25.  
  26.     public static String uName = "aradmin";  
  27.  
  28.     public static String uPwd = "ar#admin#";  
  29.       
  30.     public String path = "";  
  31.       
  32.     public String flag = "/";  
  33.       
  34.     public OtherConnection(){  
  35.     }  
  36.       
  37.     public Connection getConnection(){  
  38.      try{  
  39.       Class.forName(driver);  
  40.       conn = DriverManager.getConnection(url,uName,uPwd);  
  41.       return conn;  
  42.      }catch(Exception e){  
  43.       e.printStackTrace();  
  44.       return null;  
  45.      }  
  46.     }  
  47.       
  48.     public static void main(String arsg[]){  
  49.      long startTime = System.currentTimeMillis();  
  50.      String sql = "select * from file_files";  
  51.      String update = "";  
  52.      OtherConnection o = new OtherConnection();  
  53.      conn = o.getConnection();  
  54.      try {  
  55.    ResultSet _rs = o.getResult(sql,conn);  
  56.    if(_rs!=null){  
  57.     while(_rs.next()){  
  58.      String _path = "";  
  59.      long col01 = _rs.getLong(1);  
  60.      String col02 = _rs.getString(2);  
  61.      long col03 = _rs.getLong(3);  
  62.        
  63.      _path = o.iterative(col03, _path,conn);  
  64.      if(col03==0){  
  65.       update = "update file_files f set f.file_path = '/' where f.file_id = "+col01;  
  66.      }else{  
  67.       update = "update file_files f set f.file_path = '"+_path+"/"+col02+"' where f.file_id = "+col01;  
  68.      }  
  69.      o.update(update,conn);  
  70.     }  
  71.    }  
  72.      
  73.   } catch (Exception e) {  
  74.    e.printStackTrace();  
  75.   }  
  76.   long endTime = System.currentTimeMillis();  
  77.   SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");  
  78.     
  79.   System.out.print("更新数据所用的时间"+(startTime - endTime));  
  80.     }  
  81.  
  82.     public ResultSet getResult(String sql,Connection _conn) {  
  83.         try {  
  84.          stmt = _conn.prepareStatement(sql);  
  85.             ResultSet m_rs = stmt.executeQuery();  
  86.             return m_rs;  
  87.         } catch (SQLException e) {  
  88.             e.printStackTrace();  
  89.             return null;  
  90.         }  
  91.     }  
  92.     public void update(String sql,Connection _conn) {  
  93.      try {  
  94.       stmt = _conn.prepareStatement(sql);  
  95.       stmt.execute();  
  96.       stmt.close();  
  97.      } catch (SQLException e) {  
  98.       e.printStackTrace();  
  99.      }  
  100.     }  
  101.     //实现递归查询  
  102.     public String iterative(long id,String _path,Connection _conn) throws SQLException{  
  103.   String sql = "select * from file_files f where f.file_id = "+id;  
  104.   PreparedStatement stmt = _conn.prepareStatement(sql);  
  105.   ResultSet rs = stmt.executeQuery(sql);  
  106.   if(null!=rs){  
  107.    while(rs.next()){  
  108.     long col01 = rs.getLong(1);  
  109.     String col02 = rs.getString(2);  
  110.     long col03 = rs.getLong(3);  
  111.     path = flag+col02+_path;  
  112.     iterative(col03,path,conn);  
  113.    }  
  114.   }else{  
  115.    path = flag;  
  116.   }  
  117.   stmt.close();  
  118.   return path;  
  119.  }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值