Oracle学习笔记(续)
admin
2023-05-13 21:42:49
0次

接上文 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



相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...