JDBC错误(SQL语法)参数索引超出范围(2>参数个数,即1)

时间:2013-08-15 11:08:58

标签: java spring-mvc jdbc

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

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,所以不知道如何解决这个问题......

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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());

这很有效!感谢

答案 2 :(得分:-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;

结论:如果您重新调整值,可能会误将其放入&#34; null&#34;值的类型...表示有些事情使错误方法写入(OR)返回空值