使用了Hibernate
res=null;
accountInfo accInfo = (accountInfo)session.get(accountInfo.class,str);
res=accInfo.getAccandpass();
return res;如果str所指代的内容在数据库中没有。就抛出异常No row with the given identifier exists
程序就结束,动不了了。有没有什么办法能够让其在查不到的情况下最后仍然能够执行return res呢?????

解决方案 »

  1.   

    <logic:notEmpty>.....
    </logic:notEmpty>
    <logic:empty >
    <tr align="center">
    <td colspan="9">
    <h1>没有数据</h1>
    </td>
    </tr>
    </logic:empty>
    可以在页面用这个标签
      

  2.   

    怎么围的?
    res=null;
    try {
    accountInfo accInfo = (accountInfo)session.get(accountInfo.class,str);
    res=accInfo.getAccandpass();
    } catch (Exception e) {
    }
    return res;
      

  3.   

    Session session = sessionFac.HibernateSessionFactory.getSession();
    System.out.println("session build");
    try{
    accountInfo accInfo = (accountInfo)session.get(accountInfo.class,str);
    res=accInfo.getAccandpass(); if(res.equals(str)){
    accInfo.setIpadd(ip); 
    System.out.println("IP address is"+   ip);
    Transaction tran = session.beginTransaction();
    try{
    session.update(accInfo);
    tran.commit();
    }
    catch(Exception ex){
    tran.rollback();
    }
    }
    sessionFac.HibernateSessionFactory.closeSession();
    }catch(Exception e){}
    return res;
    如果按你的办法我试过 没用啊。如果仅仅
    try {
    accountInfo accInfo = (accountInfo)session.get(accountInfo.class,str);
    res=accInfo.getAccandpass();
    } catch (Exception e) {
    } 这样的话if里面的语句凡是用到accInfo 的都会报错 就像变量没定义一样的那种错误。无语啊
    有啥办法么?
      

  4.   

    无语啦,你的代码写的。
    先把get的方法改为load
    改为:Session session = sessionFac.HibernateSessionFactory.getSession(); 
    System.out.println("session build"); 
    try{
    accountInfo accInfo = (accountInfo)session.load(accountInfo.class,str); 
    if (accInfo != null) {
    res=accInfo.getAccandpass();  if(res != null && res.equals(str)){ 
    accInfo.setIpadd(ip); 
    System.out.println("IP address is"+  ip); 
    Transaction tran = session.beginTransaction();  session.update(accInfo); 
    tran.commit();  }

    }}catch(Exception e){} 
    sessionFac.HibernateSessionFactory.closeSession(); 
    return res;