自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

淺談Java JDBC中的遞歸查詢樹

開發(fā) 后端
本文將介紹Java JDBC中的遞歸查詢樹,遞歸查詢?yōu)榘嘀貙哟谓Y(jié)構(gòu)的關(guān)系數(shù)據(jù)提供了一種非常靈活而有效的處理方法。

Java JDBC中的遞歸查詢樹代碼說明:

程序主要是用Java JDBC連接Oracle數(shù)據(jù)庫,并用遞歸的方式查詢樹狀數(shù)據(jù)??梢詿o限級查詢數(shù)據(jù)。

數(shù)據(jù)表圖如下  

  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遞歸查詢樹數(shù)據(jù)表圖

第三列是父文件標志,0為最root節(jié)點,1代表文件名為資料庫的數(shù)據(jù),如18代表它的父文件名其id 為18為規(guī)章制度,***生成樹狀的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 創(chuàng)建時間: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("更新數(shù)據(jù)所用的時間"+(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.     //實現(xiàn)遞歸查詢  
  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.  }  

【編輯推薦】

  1. 談談優(yōu)化JDBC數(shù)據(jù)庫編程
  2. 實例說明對MySQL的JDBC連接設置
  3. 淺談如何利用JSP網(wǎng)頁中JDBC代碼連接MySQL
  4. 淺談JDBC代碼如何重復使用
  5. 如何進行Jython數(shù)據(jù)庫插入(JDBC)
責任編輯:彭凡 來源: 網(wǎng)易博客
相關(guān)推薦

2009-06-29 17:17:57

Spring

2023-08-29 09:46:12

SQLCTE遞歸

2009-07-15 15:47:12

JDBC DAO

2009-07-15 17:11:31

JDBC的概念

2009-07-15 15:18:01

JDBC連接SQL S

2009-07-15 17:00:49

JDBC查詢

2022-03-15 08:36:46

遞歸查詢SQL

2009-07-01 16:01:58

JSP數(shù)據(jù)庫

2009-07-01 17:58:20

JSP

2009-06-06 19:17:37

java遞歸刪除

2009-07-15 16:23:54

Java JDBC

2009-07-21 17:41:58

JDBC數(shù)據(jù)源

2010-04-02 15:04:14

Oracle遞歸查詢

2009-07-16 17:06:55

JSP網(wǎng)頁中JDBC代

2009-07-15 18:07:47

JDBC代碼

2009-05-05 09:46:18

Java編碼理論字符

2010-10-11 09:05:40

SQL Server

2009-07-23 13:37:45

JDBC連接SQL S

2009-07-16 14:46:48

jdbc statem

2009-07-17 17:41:25

JDBC連接SQL S
點贊
收藏

51CTO技術(shù)棧公眾號