使用Spring JDBC选择查询

时间:2014-12-12 17:56:23

标签: database spring-jdbc

我正在使用Spring JDBC模板在表上执行select语句并得到此错误:

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

查询:

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";

调用查询:

template.query(qry , new Mapperclass(),
            error.getStartDate(), error.getEndDate(), error.getReasonPattern(), exception.getSource(),
            maxNumberOfDetails);

查询看起来很好,如果我使用FETCH FIRST N ROWS的硬编码值并忽略查询中的第5个参数,我就不会收到此错误。例如:

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";

 template.query(qry , new Mapperclass(),
            error.getStartDate(), error.getEndDate(), error.getReasonPattern(), exception.getSource());

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

我认为它省略了第5个参数,因为你有一个额外的参数new MapperClass

由于您拥有所有5个参数,我认为您不能向其添加new MapperClass()。尝试省略它。