PL / SQL 프로 그래 밍 (2)
                                            
 8718 단어  보기자바 호출 저장 프로시저
                    
--  
drop table book;
create table book(
bookId number(10) primary key,
bookName varchar2(50),
publishHouse varchar2(50),
bookClass number(3) not null);
/*
   
  16-  book     ,  java    :
*/
--    :in       , out      ,          
create or replace procedure add_book(proBookId in number, proBookName in varchar2, proPublishHouse in varchar2, proBookClass in number) is
begin
  insert into book values(proBookId, proBookName, proPublishHouse, proBookClass);
end;
/*
--Java  
public class TestProcedure {
	public static void main(String[] args) {
		Connection conn = null;
		CallableStatement cs = null;
		
		try {
			//    
			Class.forName("oracle.jdbc.driver.OracleDriver");
			//    
			conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "dog", "dog");
			cs = conn.prepareCall("{call add_book(?, ?, ?)}");
			
			//    
			cs.setInt(1, 1);
			cs.setString(2, "Thinking in Java");
			cs.setString(3, "America Universe Publish");
      cs.setInt(4, 1);
	
			//  
			cs.execute();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				cs.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
*/
/*
  :         (      )
  17-      ,     、   
*/
create or replace procedure get_book(proBookId in number, proBookName out varchar2, proPublishHouse out varchar2) is
begin
  select bookName, publishHouse into proBookName, proPublishHouse from book where bookId = proBookId;
end;
/*
--Java  
public class TestProcedure {
	public static void main(String[] args) {
		Connection conn = null;
		CallableStatement cs = null;
		
		try {
			//    
			Class.forName("oracle.jdbc.driver.OracleDriver");
			//    
			conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "dog", "dog");
				
			cs = conn.prepareCall("{call get_book(?, ?, ?)");
      //      
			cs.setInt(1, 1);
			//      
			cs.registerOutParameter(2, oracle.jdbc.OracleTypes.VARCHAR);
			cs.registerOutParameter(3, oracle.jdbc.OracleTypes.VARCHAR);
			
			//  
			cs.execute();
			
			//     
			String bookName = cs.getString(2);
			String publishHouse = cs.getString(3);
			System.out.println("  :" + bookName + "  ---   :" + publishHouse);
		
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				cs.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
*/
/*
  :         (      (   ))
         ,       out  ,    package。
  18-      ,      
*/
--  book        
--insert into book values(1, 'Thinking in Java', 'America Universe Publish', '001');
insert into book values(2, 'Harry Potter', 'Titan Books Ltd', '001');
insert into book values(3, 'The Accident', 'Orion Publishing Co', '001');
--   :     
--         
create or replace package pak_ResultSet is
  type pak_cursor_type is ref cursor;
end;
--   :      
create or replace procedure pro_ResultSet(proBookClass in number, pak_cursor out pak_ResultSet.pak_cursor_type) is
begin
 --    
  open pak_cursor for select * from book where bookClass = proBookClass;
end;
/*Java  
public class TestProcedure {
	public static void main(String[] args) {
		Connection conn = null;
		CallableStatement cs = null;
		ResultSet rs = null;
		
		try {
			//    
			Class.forName("oracle.jdbc.driver.OracleDriver");
			//    
			conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "dog", "dog");	
			
			//            ,          
			cs = conn.prepareCall("{call pro_ResultSet(?, ?)}");
			cs.setInt(1, 1);
			//       
			cs.registerOutParameter(2, oracle.jdbc.OracleTypes.CURSOR);
			
			cs.execute();
			
			//    
			rs = (ResultSet) cs.getObject(2);
			
			while (rs.next()) {
				System.out.println("   :" + rs.getInt(1) + "  ---  :" + rs.getString(2) + "  ---   :" + rs.getString(3) + "  ---  :" + rs.getString(4));
			}
		
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				cs.close();
				conn.close();
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
*/  /*
     : no_data_found,           ,    
*/
/*
     : case_not_found
*/
create or replace procedure pro_caseException(EmployNum number) is
  v_sal emp.sal%type;
begin
  select sal into v_sal from emp where empno = EmployNum;
  case
    when v_sal < 1000 then
      update emp set sal = sal + 200 where empno = EmployNum;
    when v_sal < 2000 then
      update emp set sal = sal + 100 where empno = EmployNum;
      --        default  ,         case,     ,       exception
  end case;
  exception when case_not_found then
    dbms_output.put_line('case not matched');
end;
/*
     : cursor_already_open
*/
declare
  cursor emp_cursor is select ename, sal from emp;
begin
  open emp_cursor;
  for emp_record1 in emp_cursor loop
    dbms_output.put_line(emp_record1.ename);
  end loop;
  exception when cursor_already_open then
    dbms_output.put_line('Cursor has been opened!');
end;
/*
     : dup_val_on_index                   ,         
*/
begin
  insert into dept values(10, '   ', 'shanghai');
  
  exception when dup_val_on_index then
    dbms_output.put_line('Duplicated Deptno!');
end;
/*
     : invalid_cursor                  ,      (                 )
*/
declare
  cursor emp_cursor is select ename, sal from 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('Cursor has been closed!');
end;
/*
     : invalid_number           ,      
*/
begin
  update emp set sal=sal + 'abc';
  
  exception when invalid_number then
    dbms_output.put_line('Number error!');
end;
/*
     : too_many_rows     select into    ,         ,       
*/
declare
  v_ename emp.ename%type;
begin
  select ename into v_ename from emp;
  
  exception when too_many_rows then
    dbms_output.put_line('Return too many rows!');
end;
/*
     : zero_divide     2/0    ,       
*/
declare
  c_num number := 2;
begin
  c_num := c_num/0;
  
  exception when zero_divide then
    dbms_output.put_line('Zero cannot be divide!');
end;
/*
     : value_error           ,                ,       
*/
declare
  v_ename varchar2(5);
begin
  select ename into v_ename from emp where empno = 7654;
  
  exception when value_error then
    dbms_output.put_line('Value is too large for v_ename!');
end;
/*
       :
login_denide ---        ,      
not_logged_on ---          dml  ,,      
storage_error ---               ,,      
timeout_on_resource ---   oracle          ,,      
*/ b. 사용자 정의 예외: Oacle 의 오류 와 아무런 관련 이 없습니다. 개발 자가 특정 상황 에 정의 하 는 예외 입 니 다./*
     :
*/
create or replace procedure test_ModifiedException(EmployeeNo number) is
  --      
  myex exception;
begin
  update emp set sal = sal + 1000 where empno = EmployeeNo;
  --sql%notfound     update
  --raise myex;   myex   
  if sql%notfound then
    raise myex;
  end if;
  exception when myex then
    dbms_output.put_line('Myex has been occured!');
end;  보기 만 들 기: 보기 뷰 를 만 들 거나 대치 합 니 다. Name as select... [with read only]
보기 삭제: drop view viewName
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Swift3에서 UIView를 View의 중심에 배치하는 방법텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.