plsql 树形展示,及java 调用 ( 参考)http://blog.chinaunix.net/u/26573/showart.php?id=494292)
来源:互联网 发布:高德地图城市数据库 编辑:程序博客网 时间:2024/05/16 14:36
--测试数据
drop table region;
create table REGION (
ID CHAR(32) not null,
PID CHAR(32),
NAME VARCHAR2(30),
LEVEL_ number(2),
LEAF_ number(1),
constraint PK_REGION primary key (ID)
);
insert into REGION (ID, PID, NAME, LEVEL_, LEAF_) values ('根',null , '根',0, 0);
insert into REGION (ID, PID, NAME, LEVEL_, LEAF_) values ('华北','根' , '华北',1, 0);
insert into REGION (ID, PID, NAME, LEVEL_, LEAF_) values ('北京','华北' , '北京',2, 1);
insert into REGION (ID, PID, NAME, LEVEL_, LEAF_) values ('沈阳','华北' , '沈阳',2, 1);
insert into REGION (ID, PID, NAME, LEVEL_, LEAF_) values ('华南','根' , '',1, 1);
insert into REGION (ID, PID, NAME, LEVEL_, LEAF_) values ('西北','根' , '',1, 1);
---不知为何好像一个char 类型的'a' 与一个varchar2类型的'a' 并不相等,所以此例只对char 类型的id pid 类型有效,
create or replace package p_cursor is
type cursorType is ref cursor;
--type IdType is table of char(32) index by binary_integer;
end;
/
create or replace type IdType as table of char(32);
/
--判断 有没有子节点 返回1 或者0 (注意类型是char 而不是varchar2 ,好像 不可滥用)
create or replace function hasChild(p_pid char, table_name varchar2 ,pidColName varchar2 ) return number is
childrenCount number:=0;
v_sql_stmt varchar2(100) :='select count(*) from '||table_name||' where '||pidColName||'=:p_pid';
begin
EXECUTE IMMEDIATE v_sql_stmt into childrenCount USING p_pid ;
if childrenCount =0 then
return 0;
else
return 1;
end if ;
end;
/
--此过程,私用,private 通过此过程的递归调用 ,所子孙节点都 返回到ids中
create or replace procedure getAllPrivate(rootid char, table_name varchar2 ,pidColName varchar2 ,idColName varchar2, ids in out IdType ) is
v_id char(32);
id_cur p_cursor.cursorType ;--is select distinct id from region where pid=rootid;
v_sql varchar2(1000) ;
begin
v_sql:= 'select distinct '||idColName||' from '||table_name || ' where '||pidColName ||'= :rootid';
open id_cur for v_sql using rootid ;
--如果有子节点
if hasChild(rootid,table_name ,pidColName)=1 then
fetch id_cur into v_id;--先打开一个(不知为何)
loop
exit when id_cur%notfound;
ids.extend;
ids(ids.last):= v_id; --先不管有无子节点,先把自身加入
getAllPrivate(v_id,table_name,pidColName,idColName,ids);--递归处理子节点
fetch id_cur into v_id;
end loop;
close id_cur;
end if;
end;
/
-- 可以通过此函数的调用
create or replace procedure getAll(rootid char, table_name varchar2 ,pidColName varchar2 ,idColName varchar2, id_cursor in out p_cursor.cursorType) is
v_hasChild number(1);
v_allIds IdType:=IdType();
begin
v_allIds.extend;
v_allIds(1):=rootid;
-- execute immediate sql_stmt;
getAllPrivate(rootid,table_name ,pidColName ,idColName ,v_allIds);
open id_cursor for 'select * from table(cast( :v_allIds as IdType) )' using v_allIds ;
-- open id_cursor for select * from table(cast( v_allIds as IdType) );
end;
/
--测试代码
set serveroutput on
declare cursor_instance p_cursor.cursorType;
id char(32);
begin
--region 表名,pid region.pid ;id region.id cursor_instance 实际并未用到
getAll('华北','region' ,'pid','id',cursor_instance);
fetch cursor_instance into id ;
loop
exit when cursor_instance%notfound;
fetch cursor_instance into id ;
dbms_output.put_line(id);
end loop;
end;
/
java 调用存储过程
//此过程 ,传过来一个id ,然后将此节点的子孙节点的id 返回到一个游标中,
public class OracleProcedureReturnCollectionByPlSqlType_Test {
public static void main(String[] args) throws Exception {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@127.0.0.1:1521:ORCL", "scott", "scott");
CallableStatement stmt = conn.prepareCall("{call getAll(?,?,?,?,?) }");
stmt.setString(1, "华北");//根节点id值, ,此节点的所有子孙的id 将被返回
stmt.setString(2, "region");//表名
stmt.setString(3, "pid");//表region 中表示父节点的列名
stmt.setString(4, "id");//表region 中表示子节点的列名
stmt.registerOutParameter(5, oracle.jdbc.OracleTypes.CURSOR);
stmt.execute();
ResultSet rs = (ResultSet) stmt.getObject(5);
while (rs.next()) {
String id =rs.getString(1);
System.out.println(id);
}
stmt.close();
}
}
- plsql 树形展示,及java 调用 ( 参考)http://blog.chinaunix.net/u/26573/showart.php?id=494292)
- sybase笔记 转自:http://blog.chinaunix.net/u/12390/showart.php?id=473402
- http://blogold.chinaunix.net/u3/90973/showart.php?id=2061985
- 函数模板(Function Template)及带默认参数的函数 http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=25520556&id
- http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=22891521&id=2109284
- http://www.cublog.cn/u/13991/showart.php?id=173007
- ORACLE中的物化视图(原文链接http://blog.chinaunix.net/space.php?uid=8329266&do=blog&id=2032099)
- MongoDB sharding模式实现(http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=28266791&id=5758139 )
- struct的初始化,拷贝及指针成员的使用技巧(摘自http://blog.chinaunix.net/u/9577/showart_417268.html)
- AndroidManifest.xml配置文件详解(转于http://blog.chinaunix.net/uid-9185047-id-3460344.html)
- socket选项总结(setsocketopt) http://blog.chinaunix.net/uid-24517549-id-4044883.html
- 数据库的静默和挂起状态(http://blog.chinaunix.net/u/22151/showart_1165163.html)
- 长短字符串问题(转载自http://blog.chinaunix.net/u/25675/showart_261303.html)
- http://blog.chinaunix.net/search.php?q=linux+forte
- Linux下关于system调用 http://blog.chinaunix.net/uid-21768364-id-3995764.html
- 虚函数及继承 http://blog.chinaunix.net/uid-25132162-id-1564955.html
- zookeeper基本原理及适用场景 转:http://blog.chinaunix.net/uid-26748613-id-4536290.html
- http://blog.chinaunix.net/uid-22666718-id-1771703.html
- 【健康提示】世上最健康的作息时间表
- 两个在Windows mobile开发中常用的how to.
- 资料篇
- 六种用ruby调用执行shell命令的方法
- 使用shell运行sqlplus执行sql
- plsql 树形展示,及java 调用 ( 参考)http://blog.chinaunix.net/u/26573/showart.php?id=494292)
- WINDOWS MOBILE 的ARM 体系支持
- HGE引擎适用于MFC的修改
- 一句话实现一个小功能
- 十三种最深的寂寞,你可曾有过?
- HTML数据岛访问
- 真受不了!
- [讨论]一个真正的IT人来谈中国与印度的软件
- 如何学习Java