Java JDBC中的递归查询树代码说明:
程序主要是用Java JDBC连接Oracle数据库,并用递归的方式查询树状数据。可以无限级查询数据。
数据表图如下
- 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节点,1代表文件名为资料库的数据,如18代表它的父文件名其id 为18为规章制度,***生成树状的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 创建时间: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("更新数据所用的时间"+(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();
- }
- }
- //实现递归查询
- 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;
- }
- }