现在的位置: 首页 > 综合 > 正文

Incorrect column count: expected 1, actual 10 IncorrectResultSetColumnCount

2018年05月20日 ⁄ 综合 ⁄ 共 717字 ⁄ 字号 评论关闭

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

public int login(String name, String password) {
		// TODO Auto-generated method stub
		return 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 stub
		return jdbcTemplate.queryForInt(
				"select count(*) from user where name=? and password=?",
				new Object[] { name, password });

	}

其他的返回类型依次类推。记住这个错误,避免下次再犯!

抱歉!评论已关闭.