Arturo
Arturo

Reputation: 4180

Stored procedures call (namedparameterjdbctemplate)

I'm trying to call a stored procedure using jdbc. My connection is being passed through namedParameterJdbcTemplate and that's what I have to use to call it, but when I attempt to do this:

public void storedProcedure(long fileId, String Action) {

    String sql = "call procedureName(?)";

    try {
        namedParameterJdbcTemplate.update(sql, Long.valueOf(fileId) );
        
    } catch (Exception e) {
        logger.error("Error while running stored procedure  {}", sql, e);
    }
}

I get the following error:

Cannot resolve method 'update(java.lang.String, java.lang.Long)'

Sources I've tried looking at and can't make it work:

Most of them are creating a connection from the beginning but I already have it (namedParameterJdbcTemplate), also some are using datasource which I don't need because again, I have already the connection.

How can I make the call with namedParameterJdbcTemplate?

Thank you

Upvotes: 2

Views: 10608

Answers (2)

Arturo
Arturo

Reputation: 4180

This is how it works:

    final String sql = "call procedureName (:variable)";

    SqlParameterSource namedParameters = new MapSqlParameterSource("variable", variable);

    try {
        namedParameterJdbcTemplate.update(sql, namedParameters);
    } catch (Exception e){
        ...     
      }

Upvotes: 4

user2254180
user2254180

Reputation: 856

Your syntax looks wrong. When calling update using NamedParameterJdbcTemplate, you need to call the method with either of these two method signatures.

update(String sql, Map<String,?> paramMap)

or

update(String sql, SqlParameterSource paramSource)

For further information - https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplate.html

Further example here - https://netjs.blogspot.com/2016/11/insert-update-using-namedparameterjdbctemplate-spring.html

Upvotes: -1

Related Questions