Why am I obtaining this error trying to perform JdbcTemplate query using prepared statement in a Spring Boot application?

AndreaNobili :

it is long time that I am not using Spring and at the moment I am finding some difficulties trying to execute a query using prepared statement. The query result is a single row in my database (MariaDB).

I wrote this method into a @Repository class:

@Override
public TagDTO findTagByTagIdentifier(String tagIdentifier) {
    TagDTO tagDto = new TagDTO();
    String SELECT_BY_TAG_IDENTIFIER = "SELECT * FROM Tags WHERE TagIdentifier = :tagIdentifier";

    TagDTO result = (TagDTO) jdbcTemplate.query(SELECT_BY_TAG_IDENTIFIER, 
                                                new Object[] { tagIdentifier }, 
                                                BeanPropertyRowMapper.newInstance(TagDTO.class));

    return result;
}

As you can see I specify the where condition parameter with the :tagIdentifier param and I am trying to replace it into the query() method passing new Object[] { tagIdentifier } as method parameter.

But this is not working.

When I perform my unit test it fails and in my stacktrace I am obtaining only this error message:

org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized SQLException for SQL [SELECT * FROM Tags WHERE TagIdentifier = :tagIdentifier]; SQL state [null]; error code [0]; Could not set parameter at position 1 (values vas 'Y_KT8_401_ETA_GTCOMP'); nested exception is java.sql.SQLException: Could not set parameter at position 1 (values vas 'Y_KT8_401_ETA_GTCOMP')
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:90)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:82)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:82)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:655)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:690)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:722)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:732)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:782)
    at com.springboot.excelapi.repository.TagRepository.findTagByTagIdentifier(TagRepository.java:32)
    at com.springboot.excelapi.repository.TagRepository$$FastClassBySpringCGLIB$$d21d6de9.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673)
    at com.springboot.excelapi.repository.TagRepository$$EnhancerBySpringCGLIB$$f8b080ad.findTagByTagIdentifier(<generated>)
    at com.springboot.excelapi.service.TagService.findTagByTagIdentifier(TagService.java:26)
    at com.springboot.excelapi.integration.ServiceTest.findTagByTagIdentifier(ServiceTest.java:43)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:542)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
Caused by: java.sql.SQLException: Could not set parameter at position 1 (values vas 'Y_KT8_401_ETA_GTCOMP')
    at org.mariadb.jdbc.internal.util.ExceptionMapper.getSqlException(ExceptionMapper.java:163)
    at org.mariadb.jdbc.MariaDbClientPreparedStatement.setParameter(MariaDbClientPreparedStatement.java:415)
    at org.mariadb.jdbc.AbstractPrepareStatement.setString(AbstractPrepareStatement.java:1230)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(StatementFacade.java:114)
    at com.sun.proxy.$Proxy89.setString(Unknown Source)
    at org.springframework.jdbc.core.StatementCreatorUtils.setValue(StatementCreatorUtils.java:453)
    at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValueInternal(StatementCreatorUtils.java:241)
    at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValue(StatementCreatorUtils.java:172)
    at org.springframework.jdbc.core.ArgumentPreparedStatementSetter.doSetValue(ArgumentPreparedStatementSetter.java:66)
    at org.springframework.jdbc.core.ArgumentPreparedStatementSetter.setValues(ArgumentPreparedStatementSetter.java:47)
    at org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:696)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:639)
    ... 44 more

For completeness: in the same class I have a similar method performing a similar query that have no where condition (and returning multiple rows) and it works fine.

So, what is wrong with my code? What am I missing? How can I fix it to correctly perform my query?

Marco Behler :

It is because you are mixing named and positional JDBC parameters. JdbcTemplate only supports positional parameters.

So with JDBCTemplate either do this:

String SELECT_BY_TAG_IDENTIFIER = "SELECT * FROM Tags WHERE TagIdentifier = ?";

TagDTO result = (TagDTO) jdbcTemplate.query(SELECT_BY_TAG_IDENTIFIER, 
                                            new Object[] { tagIdentifier }, 
                                            BeanPropertyRowMapper.newInstance(TagDTO.class));

Or use a NamedJdbcParameterTemplate and do this:

SqlParameterSource namedParameters = new MapSqlParameterSource().addValue("tagIdentifier", 1);

String SELECT_BY_TAG_IDENTIFIER = "SELECT * FROM Tags WHERE TagIdentifier = :tagIdentifier";

TagDTO result = (TagDTO) namedParamJdbcTemplate.query(SELECT_BY_TAG_IDENTIFIER, 
                                            namedParameters,  BeanPropertyRowMapper.newInstance(TagDTO.class)}, 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=23194&siteId=1