为了账号安全,请及时绑定邮箱和手机立即绑定

使用JDBC从存储过程中获取Oracle表类型

使用JDBC从存储过程中获取Oracle表类型

汪汪一只猫 2019-12-04 10:55:39
我试图了解使用JDBC从Oracle存储过程/函数获取表数据的不同方法。六种方式如下:过程返回模式级表类型作为OUT参数程序返回包级表类型作为OUT参数程序返回包级游标类型作为OUT参数返回模式级表类型的函数返回包级表类型的函数返回包级游标类型的函数以下是PL / SQL中的一些示例:-- schema-level table typeCREATE TYPE t_type AS OBJECT (val VARCHAR(4));CREATE TYPE t_table AS TABLE OF t_type;CREATE OR REPLACE PACKAGE t_package AS  -- package level table type  TYPE t_table IS TABLE OF some_table%rowtype;  -- package level cursor type  TYPE t_cursor IS REF CURSOR;END library_types;-- and example procedures:CREATE PROCEDURE p_1 (result OUT t_table);CREATE PROCEDURE p_2 (result OUT t_package.t_table);CREATE PROCEDURE p_3 (result OUT t_package.t_cursor);CREATE FUNCTION f_4 RETURN t_table;CREATE FUNCTION f_5 RETURN t_package.t_table;CREATE FUNCTION f_6 RETURN t_package.t_cursor;我已经成功使用JDBC调用了3、4和6:// Not OK: p_1 and p_2CallableStatement call = connection.prepareCall("{ call p_1(?) }");call.registerOutParameter(1, OracleTypes.CURSOR);call.execute(); // Raises PLS-00306. Obviously CURSOR is the wrong type// OK: p_3CallableStatement call = connection.prepareCall("{ call p_3(?) }");call.registerOutParameter(1, OracleTypes.CURSOR);call.execute();ResultSet rs = (ResultSet) call.getObject(1); // Cursor results// OK: f_4PreparedStatement stmt = connection.prepareStatement("select * from table(f_4)");ResultSet rs = stmt.executeQuery();// Not OK: f_5PreparedStatement stmt = connection.prepareStatement("select * from table(f_5)");stmt.executeQuery(); // Raises ORA-00902: Invalid data type// OK: f_6CallableStatement call = connection.prepareCall("{ ? = call f_6 }");call.registerOutParameter(1, OracleTypes.CURSOR);call.execute();ResultSet rs = (ResultSet) call.getObject(1); // Cursor results所以很明显,我很难理解如何从存储过程中的OUT参数中检索模式级和包级表类型如何从存储的函数中检索包级表类型我似乎找不到任何文档,因为每个人都总是使用游标而不是表类型。也许是因为不可能?不过,我更喜欢表类型,因为它们是形式上定义的,并且可以使用字典视图(至少是架构级别的表类型)发现。注意:很明显,我可以编写一个包装函数,返回OUT参数和包级表类型。但是我更喜欢干净的解决方案。
查看完整描述

3 回答

?
慕哥6287543

TA贡献1831条经验 获得超10个赞

您也可以使用以下之一


public List<EmployeeBean> fetchDataFromSPForRM(String sInputDate) {


         List<EmployeeBean> employeeList = new ArrayList<EmployeeBean>();


         Connection dbCon = null;

         ResultSet data = null;

         CallableStatement cstmt = null;



         try {

                dbCon = DBUtil.getDBConnection();

                String sqlQuery = "{? = call PKG_HOLD_RELEASE.FN_RM_PDD_LIST()}";


                cstmt = dbCon.prepareCall(sqlQuery);


                cstmt.registerOutParameter(1, OracleTypes.CURSOR);


                cstmt.execute();


                data = (ResultSet) cstmt.getObject(1);              


                    while(data.next()){

                        EmployeeBean employee = new EmployeeBean();


                        employee.setEmpID(data.getString(1));

                        employee.setSubBusinessUnitId((Integer)data.getObject(2));

                        employee.setMonthOfIncentive((Integer)data.getObject(3));

                        employee.setPIPStatus(data.getString(5));

                        employee.setInvestigationStatus(data.getString(6));

                        employee.setEmpStatus(data.getString(7));

                        employee.setPortfolioPercentage((Integer)data.getObject(8));

                        employee.setIncentive((Double)data.getObject(9));

                        employee.setTotalSysemHoldAmt((Double)data.getObject(10));

                        employee.setTotalManualHoldAmt((Double)data.getObject(11));


                        employeeList.add(employee);

                    }


            } catch (SQLException e) {

                e.printStackTrace();

            }finally{

                try {


                    if(data != null){


                            data.close();               

                            data = null;

                    }

                    if(cstmt != null){


                        cstmt.close();

                        cstmt = null;

                    }

                    if(dbCon != null){


                            dbCon.close();              

                            dbCon = null;

                    }


                } catch (SQLException e) {

                    e.printStackTrace();

                }

            }



        return employeeList;                

     }


查看完整回答
反对 回复 2019-12-04
  • 3 回答
  • 0 关注
  • 890 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信