【问题标题】:Stored procedure parameter names in a PreparedStatement instead of parameter indexes?PreparedStatement 中的存储过程参数名称而不是参数索引?
【发布时间】:2015-08-06 11:10:21
【问题描述】:

现在,当我在 PreparedStatement 中调用存储过程时,我必须将参数添加到 CallableStatement,如下所示:

procStmt.setString(1, "a");
procStmt.setString(2, "b");
procStmt.setString(3, "c");
// etc...

一旦您调用的存储过程中有大约 50 多个参数并且并非所有参数都是必需的,这会变得非常乏味。有没有办法宁愿这样做:

procStmt.setString("myParameter", "a");
procStmt.setString("yourParameter", "b");
procStmt.setString("ourParameter", "c");
// etc...

通过这种方式,参数直接传递给需要它们的参数,您不必担心通过参数发送的顺序或需要发送空的String 对象等。

【问题讨论】:

    标签: java jdbc prepared-statement


    【解决方案1】:

    你为什么使用 PreparedStatement.. 你可以使用 CallableStatement 来调用存储过程,并且 Callable Statement 提供了通过参数名称设置值的选项...

    请参考http://docs.oracle.com/javase/7/docs/api/java/sql/CallableStatement.html#setString(java.lang.String,%20java.lang.String)

    【讨论】:

    • 啊!我知道有这样的事情!非常感谢!
    【解决方案2】:

    JDBC 不支持命名参数。如果您使用的是 Spring,我建议您使用 Spring JDBCTemplate NamedParameterJdbcTemplate。它可以在没有整个 IoC 容器的情况下使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多