81,114
社区成员
发帖
与我相关
我的任务
分享conn=JDBCUtil.getConn();
sql="{call pro_page(?,?,?,?,?,?,?,?,?)}";
CallableStatement cstmt=conn.prepareCall(sql);
cstmt.setString(1, pageBean.getOrderbyColumn());
cstmt.setString(2, pageBean.getOrderbyType());
cstmt.setString(3, pageBean.getReturnColumn());
cstmt.setString(4, pageBean.getTableName());
cstmt.setInt(5, pageBean.getCurrentPage());
cstmt.setInt(6,pageBean.getPageSize());
cstmt.setString(7, pageBean.getWhereSQL());
//输出参数,说明这是一个返回值
cstmt.registerOutParameter(8, oracle.jdbc.OracleTypes.NUMBER);
cstmt.registerOutParameter(9, oracle.jdbc.OracleTypes.CURSOR);
//执行
cstmt.execute();
//总记录数
int countrecord=cstmt.getInt(8);
pageBean.setTotalRecord(countrecord);
rs=(ResultSet)cstmt.getObject(9);create or replace procedure pro_page(
v_in_orderbycolumn in out varchar2, --排序字段
v_in_orderbytype in out varchar2, --升序ASC或降序DESC
v_in_returncolumn in out varchar2, --返回字段
v_in_tablename in out varchar2, --表名
v_in_currentpage in out number, --当前页
v_in_pagesize in out number, --每页显示记录数
v_in_sqlwhere in out varchar2, --查询条件不带where,带and
v_out_totalrecord out number, --返回总记录数
v_out_cursor out pac_attrtype.cursortype --返回当前页数据
)
as
--声明变量
v_exc_notable exception;
v_startrow number;
v_endrow number;
v_sql varchar2(500);
begin
--1 处理输入参数的默认操作
if v_in_returncolumn is null then
v_in_returncolumn :='*';
end if;
--默认升序
if v_in_orderbytype is null then
v_in_orderbytype :='ASC';
end if;
if v_in_tablename is null then
raise v_exc_notable;
end if;
if v_in_currentpage is null then
v_in_currentpage :=1;
end if;
if v_in_pagesize is null then
v_in_pagesize :=5;
end if;
if v_in_sqlwhere is null then
v_in_sqlwhere :='';
end if;
--2 计算开始行和结束行
v_startrow :=(v_in_currentpage-1)*v_in_pagesize+1;
v_endrow :=v_in_currentpage*v_in_pagesize;
--3 拼接查询分页的SQL语句
if v_in_orderbycolumn is not null then
DBMS_OUTPUT.PUT_LINE(v_in_orderbycolumn);
v_sql :='select * from (select rownum rn,g.* from(select '||v_in_returncolumn||' from '||v_in_tablename||' where 1=1 '||v_in_sqlwhere||' order by '||v_in_orderbycolumn||' '||v_in_orderbytype|| ') g where rownum<='||v_endrow||') where rn>='||v_startrow;
else
v_sql :='select * from (select rownum rn,g.* from(select '||v_in_returncolumn||' from '||v_in_tablename||' where 1=1 '||v_in_sqlwhere||') g where rownum<='||v_endrow||') where rn>='||v_startrow;
DBMS_OUTPUT.PUT_LINE(v_sql);
end if;
--4 执行查询-动态的SQL,把结果给赋值v_out_cursor
open v_out_cursor for v_sql;
--5 查询总记录数赋值给v_out_totalrecord
v_sql :='select count(*) from '||v_in_tablename||' where 1=1 '||v_in_sqlwhere;
execute immediate v_sql into v_out_totalrecord;
exception
--异常处理
when v_exc_notable then
Dbms_Output.put_line('表和视图不存在!');
when others then
Dbms_Output.put_line('过程有误!');
end pro_page;