0

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?

1 Answer 1

0

I think it's omitting the 5th parameter because you have an additional parameter, new MapperClass.

Since you have all 5 parameters I don't think you can add new MapperClass() to it. Try omitting it.

Sign up to request clarification or add additional context in comments.

6 Comments

Please consider answering as if you're not sending a text message.
I need the mapper class in my query, guess the problem is sending the number of rows param in it.
I am sorry for being rude but I thought this forum is for helping people, but it looks like its for figuring out other people mistakes. FYI I was editing my answer.
yes, since your query is of 5 parameters you should only send 5 parameters only.But you are sending 6.
Answers written in text-speak aren't really what we're looking for, so I edited it. That's how SO works, and it's the responsibility of SO citizens to provide that information to newer answerers-that's what I did. Feedback is information, not an attack-being able to accept feedback is a critical soft skill and I'd encourage you to view it as such. The answer, however, is still incorrect, as the api docs indicate. Downvoting and/or commenting on incorrect answers is also important so future visitors recognize when a solution is wrong or sub-optimal.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.