• 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: 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就可以。修改代码如下:

    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;
            }
        }
  • 相关阅读:
    Vagrant box ubuntu/xenial64 添加vagrant用户解决没有登录密码的问题
    jQuery获取浏览器URL链接的值
    js防止客户端多触发
    Jquery实现一组复选框单选
    jQuery监听文本框值改变触发事件(propertychange)
    将http调用返回json中的有关中文的unicode转换为中文
    Visual Studio 2015 Bowser Link的功能不停的向服务端发送请求
    客户端向服务端传送特殊字符解决方法(检测到有潜在危险的 Request.Form 值)
    java集群之session共享解决方案
    阻止保存要求重新创建表的更改选项
  • 原文地址:https://www.cnblogs.com/yshyee/p/3611746.html
Copyright © 2020-2023  润新知