Spring 执行SQL

Spring(JdbcTemplate.class)中的queryForMap()、queryForObject()、queryForLong()、queryForInt()等方法都会去调用
public static Object requiredSingleResult(Collection results) throws IncorrectResultSizeDataAccessException这个方法(DataAccessUtils.class)
,此方法中会判断得到的结果集Collection是否为空,为空就抛出异常EmptyResultDataAccessException
结果程序出错,在程序中catch该异常并处理一下就可以了。
Integer intTest;
  try {
    Map mapTemp = getJdbcTemplate().queryForMap(sSql, oArry );
   intTest= (Integer) mapTemp.get("N_Column");
  } catch (EmptyResultDataAccessException e) {
     intTest=new Integer(0);
  }

或者可以考虑使用queryForList()、queryForRowSet()这些方法,去处理。
List listTest = getJdbcTemplate().queryForList(sSql, oArry );

猜你喜欢

转载自hck.iteye.com/blog/1583451