简体   繁体   English

在Oracle数据库中对存储过程进行SimpleJDBC调用

[英]SimpleJDBC call for stored procedure in ORACLE database

I am trying to implement code which will have resultset using simpljdbccall or jdbccall or namedtemplatejdbc 我正在尝试使用simpljdbccall或jdbccall或namedtemplatejdbc实现将具有结果集的代码

Code will use my stored procedure which is database proc having input parameter and an REF cursor. 代码将使用我的存储过程,该过程是具有输入参数和REF游标的数据库proc。

I did not found any code which will help me to extract cursor as output to have all multiple rows details in in my resultset using JDBC 我没有找到任何可帮助我将游标提取为输出的代码,以使用JDBC在我的结果集中包含所有多行的详细信息

DATABASE PROCEDURE 数据库程序

BOOKING_PG.get_infant_info_pr(

    c_booking_id                      IN T_BOOKED_INFANT_INFO.BOOKING_ID%TYPE,
    c_booked_infant_details           OUT    booked_infant_details
)
  OPEN c_booked_infant_details  FOR
  SELECT
  BOOKING_ID                      c_booking_id,
  BOOKED_INFANT_INFO_ID             c_booked_infant_info_id,
  BOOKED_ADULT_PAX_INFO_ID      c_booked_adult_pax_info_id,

  FROM  T_BOOKED_INFANT_INFO T
  WHERE T.BOOKING_ID = c_booking_id
  and T.STATUS_ID = 1; 

JAVA Code JAVA代码

SimpleJdbcCall call = new SimpleJdbcCall(dataSource2)
                .withCatalogName("BOOKING_PG")
                .withProcedureName("get_infant_info_pr")
                .withoutProcedureColumnMetaDataAccess()
                .returningResultSet("rs1", new ParameterizedRowMapper() {
                    @Override
                    public Object[] mapRow(ResultSet rs, int rowNum) throws SQLException {
                        return new Object[]{rowNum, rs.getLong("c_booking_id"), rs.getLong(c_booked_infant_info_id) , rs.getLong(c_booked_adult_pax_info_id)};
                        }
                });
        SqlParameterSource in = new MapSqlParameterSource()
                .addValue(C_BOOKING_ID, bookingId);

        Map<String, Object> res = call.execute(in);

        List<Object[]> l1 = (List<Object[]>)res.get("rs1");

It is throwing SQL error 它抛出SQL错误

org.springframework.jdbc.BadSqlGrammarException: CallableStatementCallback; bad SQL grammar [{call EURONOVA.BOOKING_PG.GET_INFANT_INFO_PR(?)}]; nested exception is java.sql.SQLException: ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'GET_INFANT_INFO_PR'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored

I am not sure if there is something wrong in the code or we have to follow some different way to get resultset in simplejdbc call 我不确定代码中是否有错误,或者我们必须遵循一些不同的方法才能在simplejdbc调用中获取结果集

Can someone please help me in this topic? 有人可以帮我这个话题吗?

First create a class having similar data type and names which your cursor returns. 首先创建一个具有类似数据类型和游标返回名称的类。 In your case create a class named BookedInfantDetails.java 在您的情况下,创建一个名为BookedInfantDetails.java的类

public class BookedInfantDetails{

   private int bookingId;
   private int bookedInfantinfoId;
   private int bookedadultpaxinfoid;

   public int getBookingId() {
       return bookingId;
   }

   public void setBookingId(int bookingId) {
       this.bookingId = bookingId;
   }

   public int getBookedInfantinfoId() {
       return bookedInfantinfoId;
   }

   public void setBookedInfantinfoId(int bookedInfantinfoId) {
       this.bookedInfantinfoId = bookedInfantinfoId;
   }

   public int getBookedadultpaxinfoid() {
       return bookedadultpaxinfoid;
   }

   public void setBookedadultpaxinfoid(int bookedadultpaxinfoid) {
       this.bookedadultpaxinfoid = bookedadultpaxinfoid;
   }

} }

Now declare in and out parameters in the jdbccall and map the ref cursor to the class using beanproperty rowmapper. 现在,在jdbccall中声明in和out参数,并使用beanproperty rowmapper将ref游标映射到该类。

SimpleJdbcCall call = new SimpleJdbcCall(dataSource2)
            .withCatalogName("BOOKING_PG")
            .withProcedureName("get_infant_info_pr")
            .withoutProcedureColumnMetaDataAccess()
            .declareParameters(new SqlParameter("c_booking_id", OracleTypes.Integer),
             new SqlParameter("c_booked_infant_details", OracleTypes.CURSOR),
            .returningResultSet("c_booked_infant_details", 
             BeanPropertyRowMapper.newInstance(BookedInfantDetails.class));

    SqlParameterSource in = new MapSqlParameterSource()
            .addValue(C_BOOKING_ID, bookingId);

    Map<String, Object> res = call.execute(in);

    List<BookedInfantDetails> l1=
    (List<BookedInfantDetails>)res.get("c_booked_infant_details");

Find Our more details on spring docs.... https://docs.spring.io/spring-data/jdbc/old-docs/1.2.1.RELEASE/reference/html/orcl.datatypes.html#orcl.datatypes.ref_cur 在Spring docs上找到我们的更多详细信息。... https://docs.spring.io/spring-data/jdbc/old-docs/1.2.1.RELEASE/reference/html/orcl.datatypes.html#orcl.datatypes .ref_cur

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM