java - Spring SimpleJdbcCall 默认(可选)参数

标签 java spring stored-procedures jdbc

我正在尝试调用具有默认(可选)参数但不传递它们的存储过程,但它不起作用。与描述的问题基本相同 here .

我的代码:

  SqlParameterSource in = new MapSqlParameterSource()
        .addValue("ownname", "USER")
        .addValue("tabname", cachedTableName)
        .addValue("estimate_percent", 20)
        .addValue("method_opt", "FOR ALL COLUMNS SIZE 1")
        .addValue("degree", 0)
        .addValue("granularity", "AUTO")
        .addValue("cascade", Boolean.TRUE)
        .addValue("no_invalidate", Boolean.FALSE)
        .addValue("force", Boolean.FALSE);

我得到一个异常(exception):

Caused by: org.springframework.dao.InvalidDataAccessApiUsageException: Required input parameter 'PARTNAME' is missing
    at org.springframework.jdbc.core.CallableStatementCreatorFactory$CallableStatementCreatorImpl.createCallableStatement(CallableStatementCreatorFactory.java:209)

其中 PARTNAME 是根据 this 的可选参数.我可以在没有 PARTNAME 参数的情况下手动运行此过程这一事实也证实了这一点。

最佳答案

Ater 放弃了这个问题,只是传递了所有参数,包括可选参数,我遇到了它无法传递 boolean 参数的问题,因为 boolean 不是 SQL 数据类型,只是 PL/SQL。

所以我目前的解决方案是 JDBC 不适合运行存储过程,这就是我解决它的方法:

  jdbcTemplate.execute(
        new CallableStatementCreator() {
           public CallableStatement createCallableStatement(Connection con) throws SQLException{
              CallableStatement cs = con.prepareCall("{call sys.dbms_stats.gather_table_stats(ownname=>user, tabname=>'" + cachedMetadataTableName + "', estimate_percent=>20, method_opt=>'FOR ALL COLUMNS SIZE 1', degree=>0, granularity=>'AUTO', cascade=>TRUE, no_invalidate=>FALSE, force=>FALSE) }");
              return cs;
           }
        },
        new CallableStatementCallback() {
           public Object doInCallableStatement(CallableStatement cs) throws SQLException{
              cs.execute();
              return null; // Whatever is returned here is returned from the jdbcTemplate.execute method
           }
        }
  );

关于java - Spring SimpleJdbcCall 默认(可选)参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13162043/

相关文章:

java - 将 Intent 从 Activity 传递到扩展应用程序的类

java - 在javafx gridpane中添加不可见行

spring - 注释数组作为注释的参数,在 Scala 中

java - spring是否提供2个mvc平台,grails和spring mvc?

.net - 在 SQL Server 2005 存储过程中使用 .NET Framework 3.5

java - Spring 安全 : How Spring security registers new session in SessionRegistry?

java - Xpath - 如何获取元素之间包含的数据,而不是元素本身

java - 有什么方法可以覆盖 spring data jpa 存储库的 bean 定义吗?

mysql - SQL:返回具有匹配百分比计算列的用户表?

在表值函数中使用#table/Temp 表的 SQL