参数索引超出范围,具有多个连接和可选输入参数

时间:2017-05-29 06:47:40

标签: mysql jdbctemplate

如果我需要使输入参数可选,我在使用Spring JdbcTemplate时遇到多个Join的问题。

这就是情景。

我必须执行Join的SQL表是:

CREATE TABLE users (
id             INT NOT NULL AUTO_INCREMENT,
name           VARCHAR(15),
password       VARCHAR(20),
info           VARCHAR(30),
active         BOOLEAN,
locked         BOOLEAN,
lockout_duration INT DEFAULT 0,
lockout_limit  DATETIME,
login_attempts INT DEFAULT 0,
PRIMARY KEY(id)
);

CREATE TABLE profiles (
id             INT NOT NULL AUTO_INCREMENT,
name           VARCHAR(15),
info           VARCHAR(30),
PRIMARY KEY(id)
);

CREATE TABLE profiling(
user_id        INT NOT NULL,
profile_id     INT NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (profile_id) REFERENCES profiles(id) ON DELETE CASCADE,
PRIMARY KEY(user_id,profile_id)
);

分析是将用户与其个人资料相关联的表格;该配置文件必须用于识别允许用户执行的操作。

在我的前一篇文章中,我问如何使sql参数可选,我获得了一个完美的响应,这是有效的,从那时起我就一直使用。所以,如果我需要这样做,请放在哪里:

WHERE(null或variabile_name =?)

并且,使用jdbctemplate,我写道:

jdbcTemplate.query(SQL,new Object [] {variabile_name,variabile_name},mapper_name);

当然,SQL是我进行查询的String对象。

所以,我也是在这种情况下做的,但是我得到了错误:

java.lang.IllegalStateException: Failed to execute CommandLineRunner

Caused by: java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2).

我在这里报告完整的方法:

/**
     * This metod return the join between users and profiles
     * made using the profiling table
     * 
     * @param userID the user id code
     * @param profilesID the profile id code
     * @return the join list
     */
    public List<Profiling> joinWithUsersandProfiles(Integer userID, Integer profileID)
    {
        //This is the mapper for Profiling
        RowMapper<Profiling> profilingMapper = new RowMapper<Profiling>()
        {
            //This method must be implemented when we use a row mapper
            public Profiling mapRow(ResultSet rs, int rowNum) throws SQLException
            {
                Profiling profiling = new Profiling();
                profiling.setUser_id(rs.getInt("profiling.user_id"));
                profiling.setProfile_id(rs.getInt("profiling.profile_id"));
                //mapping users variabiles
                profiling.setUsersId(rs.getInt("users.id"));
                profiling.setUsersActive(rs.getBoolean("users.active"));
                profiling.setUsersInfo(rs.getString("users.info"));
                profiling.setUsersLocked(rs.getBoolean("users.locked"));
                profiling.setUsersLockoutDuration(rs.getInt("users.lockout_duration"));
                profiling.setUsersLockoutLimit(rs.getTime("users.lockout_limit"));
                profiling.setUsersLoginAttempts(rs.getInt("users.login_attempts"));
                profiling.setUsersName(rs.getString("users.name"));
                profiling.setUsersPassword(rs.getString("users.password"));
                //mapping profiles variabiles
                profiling.setProfilesId(rs.getInt("profiles.id"));
                profiling.setProfilesInfo(rs.getString("profiles.info"));
                profiling.setProfilesName(rs.getString("profiles.name"));

                return profiling;

            }
        };

        /**
         * This is the string that contain the query to obtain the data from profiling table.
         * Please note that the notation "? is null or x = ?" means that the x variabile is optional;
         * it can be asked as no making the query. 
         * If we give alla input equals null, it means that we must perform a SQl Select * From table.
         */
        String SQL = "SELECT * FROM profiling JOIN users ON profiling.user_id = users.id JOIN profiles ON profiling.profile_id = profiles.id WHERE (is null or profiling.user_id = ?) AND (is null or profiling.profile_id = ?)";

        /**
         * The list containing the results is obtained using the method query on jdcbtemplate, giving in in input to it the query string, the array of object 
         * containing the input variabile of the method and the rowmapper implemented.
         */
        List<Profiling> theProfilings = jdbcTemplate.query(SQL, new Object[]{userID, userID, profileID, profileID}, profilingMapper); 

        return theProfilings;
    }

我知道问题是由可选的变量。为什么?如果我尝试删除可选代码并从:

传递
(is null or variabile_name = ?)

variabile_name = ?

代码完美无缺。

那么,我的错误是什么?

编辑:解决了自己。我忘了?在&#34;之前是空的&#34;码。所以,转到:

(? is null or variabile_name = ?)

代码有效。

0 个答案:

没有答案