博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
oracle 应用程序调用存储函数
阅读量:4549 次
发布时间:2019-06-08

本文共 5131 字,大约阅读时间需要 17 分钟。

package com.founder.ec.common.lucene;import java.sql.CallableStatement;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import oracle.jdbc.internal.OracleCallableStatement;import oracle.jdbc.internal.OracleTypes;public class ReadMysql {    public static Connection getConnection() throws Exception {        String url = "jdbc:oracle:thin:@192.168.";        Class.forName("oracle.jdbc.driver.OracleDriver");        String userName = "";        String password = "";        Connection con = DriverManager.getConnection(url, userName, password);        return con;    }    /**     * CREATE  PROCEDURE member_procedure_out(memberId in NUMBER,realname OUT  VARCHAR2,mobile_phone OUT VARCHAR2)     *///释放数据库资源    public static void release(Connection con, CallableStatement call, ResultSet res) {        if (res != null || call != null || con != null) {            try {                res.close();                con.close();                call.close();            } catch (Exception e) {                e.printStackTrace();            } finally {                res = null;                call = null;                res = null;            }        }    }    public static void main(String[] args) {        /**         *         CREATE  PACKAGE mypackage AS         TYPE empcursor is REF CURSOR ;         PROCEDURE queryEmpList(memberId in NUMBER,empList OUT empcursor);         END mypackage;         */        try {            //调用存储过程,根据会员id获得会员name             // testpro();            //调用存储函数,根据会员id计算其年收入            //testfunction();            //调用包下的存储过程,游标            testCursor();        } catch (Exception e) {            e.printStackTrace();        } finally {            // release(con, call, null);        }    }    private static void testCursor() {        Connection con = null;        CallableStatement call = null;        try {            String sql = "{call mypackage.queryEmpList(?,?)}";            con = getConnection();            call = con.prepareCall(sql);            call.setInt(1,7923);            call.registerOutParameter(2,OracleTypes.CURSOR);//光标            call.execute();           //取出该会员的所有信息            ResultSet res = ((OracleCallableStatement) call).getCursor(2);         while (res.next()){             String name=res.getString("real_name");             int memberId=res.getInt("member_id");             System.out.print(name+memberId);         }        } catch (Exception e) {            e.getMessage();        }    }    private static void testfunction() {        Connection con = null;        CallableStatement call = null;        try {            String sql = "{?=call query_member(?)}";            con = getConnection();            call = con.prepareCall(sql);            call.registerOutParameter(1,OracleTypes.NUMBER);            call.setInt(2,7923);            call.execute();            double sum=call.getDouble(1);            System.out.println(sum);        } catch (Exception e) {            e.getMessage();        }    }    private static void testpro() {        Connection con = null;        CallableStatement call = null;        try {            String sql = "{call member_procedure_out(?,?)}";            con = getConnection();            call = con.prepareCall(sql);            call.setInt(1, 7923);            call.registerOutParameter(2, OracleTypes.VARCHAR);        } catch (Exception e) {            e.getMessage();        }        try {            call.execute();            String realName = call.getString(2);            System.out.println(realName);        } catch (Exception e) {            e.printStackTrace();        }    }}
create PROCEDURE raise_order_price_procedure(orderId in NUMBER )  as  psal ORDER_INFO.paid_fee%TYPE ;  BEGIN    select paid_fee into psal from ORDER_INFO where ORDER_ID=orderId;    update ORDER_INFO set paid_fee =paid_fee+100 where ORDER_ID=orderId; dbms_output.put_line('涨前:'||psal||'涨后:'||(psal+100));  END;drop  PROCEDURE member_procedure_out;  CREATE  PROCEDURE member_procedure_out(memberId in NUMBER,realname OUT  VARCHAR2)  AS    BEGIN      select  m.REAL_NAME into realname from member m where m.MEMBER_ID=memberId;    END;/--BEGIN  raise_order_price_procedure(102467);END;  CREATE  FUNCTION query_member(memberId in NUMBER)    RETURN NUMBER    AS      psal member.account_fee%type;      pcomm member.sum_points%type;    BEGINselect  account_fee , sum_points INTO psal,pcomm from MEMBER where MEMBER_ID=memberId;      RETURN psal*12+nvl(pcomm,0);    END;  drop PACKAGE mypackage;CREATE  PACKAGE mypackage AS  TYPE empcursor is REF CURSOR ;  PROCEDURE queryEmpList(memberId in NUMBER,empList OUT empcursor);  END mypackage;create or replace  PACKAGE  BODY mypackage AS  PROCEDURE queryEmpList(memberId in NUMBER,empList OUT empcursor)as    BEGIN      OPEN empList for select * from MEMBER  where MEMBER_ID=memberId;    END queryEmpList;  END mypackage; create package emp_pkg is procedure emp_update_ename(v_empno varchar2,v_ename varchar2); function emp_get_sal(v_empno varchar2) return number; end;select * from  member where  MEMBER_ID=7923

 

转载于:https://www.cnblogs.com/wangchuanfu/p/10963282.html

你可能感兴趣的文章
23种设计模式中的命令模式
查看>>
[转载]年薪10w和年薪100w的人,差在哪里?
查看>>
shell 日期参数
查看>>
尼姆游戏(吃花生米问题)
查看>>
最小瓶颈路
查看>>
PHP isset()与empty()的使用区别详解
查看>>
Android自定义控件(五)自定义Dialog QuickOptionDialog
查看>>
初学java之面板布局的控制
查看>>
简单的验证码识别(opecv)
查看>>
一款基于jQuery的图片分组切换焦点图插件
查看>>
Python学习-字符串函数操作3
查看>>
MySQL存储二进制数据
查看>>
万网博通NMSS平台二次开发(UDP方式传输)
查看>>
Python爬虫入门 1 Python环境的安装
查看>>
iOS中GCD的使用小结
查看>>
HTML 之 Web页面表单form中只有一个input的text元素,按回车默认提交
查看>>
[BZOJ2870]最长道路tree
查看>>
存储过程简单的动态订单号
查看>>
关于从jsp 中 引用 js 中的里层function
查看>>
读取当前配置文件的方法
查看>>