错误代码如下
org.springframework.dao.TypeMismatchDataAccessException: Type mismatch affecting row number 0 and column type 'BIGINT': Value [7] is of type [java.lang.Integer] and cannot be converted to required type [int]
at org.springframework.jdbc.core.SingleColumnRowMapper.mapRow(SingleColumnRowMapper.java:101)
at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:93)
at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:60)
at org.springframework.jdbc.core.JdbcTemplate$1QueryStatementCallback.doInStatement(JdbcTemplate.java:455)
使用Spring框架 运用JdbcTemplate来查询表里的数据的条数;出错了
错误代码如下
//获取单个的值
@Test
public void queryone(){String sql="select count(*) from user";int a=jdbcTemplate.queryForObject(sql, int.class);System.out.println(a);
}
错误原因
这个错误信息说的很明显,我给出的结果类型是int类型,实际应该是BIGINT类型,
正确代码如下
//获取单个的值
@Test
public void queryone(){String sql="select count(*) from user";System.out.println(jdbcTemplate.queryForObject(sql, BigInteger.class));
}