传递args计数的jdbcTemplate错误

时间:2017-06-22 19:21:46

标签: spring spring-mvc jdbctemplate

奇怪的JDBC TEMPLATE错误。

当您使用下面的代码对表执行更新时,我收到以下错误:

Org.springframework.jdbc.BadSqlGrammarException:PreparedStatementCallback; SQL语法不正确[UPDATE adminformulariocontratacao SET statusatual =? WHERE(formulariouuid =?)];嵌套异常是java.sql.SQLException:没有为参数2指定值

decimal

当我使用下面的修改运行代码时,它正常工作。有什么区别?

public void atualizaStatusAtendimentoContratacao(String formulariouuid,String statusGravar) {

    final String query = "UPDATE adminformulariocontratacao "
                      +  "SET statusatual = ? "                             
                      +  "WHERE (formulariouuid = ?)";

    Object argsAtualizaStatus = new Object[] { 
            statusGravar, 
            formulariouuid
        };

    try {
        jdbcTemplate.update(query, argsAtualizaStatus);
    } catch (DataAccessException e) {
        logger.log(Level.ERROR,
                "##ERRO ao Gravar atualização de status do atendimento contratacao:" + e.getLocalizedMessage());
    }

1 个答案:

答案 0 :(得分:0)

没有什么奇怪的:

源代码:

@Override
public int update(String sql, Object... args) throws DataAccessException {
    return update(sql, newArgPreparedStatementSetter(args));
}

以防:

  

Object argsAtualizaStatus = new Object [] {               statusGravar,               formulariouuid           };

type is Object

  

Object [] argsAtualizaStatus = new Object [] {               statusGravar,               formulariouuid           };

type是对象数组

当你这样做时:

  

jdbcTemplate.update(query,new Object [] {statusGravar,formulariouuid});

你给出了正确的类型 - 数组

在第一个示例中,您提供了对象,而jdbc模板将其用作Object而不是Object

的数组

修复:

Object [] argsAtualizaStatus = new Object [] {statusGravar,formulariouuid};