首页 > 解决方案 > SQLException: Select 语句抛出异常 The index 1 is out of range

问题描述

我尝试连接到 MS SQL Server,但是当我首次使用它时,我得到了 SQLException。

PreparedStatementCallback; SQL [select * from dbo.theLogin tl;]; 索引 1 超出范围

public ArrayList<Login> select(String username, String password) {
    JdbcTemplate select = new JdbcTemplate(dataSource);
    return  (ArrayList<Login>) select.query(
                    "select tl.username, tl.password from dbo.theLogin tl;",
                    new Object[] {username, password},
                    new LoginRowMapper());
}

我无法弄清楚它有什么问题。请帮忙。预先感谢

标签: javasql-serversqlexception

解决方案


The second parameter to your query() call should be a list of values to use to bind to any variables in the SQL statement - but your SQL does not have any variables to bind to, so you get that "index 1 is out of range" when it tries to set the first parameter to the value of username. Your SQL looks like it will return a list of all username and password pairs from your table - which may not be what you were after...


推荐阅读