`

plsql package - example

    博客分类:
  • db
 
阅读更多

plsql package - example

 

an example to use plsql package/function/procedure of oracle,

 

include:

  • package/functipn/procedure define
  • sys_refcursor use
  • call from plsql
  • call from jdbc

------

table

 

create table customer(
	customer_id NUMBER(10),
	customer_name VARCHAR2(100),
	address varchar2(200),
	primary key(customer_id)
);

create table orders(
	order_id NUMBER(10),
	order_date DATE,
	order_name VARCHAR2(200),
	customer_id NUMBER(10),
	primary key(order_id)
);
 

------

data

 

-- customer
declare
  n CONSTANT NUMBER(4) := 5; -- row count
  i NUMBER(4);
begin
  for i in 1..n loop
	insert into customer values (i, concat('eric',i), 'shenzhen');
  end loop;
end;

-- orders
declare
  n CONSTANT NUMBER(4) := 5; -- customer row count
  i NUMBER(4);  -- customer id
  j NUMBER(2);
  order_id NUMBER(6) := 1;
begin
  for i in 1..n loop
    for j in 1..i loop
	insert into orders values (order_id, to_date('2010-3-21','YYYY-MM-DD'), concat('order_',order_id), i);
      order_id:=order_id+1;
    end loop;
  end loop;
end;
 

 

------

package

 

create or replace
PACKAGE pkg_orders_by_customer
AS
	procedure proc_orders_by_customer(p_customer_id in orders.customer_id%type, order_ref_cursor out SYS_REFCURSOR);
	function fun_order_count(p_customer_id in NUMBER) return NUMBER;
END;
  

------

package body

 

create or replace PACKAGE body pkg_orders_by_customer AS
	procedure proc_orders_by_customer(p_customer_id in orders.customer_id%type, order_ref_cursor out SYS_REFCURSOR)
	IS
	BEGIN
		open order_ref_cursor for
		select c.customer_id, o.order_id, o.order_name, o.order_date
		from customer c join orders o on c.customer_id=o.customer_id
		where o.customer_id = p_customer_id;      
	END proc_orders_by_customer;

	function fun_order_count(p_customer_id in NUMBER) return NUMBER
	IS
	order_count NUMBER;
	BEGIN
		select count(*) into order_count from orders o where o.customer_id=p_customer_id;
		return order_count;
	END fun_order_count;
END;
  

------

call function - plsql

 

set serveroutput on;

declare
  cid NUMBER(5);
  ocount NUMBER(5);
  n NUMBER(4):=5; -- customer count
begin
  for cid in 1..n loop
    ocount := pkg_orders_by_customer.fun_order_count(p_customer_id => cid);
    dbms_output.put_line('customer_id: ' || cid || ', order_count: ' || ocount);
  end loop;
end;
  

------

call procedure - plsql

 

set serveroutput on;

declare
  n NUMBER(4):=5; -- customer count
  cid NUMBER(5);
  v_cursor SYS_REFCURSOR;
  v_customer_id customer.customer_id%type;
  v_order_id orders.order_id%type;
  v_order_name orders.order_name%type;
  v_order_date orders.order_date%type;  
begin
  for cid in 1..n loop
    pkg_orders_by_customer.proc_orders_by_customer(p_customer_id => cid, order_ref_cursor => v_cursor);
    loop
      fetch v_cursor 
      into v_customer_id, v_order_id, v_order_name, v_order_date;
      EXIT WHEN v_cursor%NOTFOUND;
      dbms_output.put_line(v_customer_id || ', ' || v_order_id || ', ' || v_order_name || ', ' || v_order_date);
    end loop;
    close v_cursor;
    dbms_output.new_line();
  end loop;
end;
 

------

call function - jdbc

 

public static void functionTest() {
	try {
		Class.forName("oracle.jdbc.driver.OracleDriver");

		String url, user, password;
		url = "jdbc:oracle:thin:@localhost:1521:xe";
		user = "cumtspace";
		password = "abcdefg";

		// create connection
		Connection conn = DriverManager.getConnection(url, user, password);

		// create CallableStatement
		CallableStatement cstmt = conn.prepareCall("{?=call pkg_orders_by_customer.fun_order_count(?)}");
		cstmt.registerOutParameter(1, Types.INTEGER);
		int customer_id = 4;
		cstmt.setInt(2, customer_id);
		// execute
		cstmt.execute();
		// get out param
		Integer count = cstmt.getInt(1);
		System.out.println(customer_id + ", " + count);

		cstmt.close();
		conn.close();
	} catch (ClassNotFoundException e) {
		e.printStackTrace();
	} catch (SQLException e) {
		e.printStackTrace();
	}
}
 

------

call procedure - jdbc

 

public static void procedureTest() {
	try {
		Class.forName("oracle.jdbc.driver.OracleDriver");

		String url, user, password;
		url = "jdbc:oracle:thin:@localhost:1521:xe";
		user = "cumtspace";
		password = "abcdefg";

		// create connection
		Connection conn = DriverManager.getConnection(url, user, password);

		// create CallableStatement
		CallableStatement cstmt = conn.prepareCall("{call pkg_orders_by_customer.proc_orders_by_customer(?,?)}");

		// set in/out params
		int cid = 4;
		cstmt.setInt(1, cid);
		cstmt.registerOutParameter(2, OracleTypes.CURSOR);

		// execute
		cstmt.execute();

		// get ResultSet from out param
		ResultSet rs = (ResultSet) cstmt.getObject(2);

		// handle resultset
		int customer_id, order_id;
		String order_name;
		Date order_date;
		while (rs.next()) {
			customer_id = rs.getInt(1);
			order_id = rs.getInt(2);
			order_name = rs.getString(3);
			order_date = rs.getDate(4);

			System.out.format("%5d\t%5d\t%15s\t%s\n", customer_id, order_id, order_name, order_date);
		}

		// close
		rs.close();
		cstmt.close();
		conn.close();
	} catch (ClassNotFoundException e) {
		e.printStackTrace();
	} catch (SQLException e) {
		e.printStackTrace();
	}
}
 

------


 

分享到:
评论

相关推荐

    plsql-vf-dbfodbc.rar

    标题 "plsql-vf-dbfodbc.rar" 暗示了这个压缩包与PL/SQL(Oracle数据库的编程环境)以及Visual FoxPro(VFP)数据库系统有关,特别是涉及到在PL/SQL环境中通过ODBC(Open Database Connectivity)驱动程序访问DBF...

    plsqldev-9.0

    plsqldev-9.0.zip

    PLSQL Developer-dba

    PLSQL Developer-dba

    plsqldev-12.汉化版

    plsqldev-12.汉化版 Product Code:4t46t6vydkvsxekkvf3fjnpzy5wbuhphqz Serial Number:601769 password:xs374ca

    plsql资料--plsql资料

    PLSQL,全称为Procedural Language/SQL,是Oracle数据库提供的一个编程环境,结合了SQL(结构化查询语言)和过程式编程语言的特性。它主要用于编写与Oracle数据库交互的应用程序,尤其是数据库的后台处理部分。这个...

    plsql1504-x64 安装包

    PLSQL Developer是一款强大的Oracle数据库管理工具,主要用于编写、调试、执行和管理PL/SQL代码。这个"plsql1504-x64"安装包是针对64位操作系统的版本,1504可能指的是软件的特定版本号,通常包含了各种性能改进和新...

    01.plsql基础-变量.sql

    01.plsql基础-变量.sql

    PLSQL10-GGS(20131030).zip

    PLSQL,全称为Procedural Language/SQL,是Oracle数据库附带的一种编程语言,它将SQL语句嵌入到一种过程式的语言中,使得开发者能够更有效地管理和操作Oracle数据库。这个"PLSQL10-GGS(20131030).zip"文件是一个绿色...

    PLSQL Developer-12.rar

    PLSQL Developer是一款强大的Oracle数据库开发工具,专为数据库管理员、开发者和分析师设计,提供了一站式的解决方案,用于编写、测试和调试PL/SQL代码。它简化了与Oracle数据库的交互,提高了开发效率,使得数据库...

    精通Oracle10g PlSQL编程-2

    精通Oracle10g PlSQL编程-2

    PLSQL教材--语法手册.doc

    PLSQL,全称为Procedural Language/SQL,是Oracle数据库的一种扩展SQL语法,它结合了SQL的查询功能和过程性编程语言的特点,使得开发者能够编写更复杂的数据库操作和管理程序。以下是对PLSQL语法手册中提及的部分...

    PLSQL--ORACLE连接工具

    PLSQL(Procedural Language/Structured Query Language)是Oracle数据库的一种扩展,它为SQL语言添加了过程编程特性,使得在数据库管理与开发中能够编写复杂的逻辑代码。ORACLE连接工具则是用于与Oracle数据库进行...

    sonar-plsql-plugin-2.9.1.1051

    sonar-plsql-plugin-2.9.1.1051jar包,支持sonarqube对sql的扫描

    PLSQL 12.07-32位和64位含注册码和环境变量配置教程

    PLSQL Developer是一款强大的Oracle数据库管理工具,主要用于编写、调试、执行和管理PL/SQL程序单元。这个12.07版本的教程涵盖了32位和64位系统的安装与配置,包括注册码激活和环境变量设置,对于Oracle数据库开发者...

    plsql-web.rar--plsql-web.rar

    Web版的PLSQL工具,如"plsql-web.rar",旨在提供一种通过Web界面来远程访问和管理Oracle数据库的解决方案。 "plsql-web.rar"可能是一个包含PLSQL Web客户端的压缩文件,允许用户通过Web浏览器进行数据库开发、查询...

    plsql--无需安装oracle,无需安装oralce客户端

    解压后,打开plsqldev.exe,再打开keygen.exe进行注册,注册完后,点取消进入主界面,然后打开tools-->Connection,Oracle Home设置为“解压目录\oracleclient”,OCI library设置为“解压目录\oracleclient\oci.dll...

    plsql12-32位+64位-附注册码9999

    "plsql12-32位+64位-附注册码9999"指的是PL/SQL Developer的第12个版本,同时提供了适用于32位和64位操作系统的安装程序。这表明用户可以在不同的计算机系统环境下使用该工具。附带的注册码“9999”是激活软件的密钥...

    Oracle PLSQL编程-经典试题

    里面有orcale plsql经典试题,视图,存储过程,游标,包,异常处理。

    PLSQL笔记-从hello word到触发器,包,游标高级应用

    PLSQL,全称为Procedural Language/SQL,是Oracle数据库提供的结构化查询语言扩展,它结合了SQL(Structured Query Language)的查询功能和过程性编程语言的控制结构,用于开发在Oracle环境中运行的应用程序。...

Global site tag (gtag.js) - Google Analytics