Incorrect column count: expected 1, actual 10 IncorrectResultSetColumnCount

来源:互联网 发布:有设计感的淘宝店 编辑:程序博客网 时间:2024/06/06 09:06

      在使用JdbcTemplate的时候,当调用类似queryForInt(String sql,args)等方法时,其返回值是int类型(或者String类型),这个时候因为我们的sql语句导致了如题的错误。如果返回的是int类型,则查询的结果应该是int类型的,现在举例如下:
//错误的sql代码

public int login(String name, String password) {// TODO Auto-generated method stubreturn jdbcTemplate.queryForInt("select * from user where name=? and password=?",new Object[] { name, password });}

以上代码导致如题错误

这样是错误的,因为该方法返回值为int类型,使用 *的话返回的是结果集。如果
改成select name 或者select password 也还是不行,因为此处name和password
类型均为String类型。
正确的思路是:方法返回值与查询结果类型应该是相匹配的的。所以此处改为select count(*) from user where name... 
//正确代码
public int login(String name, String password) {// TODO Auto-generated method stubreturn jdbcTemplate.queryForInt("select count(*) from user where name=? and password=?",new Object[] { name, password });}
其他的返回类型依次类推。记住这个错误,避免下次再犯!

0 2