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;
}
}

org.springframework.dao.EmptyResultDataAccessException的更多相关文章

  1. (后端)org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1,actual 0

    两种方案: 用queryForList方法替换queryForObject或者queryForMap,因为这两个方法必须要有值,不能为空. 把这个异常捕获,用try/catch. 这个查询的结果是nu ...

  2. 【异常】Caused by: java.lang.ClassNotFoundException: org.springframework.dao.DataIntegrityViolationException

    Caused by: java.lang.ClassNotFoundException: org.springframework.dao.DataIntegrityViolationException ...

  3. org.springframework.dao.DataIntegrityViolationException:

    数据库用的hibernate,开发工具用的myeclipse,使用开发工具连接数据库生成hibernate基于xml的po类,运行时报org.springframework.dao.DataInteg ...

  4. javaEE-----org.springframework.dao.InvalidDataAccessApiUsageException: Write operation

    org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read ...

  5. org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [41] did not match expected type [java.lang.Integer (n/a)];

    题记:以前记录过一些自己遇到的BUG,这个行为,让我一看报错的提示信息就能定位到问题的所在,后来记得比较多了,好多是重复性的再加上比较忙就没有详细的记录了,今天的工作量比较小,就顺便记录一下,以便以后 ...

  6. org.springframework.dao.InvalidDataAccessApiUsageException:The given object has a null identifi的解决方案

    异常信息: org.springframework.dao.InvalidDataAccessApiUsageException: The given object has a null identi ...

  7. org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode

    [spring]:org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowe ...

  8. OpenSessionInViewFilter与org.springframework.dao.InvalidDataAccessApiUsageException

    报错:org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in r ...

  9. org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: sys.entity.Role; nested exception is org.hibernate.PersistentObjectException: 的解决方案

    1.错误信息 org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist ...

随机推荐

  1. Android中图片的目录

    图片放在hdpi及在mdpi中的效果是不一样的 http://blog.csdn.net/loongggdroid/article/details/9469935

  2. layout_weight

    最近写Demo,突然发现了Layout_weight这个属性,发现网上有很多关于这个属性的有意思的讨论,可是找了好多资料都没有找到一个能够说的清楚的,于是自己结合网上资料研究了一下,终于迎刃而解,写出 ...

  3. 懂说话,让冲突、尴尬时刻都bye-bye

    回忆一下自己从小到大的成长历程,你容不容易交到新朋友?在不熟悉的社交场合中,你是否也曾面临坐立难安.不知从何聊起的窘境?事实上,无论身处学校或社会,担任职务是基础或高阶,终其一生都会派上用场的能力,就 ...

  4. Web模板大全

    http://www.dede58.com/a/free/ http://down.admin5.com/moban/109507.html http://www.chinaz.com/ http:/ ...

  5. C#语言之“中英文混合字符串对齐”的方法【转】

    参考自:(1)http://www.cnblogs.com/cnluoke/articles/1213398.html (2)http://www.cnblogs.com/sql4me/archive ...

  6. Android添加横线和竖线分割界面

    竖线 <View       android:layout_width="1dip"     android:layout_height="match_parent ...

  7. 【转】Linux下tar.xz结尾的文件的解压方法--不错

    原文网址:http://blog.csdn.net/silvervi/article/details/6325698 今天尝试编译内核,下载到了一份tar.xz结尾的压缩文件,网上解决方法比较少,不过 ...

  8. WifiDog and OpenWrt

    $Id$ 2   3 OpenWRT specific README 4 ======================= 5   6 So, you want to run wifidog on on ...

  9. 第六百二十一天 how can I 坚持

    好歹都是一些集成的框架,也懒得去深入研究了,微信小程序,今天体验了一个,虽然内心非常相信张小龙,但是还是不太看好,但是感觉确实需要一种方式去简化应用程序的开发,今天把没用的app都删了,太耽误时间了, ...

  10. python爬爬(网友提供学习)

    import urllib2,urllib,os,re def ZZ(url): pathw=os.getcwd() #图片和标题目录 imagetitleregion=r'<div class ...