Wednesday, October 5, 2011

Calling stored procedure from JdbcTemplate

I am using JdbcTemplate to make database queries, like:
List<Map<String,Object>> rows = jdbcTemplate.queryForList("select * from rows");
Recently I had to replace SQL selects with stored procedures, but after doing so, I had exception:
java.sql.SQLException: The SQL statement must not contain a procedure call or parameter markers.
        at net.sourceforge.jtds.jdbc.JtdsStatement.executeQuery(
        at com.mchange.v2.c3p0.impl.NewProxyStatement.executeQuery(
        at org.springframework.jdbc.core.JdbcTemplate$1QueryStatementCallback.doInStatement(
        at org.springframework.jdbc.core.JdbcTemplate.execute(
        at org.springframework.jdbc.core.JdbcTemplate.query(
        at org.springframework.jdbc.core.JdbcTemplate.query(
        at org.springframework.jdbc.core.JdbcTemplate.queryForList(
As I found out finally, when queryForList is called without parameters it cann't call stored procedures, so to do it you just need to add empty parameter list:
List<Map<String,Object>> tables = jdbcTemplate.queryForList("{call getRows() }", new Object[] {});

No comments:

Post a Comment