简体   繁体   English

存储过程参数名称在PreparedStatement中而不是参数索引中?

[英]Stored procedure parameter names in a PreparedStatement instead of parameter indexes?

Right now when I call a stored procedure in a PreparedStatement I have to add the parameters to the CallableStatement like so: 现在,当我在PreparedStatement调用存储过程时,必须将参数添加到CallableStatement如下所示:

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

Which can get pretty tedious once you have about 50+ parameters in the stored procedure you're calling and not all of them are required. 一旦您正在调用的存储过程中有大约50个以上的参数,并且不需要全部,那么这将变得非常繁琐。 Is there a way to rather do it like this: 有没有办法像这样做:

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

This way the arguments gets passed directly to the parameters that require them and you don't have to worry about the order you send through the arguments or needing to send empty String objects etc. 这样,参数就直接传递给需要它们的参数,而您不必担心通过参数发送的顺序或发送空String对象等的String

JDBC does not support named parameters. JDBC不支持命名参数。 If you are using Spring I would suggest to use Spring JDBCTemplate NamedParameterJdbcTemplate . 如果您使用的是Spring,则建议使用Spring JDBCTemplate NamedParameterJdbcTemplate It can be used without the whole IoC container. 它可以在没有整个IoC容器的情况下使用。

why are you using PreparedStatement.. you can use CallableStatement to invoke a stored procedure and Callable Statement provides option to set values by parameter name... 为什么要使用PreparedStatement ..您可以使用CallableStatement调用存储过程,而Callable Statement提供了通过参数名称设置值的选项。

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

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

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