0

运行此代码时出现此未知错误:

String SELECT_getWhenValueChanged = "SELECT a.* FROM status AS a WHERE a.value <> (SELECT b.value FROM status AS b WHERE a.idCategory = ? AND b.idCategory = a.idCategory and a.timeStamp > b.timeStamp ORDER BY b.timeStamp DESC LIMIT 1) ORDER BY timeStamp DESC";
    try {
        Survey surveyTemp = new Survey();
        surveyTemp = (Survey) getJdbcTemplate()
                .queryForObject(SELECT_getWhenValueChanged,
                        new Object[] { categoryId, categoryId },
                        new SurveyMapper());
        /*
         * SQL Question ask when the value has changed and get the value
         * that changed from and changed to, the first one is the current
         * value and the second is the value before it changed
         */

        if (!surveyTemp.getTimestamp().isEmpty()
                && !presentSurvey.getTimestamp().isEmpty()) {
            presentSurvey.setTimestamp(surveyTemp.getTimestamp());
        }
    } catch (BadSqlGrammarException e) {
        e.printStackTrace();
    } catch (EmptyResultDataAccessException e) {

    } catch (Exception e) {
        e.printStackTrace();
    }
    return presentSurvey;

有人知道这是什么意思吗?

org.springframework.dao.TransientDataAccessResourceException: PreparedStatementCallback; SQL [SELECT a.* FROM status AS a WHERE a.value <> (SELECT b.value FROM status AS b WHERE a.idCategory = ? AND b.idCategory = a.idCategory and a.timeStamp > b.timeStamp ORDER BY b.timeStamp DESC LIMIT 1) ORDER BY timeStamp DESC]; Parameter index out of range (2 > number of parameters, which is 1).; nested exception is java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 1).

我不擅长SQL,所以不知道如何解决这个问题......

4

3 回答 3

1

由于您的 中只有一个参数PreparedStatement,因此您不应将两个参数传递给它。也许你可以new Object[] { categoryId, categoryId }换成new Object[] { categoryId }.

于 2013-08-15T11:15:33.433 回答
0

将其更改为

        String SELECT_getWhenValueChanged = "SELECT a.* FROM status AS a WHERE a.idCategory = ? AND a.value = ? AND a.timeStamp > (SELECT b.timeStamp FROM status AS b WHERE b.idCategory = ? and b.value <> ? ORDER BY timeStamp DESC LIMIT 1) ORDER BY timeStamp ASC LIMIT 1";
    try {
        Survey surveyTemp = (Survey) getJdbcTemplate().queryForObject(SELECT_getWhenValueChanged,
                        new Object[] { categoryId, presentSurvey.getValue(), categoryId, presentSurvey.getValue() },
                        new SurveyMapper());

那行得通!谢谢

于 2013-08-15T12:37:21.703 回答
-1

// 在 Spring DAO 模块中使用 On 时会引发这些异常

// Correct Code
public StudentRegistrationBean select(int studentId) {

    StudentRegistrationBean queryForObject = jdbcTemplate.queryForObject(
             "SELECT * FROM STUDENTREGISTRATION WHERE STUDENTID=?", new Object[] { studentId },
            new BeanPropertyRowMapper<StudentRegistrationBean>(
                    StudentRegistrationBean.class));
    return queryForObject;

// Wrong Code
public StudentRegistrationBean select(int studentId) {

    StudentRegistrationBean queryForObject = jdbcTemplate.queryForObject(
            "SELECT * FROM STUDENTREGISTRATION WHERE STUDENTID=?", new Object[] { studentId },
            new BeanPropertyRowMapper<StudentRegistrationBean>(
                    StudentRegistrationBean.class));
    return null;

结论: 如果您正在重新调整值,您可能会误将“null”类型的值...

于 2014-12-04T19:23:21.953 回答