org.springframework.dao.EmptyResultDataAccessException

org.springframework.dao.EmptyResultDataAccessException
public Wcrash getWcrashInfo(int id) {
        String sql = "select plateform_id,android_version,app_version_code,app_version_name,device_id,"
                + "model,brand,product,stack_trace,crash_date,package_name from wcrash where id = ? ";
        return this.jdbcTemplate.queryForObject(
                sql, 
                new Object[]{id},
                new RowMapper<Wcrash>(){
                    @Override
                    public Wcrash mapRow(ResultSet rs, int i) throws SQLException {
                        Wcrash wcrash = new Wcrash();
                        wcrash.setPlateformId(toNullStr(rs.getString("plateform_id")));
                        wcrash.setAndroidVersion(toNullStr(rs.getString("android_version")));
                        wcrash.setAppVersionCode(toNullStr(rs.getString("app_version_code")));
                        wcrash.setAppVersionName(toNullStr(rs.getString("app_version_name")));
                        wcrash.setDeviceId(toNullStr(rs.getString("device_id")));
                        wcrash.setModel(toNullStr(rs.getString("model")));
                        wcrash.setBrand(toNullStr(rs.getString("brand")));
                        wcrash.setProduct(toNullStr(rs.getString("product")));
                        wcrash.setStackTrace(toNullStr(rs.getString("stack_trace")));
                        wcrash.setCrashDate(toNullStr(rs.getString("crash_date")));
                        wcrash.setPackageName(toNullStr(rs.getString("package_name")));
                        return wcrash;
                    } 
                });
    }
org.springframework.dao.EmptyResultDataAccessException

异常信息:

org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0
at org.springframework.dao.support.DataAccessUtils.requiredSingleResult(DataAccessUtils.java:71)
at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:729)

queryForObject无记录时会抛出异常,而对于query()list的类型不会抛出此异常。解决方法就是捕获这个异常然后在catch中返回null就可以。修改代码如下:

org.springframework.dao.EmptyResultDataAccessException
public Wcrash getWcrashInfo(int id) {
        String sql = "select plateform_id,android_version,app_version_code,app_version_name,device_id,"
                + "model,brand,product,stack_trace,crash_date,package_name from wcrash where id = ? ";
        try{
            return this.jdbcTemplate.queryForObject(
                sql, 
                new Object[]{id},
                new RowMapper<Wcrash>(){
                    @Override
                    public Wcrash mapRow(ResultSet rs, int i) throws SQLException {
                        Wcrash wcrash = new Wcrash();
                        wcrash.setPlateformId(toNullStr(rs.getString("plateform_id")));
                        wcrash.setAndroidVersion(toNullStr(rs.getString("android_version")));
                        wcrash.setAppVersionCode(toNullStr(rs.getString("app_version_code")));
                        wcrash.setAppVersionName(toNullStr(rs.getString("app_version_name")));
                        wcrash.setDeviceId(toNullStr(rs.getString("device_id")));
                        wcrash.setModel(toNullStr(rs.getString("model")));
                        wcrash.setBrand(toNullStr(rs.getString("brand")));
                        wcrash.setProduct(toNullStr(rs.getString("product")));
                        wcrash.setStackTrace(toNullStr(rs.getString("stack_trace")));
                        wcrash.setCrashDate(toNullStr(rs.getString("crash_date")));
                        wcrash.setPackageName(toNullStr(rs.getString("package_name")));
                        return wcrash;
                    } 
                });
        }catch(EmptyResultDataAccessException e){
            return null;
        }
    }
org.springframework.dao.EmptyResultDataAccessException

org.springframework.dao.EmptyResultDataAccessException,布布扣,bubuko.com

org.springframework.dao.EmptyResultDataAccessException

上一篇:Gentoo emerge USE 安装postgresql


下一篇:sql 保留两位小数