I am executing a select statement on a table using Spring JDBC template and get this error:
org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [SELECT * FROM EXCEPTION WHERE CREATED_DATE BETWEEN ? AND ? AND REASON LIKE ? AND SOURCE LIKE ? ORDER BY CREATED_DATE DESC FETCH FIRST ? ROWS ONLY]; nested exception is com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=?;ATE DESC FETCH FIRST;<space>, DRIVER=3.59.81
Query:
String qry = "SELECT * FROM ERROR" +
" WHERE CREATED_DATE BETWEEN ? AND ?" +
" AND REASON LIKE ?" +
" AND SOURCE LIKE ?" +
" ORDER BY CREATED_DATE DESC" +
" FETCH FIRST ? ROWS ONLY";
call the query:
template.query(qry , new Mapperclass(),
error.getStartDate(), error.getEndDate(), error.getReasonPattern(), exception.getSource(),
maxNumberOfDetails);
The query looks fine and I don't get this error if I use hard coded value for FETCH FIRST N ROWS and omit the 5th param from the query. Example:
String qry = "SELECT * FROM ERROR" +
" WHERE CREATED_DATE BETWEEN ? AND ?" +
" AND REASON LIKE ?" +
" AND SOURCE LIKE ?" +
" ORDER BY CREATED_DATE DESC" +
" FETCH FIRST 5 ROWS ONLY";
and
template.query(qry , new Mapperclass(),
error.getStartDate(), error.getEndDate(), error.getReasonPattern(), exception.getSource());
Can someone please help me?