您好,登錄后才能下訂單哦!
接上文 Oracle學(xué)習(xí)筆記
PL/SQL 數(shù)據(jù)分頁
Java調(diào)用無返回值的存儲過程
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中調(diào)用存儲過程(無返回值)
CallabelStatement cs = connection.prepareCall(“{call pro(?,?,?)}”); cs.setInt(1,10); cs.setString(2,”笑傲江湖”); cs.setString(3,”人民出版社”); cs.execute();
/******************************************************************************/
Java調(diào)用有返回值的存儲過程
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中調(diào)用存儲過程(有返回值)
CallabelStatement cs = connection.prepareCall(“{call pro1(?,?)}”); cs.setInt(1,7788); cs.registerOutParameter(2,oracle.jdbc.OracleTypes.VARCHAR); cs.execute(); String name = cs.getString(2);//取出返回值
/******************************************************************************/
返回結(jié)果集的存儲過程
Step1.創(chuàng)建一個包,在包中定義類型test_cursor
create or replace package testpackage as type test_cursor is refcursor; end testpackage;
Step2.創(chuàng)建過程
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中調(diào)用
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條數(shù)據(jù)~~~~模板 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; --創(chuàng)建包,聲明游標(biāo) end testpackage;
create or replace procedure fenye (tablename invarchar2, Pagesize in number, Pagenow in number, myrows out number,--總記錄數(shù) myPageCount out number,--總頁數(shù) 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調(diào)用
CallabelStatement cs = connection.prepareCall(“{call fenye(?,?,?,?,?,?)}”);//調(diào)用存儲過程 cs.setString(1,”SCOTT.EMP”);//設(shè)置表名 cs.setInt(2,5);//設(shè)置Pagesize cs.setInt(3,1);//設(shè)置Pagenow cs.registerOutParameter(4,oracle.jdbc.OracleTypes.INTEGER);//注冊總記錄數(shù) cs.registerOutParameter(5,oracle.jdbc.OracleTypes.INTEGER);// 注冊總頁數(shù) cs.registerOutParameter(6,oracle.jdbc.OracleTypes.INTEGER);// 注冊返回結(jié)果集 cs.execute(); introwNum = cs.getInt(4);// intpageCount = cs.getInt(5); ResultSetrs = (ResultSet)cs.getObject(6);//結(jié)果集
異常處理
預(yù)定義異常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;
預(yù)定義異常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;
預(yù)定義異常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('游標(biāo)已經(jīng)被打開'); end;
預(yù)定義異常dup_val_on_index
begin insert into SCOTT.DEPT values(10,'公安部','北京'); exception when dup_val_on_index then dbms_output.put_line('在deptno列上不能出現(xiàn)重復(fù)值'); end;
預(yù)定義異常invalid_cursor
declare cursor emp_cursor is select ename,sal from SCOTT.EMP; emp_record emp_cursor%rowtype; begin --open emp_cursor;--打開游標(biāo) 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('請檢查游標(biāo)是否已經(jīng)打開'); end;
預(yù)定義異常invalid_number
begin update SCOTT.EMP set sal = sal +'lll'; exception when invalid_number then dbms_output.put_line('無效數(shù)字'); end;
預(yù)定義異常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;
預(yù)定義異常zero_divide
被除數(shù)為0時觸發(fā)。
預(yù)定義異常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;
預(yù)定義異常login_denied
用戶非法登錄時觸發(fā)。
預(yù)定義異常not_logged_on
如果用戶沒有登錄就執(zhí)行dml就會觸發(fā)。
預(yù)定義異常storage_error
如果超出了內(nèi)存空間或是內(nèi)存被破壞就觸發(fā)。
預(yù)定義異常timeout_on_resorce
如果Oracle在等待資源時,出現(xiàn)了超時就觸發(fā)。
自定義異常
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;
視圖與表的區(qū)別
① 表需要占用磁盤空間,視圖不需要
② 視圖不能添加索引
③ 使用視圖可以簡化復(fù)雜查詢
④ 視圖有利于提高安全性
創(chuàng)建視圖
create view my_view as select * fromSCOTT.EMP where sal <1000;
可以在最后帶上 with read only
刪除視圖
drop view my_view
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。