SimpleJdbcCall调用函数

问题描述

我有一个名为GET_RISK_GROUP的oracle函数

当我尝试调用函数时:

SimpleJdbcCall jdbcCall = new SimpleJdbcCall(jdbcTemplate)
                .withSchemaName("NEWIB")
                .withCatalogName("PKG_ONLINE_IB_PC_OPERATIONS")
                .withFunctionName("GET_RISK_GROUP");
        sqlParameterSource source = new MapsqlParameterSource().addValue("P_TAX_NUMBER",taxnumber);
        jdbcCall.executeFunction(String.class,source);

我得到异常:

2020-09-11 15:40:25.692 ERROR 1276 --- [nio-8698-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing Failed; nested exception is org.springframework.jdbc.UncategorizedsqlException: CallableStatementCallback; uncategorized sqlException for sql [{? = call GET_RISK_GROUP()}]; sql state [99999]; error code [17041]; Missing IN or OUT parameter at index:: 1; nested exception is java.sql.sqlException: Missing IN or OUT parameter at index:: 1] with root cause

找不到任何解决方案。有任何想法吗?由于这个问题,我将代码更改为:

jdbcTemplate.execute(
                con -> {
                    CallableStatement cs = con.prepareCall("{? = call NEWIB.PKG_ONLINE_IB_PC_OPERATIONS.GET_RISK_GROUP(?)}");
                    cs.registerOutParameter(1,Types.NVARCHAR); // or whatever type your function returns.
                    // Set your arguments
                    cs.setString(2,taxnumber);
                    return cs;
                },(CallableStatementCallback<String>) cs -> {
                    cs.execute();
                    String result = cs.getString(1);
                    return result; // Whatever is returned here is returned from the jdbcTemplate.execute method
                }
        );

这很好。

解决方法

有了这方面的经验,我只能说只作一些标准更改,然后尝试一下,因为相同的代码可以在您的机器上工作。记不清了,但是在一个问题中提到了jdbc驱动程序可以有所作为。

我之所以添加withoutProcedureColumnMetaDataAccess,是因为SimpleJdbcCall喜欢经常查询列详细信息的元数据,并且在以后有更多此类调用时为避免性能问题,建议添加它。

对于函数调用,我们还需要注册(如何使用CallableStatement或将其声明为将用作返回类型的第一个参数。

我希望下面的作品有效,

SimpleJdbcCall jdbcCall = new SimpleJdbcCall(jdbcTemplate)
                .withSchemaName("NEWIB")
                .withCatalogName("PKG_ONLINE_IB_PC_OPERATIONS")
                .withFunctionName("GET_RISK_GROUP")
                .withoutProcedureColumnMetaDataAccess()
                .declareParameters(new SqlOutParameter("return",Types.VARCHAR),new SqlParameter("p_tax_number",Types.VARCHAR));
        
// add value to the paramaters                         
SqlParameterSource parameterMap = new MapSqlParameterSource().addValue("p_tax_number",taxNumber);
        
// call
String result = jdbcCall.executeFunction(String.class,source);

P.S。 如果不起作用,您可以在问题中发布函数正文,还是运行以下查询并将结果粘贴到此处

select object_name,argument_name,position,data_type,data_length,in_out 
from   user_arguments
where  OBJECT_NAME ='GET_RISK_GROUP' 
and    Package_name='PKG_ONLINE_IB_PC_OPERATIONS'