接上文 Oracle學習筆記
PL/SQL 數據分頁
Java調用無返回值的存儲過程
create table book ( bookId number, bookName varchar2(50), publishHouse varchar2(50) );
create or replace procedure pro_page (BookId in number,BookName in varchar2,PublishHouse in varchar2)is begin insert into book values(BookId,BookName,PublishHouse); end;
Java中調用存儲過程(無返回值)
CallabelStatement cs = connection.prepareCall(“{call pro(?,?,?)}”);
cs.setInt(1,10);
cs.setString(2,”笑傲江湖”);
cs.setString(3,”人民出版社”);
cs.execute();/******************************************************************************/
Java調用有返回值的存儲過程
create or replace procedure pro1 (no in number,name out varchar2)is begin select ename into name from SCOTT.Emp where empno =no; end;
Java中調用存儲過程(有返回值)
CallabelStatement cs = connection.prepareCall(“{call pro1(?,?)}”);
cs.setInt(1,7788);
cs.registerOutParameter(2,oracle.jdbc.OracleTypes.VARCHAR);
cs.execute();
String name = cs.getString(2);//取出返回值/******************************************************************************/
返回結果集的存儲過程
Step1.創建一個包,在包中定義類型test_cursor
create or replace package testpackage as type test_cursor is refcursor; end testpackage;
Step2.創建過程
create or replace procedure pro(no in number,v_cursor out testpackage.test_cursor) begin open v_cursor for select * from SCOTT.emp where deptno =no; end;
Step3.在Java中調用
CallabelStatement cs = connection.prepareCall(“{call pro(?,?)}”);
cs.setInt(1,10);
cs.registerOutParameter(2,oracle.jdbc.OracleTypes.CURSOR);
cs.execute();
ResultSetrs = (ResultSet)cs.getObject(2);
while(rs.next()){
}
分頁過程
select t1.*,rownum rn from(select*fromSCOTT.Student) t1;--按照編號排序 select t1.*,rownum rn from(select*fromSCOTT.Student) t1 where rownum<10; select * from(select t1.*,rownum rn from(select*fromSCOTT.Student) t1 where rownum<=20)where rn >=11;--查詢第11-20條數據~~~~模板 select * from(select t1.*,rownum rn from(select*fromSCOTT.EMP orderby sal) t1 where rownum<=9)where rn >=5;--按照sal排序
create or replace package testpackage as type test_cursor is refcursor; --創建包,聲明游標 end testpackage;
create or replace procedure fenye (tablename invarchar2, Pagesize in number, Pagenow in number, myrows out number,--總記錄數 myPageCount out number,--總頁數 my_cursor out testpackage.test_cursor--返回的記錄集 )is v_sql varchar2(1000); v_begin number:=(Pagenow-1)*Pagesize+1; v_end number:=Pagenow*Pagesize; begin v_sql :='select* from (select t1.*,rownum rn from (select * from '||tablename||'order by sal) t1 where rownum <= '||v_end||')where rn >= '||v_begin; open my_cursor for v_sql; v_sql :='selectcount(*) from '|| tablename; execute immediate v_sql into myrows; if mod(myrows,Pagesize)=0 then myPageCount =myrows/Pagesize; else myPageCount =myrows/Pagesize+1; endif; end;
Java調用
CallabelStatement cs = connection.prepareCall(“{call fenye(?,?,?,?,?,?)}”);//調用存儲過程
cs.setString(1,”SCOTT.EMP”);//設置表名
cs.setInt(2,5);//設置Pagesize
cs.setInt(3,1);//設置Pagenow
cs.registerOutParameter(4,oracle.jdbc.OracleTypes.INTEGER);//注冊總記錄數
cs.registerOutParameter(5,oracle.jdbc.OracleTypes.INTEGER);// 注冊總頁數
cs.registerOutParameter(6,oracle.jdbc.OracleTypes.INTEGER);// 注冊返回結果集
cs.execute();
introwNum = cs.getInt(4);//
intpageCount = cs.getInt(5);
ResultSetrs = (ResultSet)cs.getObject(6);//結果集
異常處理
預定義異常no_data_found
declare
v_name SCOTT.EMP.ENAME%type;
begin
select ename intov_name from SCOTT.EMP where empno = &no;
dbms_output.put_line('名字:'||v_name);
exception
when no_data_found then
dbms_output.put_line('編號沒有');
end;
預定義異常case_not_found
create or replace procedure pro(no number)is
v_sal SCOTT.EMP.SAL%type;
begin
select sal intov_sal from SCOTT.EMP where empno =no;
case
when v_sal <1000then
update SCOTT.EMP set sal = sal +100 where empno =no;
when v_sal <2000then
update SCOTT.EMP set sal = sal +200 where empno =no;
endcase;
exception
when case_not_found then
dbms_output.put_line('case語句沒有與'||v_sal||'相匹配的條件');
end;
預定義異常cursor_already_open
declare
cursor emp_cursor is select ename,sal from SCOTT.EMP;
begin
open emp_cursor;
for emp_record in emp_cursor
loop
dbms_output.put_line(emp_record.ename);
end loop;
exception
when cursor_already_open then
dbms_output.put_line('游標已經被打開');
end;
預定義異常dup_val_on_index
begin
insert into SCOTT.DEPT values(10,'公安部','北京');
exception
when dup_val_on_index then
dbms_output.put_line('在deptno列上不能出現重復值');
end;
預定義異常invalid_cursor
declare
cursor emp_cursor is select ename,sal from SCOTT.EMP;
emp_record emp_cursor%rowtype;
begin
--open emp_cursor;--打開游標
fetch emp_cursor into emp_record;
dbms_output.put_line(emp_record.ename);
close emp_cursor;
exception
when invalid_cursor then
dbms_output.put_line('請檢查游標是否已經打開');
end;
預定義異常invalid_number
begin
update SCOTT.EMP set sal = sal +'lll';
exception
when invalid_number then
dbms_output.put_line('無效數字');
end;預定義異常too_many_rows
declare
v_name SCOTT.EMP.ENAME%type;
begin
select ename into v_name from SCOTT.EMP;
exception
when too_many_rows then
dbms_output.put_line('返回了多行');
end;
預定義異常zero_divide
被除數為0時觸發。
預定義異常value_error
declare
v_name varchar2(2);
begin
select ename intov_name from SCOTT.EMP where empno = &no;
dbms_output.put_line(v_name);
exception
when value_error then
dbms_output.put_line('變量尺寸不足');
end;
預定義異常login_denied
用戶非法登錄時觸發。
預定義異常not_logged_on
如果用戶沒有登錄就執行dml就會觸發。
預定義異常storage_error
如果超出了內存空間或是內存被破壞就觸發。
預定義異常timeout_on_resorce
如果Oracle在等待資源時,出現了超時就觸發。
自定義異常
create or replace procedure pro_exception_test(no number)
is
my_exception exception;--自定義異常
begin
update SCOTT.EMP set sal = sal +100 where empno =no;
if sql%not found then
raise my_exception;
endif;
exception when my_exception then
dbms_output.put_line('沒有做任何更新');
end;
視圖與表的區別
① 表需要占用磁盤空間,視圖不需要
② 視圖不能添加索引
③ 使用視圖可以簡化復雜查詢
④ 視圖有利于提高安全性
創建視圖
create view my_view as select * fromSCOTT.EMP where sal <1000;
可以在最后帶上 with read only
刪除視圖
drop view my_view
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。