What is the correct way to call stored procedures using the modern day (circa 2012) Spring JDBC Template?
Say I have a stored procedure that declares an IN and OUT parameter, something like this:
mypkg.doSomething( id OUT int, name IN String, date IN Date )
I came across CallableStatementCreator based CallableStatementCreator where we must explicitly register the IN and OUT parameters. Consider the following method in the JdbcTemplate class:
public Map<String, Object> call(CallableStatementCreator csc, List<SqlParameter> declaredParameters)
Of course, I know that I can use it like this:
List<SqlParameter> declaredParameters = new ArrayList<SqlParameter>(); declaredParameters.add(new SqlOutParameter("id", Types.INTEGER)); declaredParameters.add(new SqlParameter("name", Types.VARCHAR)); declaredParameters.add(new SqlParameter("date", Types.DATE)); this.jdbcTemplate.call(new CallableStatementCreator() { @Override CallableStatement createCallableStatement(Connection con) throws SQLException { CallableStatement stmnt = con.createCall("{mypkg.doSomething(?, ?, ?)}"); stmnt.registerOutParameter("id", Types.INTEGER); stmnt.setString("name", "<name>"); stmnt.setDate("date", <date>); return stmnt; } }, declaredParameters);
What is the purpose of declaredParameters when I already register them in my csc implementation? In other words, why do I need to switch to csc when Spring can just do con.prepareCall(sql) internally? Basically, I can’t get through in either of them, or in both of them?
Or is there a much better way to invoke stored procedures (using the Spring JDBC Template) than what I have come across so far?
Note. You may find many questions that appear to have a similar heading, but they do not match this.
java spring spring-jdbc jdbctemplate
adarshr Feb 20 2018-12-12T00: 00Z
source share