Oracle學(xué)習(xí)筆記(續(xù))

接上文 Oracle學(xué)習(xí)筆記

我們注重客戶提出的每個(gè)要求,我們充分考慮每一個(gè)細(xì)節(jié),我們積極的做好做網(wǎng)站、網(wǎng)站制作服務(wù),我們努力開拓更好的視野,通過不懈的努力,創(chuàng)新互聯(lián)贏得了業(yè)內(nèi)的良好聲譽(yù),這一切,也不斷的激勵著我們更好的服務(wù)客戶。 主要業(yè)務(wù):網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)站設(shè)計(jì),成都微信小程序,網(wǎng)站開發(fā),技術(shù)開發(fā)實(shí)力,DIV+CSS,PHP及ASP,ASP.Net,SQL數(shù)據(jù)庫的技術(shù)開發(fā)工程師。


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)建一個(gè)包,在包中定義類型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時(shí)觸發(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

           用戶非法登錄時(shí)觸發(fā)。

預(yù)定義異常not_logged_on

           如果用戶沒有登錄就執(zhí)行dml就會觸發(fā)。

預(yù)定義異常storage_error

           如果超出了內(nèi)存空間或是內(nèi)存被破壞就觸發(fā)。

預(yù)定義異常timeout_on_resorce

           如果Oracle在等待資源時(shí),出現(xiàn)了超時(shí)就觸發(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

標(biāo)題名稱:Oracle學(xué)習(xí)筆記(續(xù))
文章源于:http://bm7419.com/article22/igcicc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站網(wǎng)頁設(shè)計(jì)公司、營銷型網(wǎng)站建設(shè)、網(wǎng)站建設(shè)網(wǎng)站改版、全網(wǎng)營銷推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)