淺談Java JDBC中的遞歸查詢樹
作者:wangfeihg85
本文將介紹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ù)表圖如下
- create table FILE_FILES
- (
- FILE_ID INTEGER not null,
- NAME VARCHAR2(500),
- PARENT_ID INTEGER,
- FILE_TITLE VARCHAR2(500),
- FILE_TYPE VARCHAR2(150),
- FILE_PATH VARCHAR2(4000),
- )
第三列是父文件標志,0為最root節(jié)點,1代表文件名為資料庫的數(shù)據(jù),如18代表它的父文件名其id 為18為規(guī)章制度,***生成樹狀的path路徑到第6列。
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.text.SimpleDateFormat;
- /**
- * @author 作者姓名 wangyongfei
- * @version 創(chuàng)建時間:Jun 16, 2009 3:01:07 AM
- * 類說明:
- */
- public class OtherConnection {
- private static Connection conn;
- private static PreparedStatement stmt;
- private static ResultSet rs ;
- public static String driver = "oracle.jdbc.driver.OracleDriver";
- public static String url = "jdbc:oracle:thin:@192.168.0.23:1521:arsystem";
- public static String uName = "aradmin";
- public static String uPwd = "ar#admin#";
- public String path = "";
- public String flag = "/";
- public OtherConnection(){
- }
- public Connection getConnection(){
- try{
- Class.forName(driver);
- conn = DriverManager.getConnection(url,uName,uPwd);
- return conn;
- }catch(Exception e){
- e.printStackTrace();
- return null;
- }
- }
- public static void main(String arsg[]){
- long startTime = System.currentTimeMillis();
- String sql = "select * from file_files";
- String update = "";
- OtherConnection o = new OtherConnection();
- conn = o.getConnection();
- try {
- ResultSet _rs = o.getResult(sql,conn);
- if(_rs!=null){
- while(_rs.next()){
- String _path = "";
- long col01 = _rs.getLong(1);
- String col02 = _rs.getString(2);
- long col03 = _rs.getLong(3);
- _path = o.iterative(col03, _path,conn);
- if(col03==0){
- update = "update file_files f set f.file_path = '/' where f.file_id = "+col01;
- }else{
- update = "update file_files f set f.file_path = '"+_path+"/"+col02+"' where f.file_id = "+col01;
- }
- o.update(update,conn);
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- long endTime = System.currentTimeMillis();
- SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
- System.out.print("更新數(shù)據(jù)所用的時間"+(startTime - endTime));
- }
- public ResultSet getResult(String sql,Connection _conn) {
- try {
- stmt = _conn.prepareStatement(sql);
- ResultSet m_rs = stmt.executeQuery();
- return m_rs;
- } catch (SQLException e) {
- e.printStackTrace();
- return null;
- }
- }
- public void update(String sql,Connection _conn) {
- try {
- stmt = _conn.prepareStatement(sql);
- stmt.execute();
- stmt.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- //實現(xiàn)遞歸查詢
- public String iterative(long id,String _path,Connection _conn) throws SQLException{
- String sql = "select * from file_files f where f.file_id = "+id;
- PreparedStatement stmt = _conn.prepareStatement(sql);
- ResultSet rs = stmt.executeQuery(sql);
- if(null!=rs){
- while(rs.next()){
- long col01 = rs.getLong(1);
- String col02 = rs.getString(2);
- long col03 = rs.getLong(3);
- path = flag+col02+_path;
- iterative(col03,path,conn);
- }
- }else{
- path = flag;
- }
- stmt.close();
- return path;
- }
- }
【編輯推薦】