简体   繁体   中英

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:

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. 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.

JDBC does not support named parameters. If you are using Spring I would suggest to use Spring JDBCTemplate NamedParameterJdbcTemplate . It can be used without the whole IoC container.

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...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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