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

にはパラメーターが1 つしかないため、 2 つのPreparedStatementパラメーターを渡す必要はありません。に着替えてもいいかもしれません。new Object[] { categoryId, categoryId }new Object[] { categoryId }

于 2013-08-15T11:15:33.433 に答える
0

changed it to

        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());

And that worked! thanks

于 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」タイプの値を間違える可能性があります...つまり、何らかの間違いを犯すメソッドの記述(OR)はnull値を返します

于 2014-12-04T19:23:21.953 に答える