码迷,mamicode.com
首页 > 数据库 > 详细

JDBC操作存储过程、存储函数、out参数使用游标

时间:2018-08-01 18:23:28      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:package   ...   out   mat   stc   into   dex   nvl   变量   

JDBC工具类

public class JDBCUtils {
    private static String driver = "oracle.jdbc.OracleDriver";
    private static String url = "jdbc:oracle:thin:@192.168.79.128:1521/orcl";
    private static String user = "scott";
    private static String password = "tiger";
    
    
    static{
        //注册驱动
        //DriverManager.registerDriver(driver)
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            throw new ExceptionInInitializerError(e);
        }
    }
    
    public static Connection getConnection(){
        try {
            return DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    public static void release(Connection conn,Statement st,ResultSet rs){
        if(rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }finally{
                rs = null; ///-----> 原因:Java GC: Java的GC不受代码的控制
            }
        }
        if(st != null){
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }finally{
                st = null;
            }
        }
        if(conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }finally{
                conn = null;
            }
        }
    }
}

存储过程

--存储过程
create or replace procedure queryEmpInformation(eno in number,
                                                pename out varchar2,
                                                psal out number,
                                                pjob out varchar2)
is
begin
  select ename,sal,job into pename,psal,pjob from emp where empno = eno;
end queryEmpInformation;

java代码

@Test
    public void testProcedure(){
        //jdbc调用存储过程sql语句:
        //{call <procedure-name>[(<arg1>,<arg2>, ...)]}
        String sql = "{call queryEmpInformation(?,?,?,?)}";
        
        Connection conn = null;
        CallableStatement call = null;
        
        try{
            conn = JDBCUtils.getConnection();
            call = conn.prepareCall(sql);
            
            //输入参数赋值
            call.setInt(1, 7839);
            
            //输出参数,声明为输出参数
            call.registerOutParameter(2, OracleTypes.VARCHAR);
            call.registerOutParameter(3, OracleTypes.NUMBER);
            call.registerOutParameter(4, OracleTypes.VARCHAR);
            
            //执行
            call.execute();
            
            //获取值
            String name = call.getString(2);
            double sal = call.getDouble(3);
            String job = call.getString(4);
            System.out.println(name+"\t"+sal+"\t"+job);
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            JDBCUtils.release(conn, call, null);
        }
    }

存储函数

--存储函数
create or replace function queryEmpIncome(eno in number)
return number
is
psal emp.sal%type;
pcomm emp.comm%type;
begin
  select sal,comm into psal,pcomm from emp where empno = eno;
  return psal*12+nvl(pcomm,0);
end queryEmpIncome;

java代码

@Test
    public void testFuntion(){
        //jdbc调用存储函数sql语句:
        //{?= call <procedure-name>[(<arg1>,<arg2>, ...)]}
        String sql = "{? = call queryEmpIncome(?)}";
        
        Connection conn = null;
        CallableStatement call = null;
        
        try{
            conn = JDBCUtils.getConnection();
            call = conn.prepareCall(sql);
            
            //注册输出参数
            call.registerOutParameter(1, OracleTypes.NUMBER);
            //输入参数
            call.setInt(2, 7839);
            
            //执行
            call.execute();
            
            //取出返回值
            double income = call.getDouble(1);
            System.out.println("年薪:"+income);
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            JDBCUtils.release(conn, call, null);
        }
    }

out参数游标

--创建包头
create or replace package mypackage 
is
--定义一个cursor类型变量
type empcursor is ref cursor;
procedure queryEmpList(dno in number,empList out empcursor);
end mypackage;


--创建包体
create or replace package body mypackage is
  procedure queryEmpList(dno in number,empList out empcursor)
    as
    begin
      open empList for select * from emp where deptno = dno;
      end;
end mypackage;

java代码

@Test
    public void testCursor(){
        String sql = "call mypackage.queryEmpList(?,?)";
        
        Connection conn = null;
        CallableStatement call = null;
        ResultSet rs = null;
        
        try{
            conn = JDBCUtils.getConnection();
            call = conn.prepareCall(sql);
            
            call.setInt(1, 20);
            call.registerOutParameter(2, OracleTypes.CURSOR);
            
            call.execute();
            
            rs = ((OracleCallableStatement)call).getCursor(2);
            while(rs.next()){
                String name = rs.getString("ename");
                double sal = rs.getDouble("sal");
                System.out.println(name+"\t"+sal);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            JDBCUtils.release(conn, call, null);
        }
    }

 

JDBC操作存储过程、存储函数、out参数使用游标

标签:package   ...   out   mat   stc   into   dex   nvl   变量   

原文地址:https://www.cnblogs.com/zsh-wj/p/9402486.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!