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

Posted 狐小仙

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JDBC操作存储过程存储函数out参数使用游标相关的知识,希望对你有一定的参考价值。

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+"	"+sal+"	"+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+"	"+sal);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            JDBCUtils.release(conn, call, null);
        }
    }

 

以上是关于JDBC操作存储过程存储函数out参数使用游标的主要内容,如果未能解决你的问题,请参考以下文章

使用JPA调用存储过程,返回存储多行数据的OUT参数(游标)

Oracle 存储过程中OUT参数的用法

如何通过 SQL Developer 执行带有游标和表 OUT 参数的存储过程?

Oracle存储过程,游标使用

MySQL数据库编程02

Java 和数据库:关于使用多个结果集和游标