public List<TblRentInfo>findProjectByTitle(String title) throws Exception
{
List<TblRentInfo>  p= null;
List<TblRentInfo> pList=userDao.search("and title='"+title+"'");
if(pList.size()>0)
{
p=(List<TblRentInfo>) pList.get(0);
//System.out.println(p.add(pList.get(0)));
System.out.println("2222222");
System.out.println(p);
}
return p;

}
public  List<TblRentInfo> search(String condition) throws Exception
{
  List<TblRentInfo> userList=new ArrayList<TblRentInfo>();
  try
{
session=HibernateSessionFactory.getSession();
Query query=session.createQuery("from TblRentInfo where 1=1 "+condition);
userList=query.list();
System.out.println("1111111111");
}catch(Exception ex)
{
ex.printStackTrace();
throw new Exception("数据库错误");
}finally
{
HibernateSessionFactory.closeSession();
}
 
  return userList;
}HibernateStruts

解决方案 »

  1.   

    前台界面没值,SQL语句已经查出来了,但是不返回。。调试时 search()中 111111 可以输出,但是findProjectByTitle()方法里面的 2222没输出结果。。p=(List<TblRentInfo>) pList.get(0);  我猜 应该是这行语句得不到值。
      

  2.   

    List<TblRentInfo> pList 这个列表是空的
      

  3.   

    检查数据库和是否有返回值,log一下
      

  4.   

    怎么 log ? 不会  求指教
      

  5.   

    log4j有吧,直接log.info,看看你的sql语句是什么内容,放到sql里面执行一下就知道了
      

  6.   


    SQL 语句查出来了。 是我要的查的语句,但是就是返回值收不到,p=(List<TblRentInfo>) pList.get(0); P是空值
      

  7.   


    SQL 语句查出来了。 是我要的查的语句,但是就是返回值收不到,p=(List<TblRentInfo>) pList.get(0); P是空值你的库里面有对应的数据??直接copy到你的数据库去查,没数据当然查不到呀,前提你的sql没问题!
      

  8.   


    SQL 语句查出来了。 是我要的查的语句,但是就是返回值收不到,p=(List<TblRentInfo>) pList.get(0); P是空值你的库里面有对应的数据??直接copy到你的数据库去查,没数据当然查不到呀,前提你的sql没问题!
    public List<TblRentInfo>findProjectByTitle(String title) throws Exception
    {
    List<TblRentInfo>  p= null;
    List<TblRentInfo> pList=userDao.search("and title='"+title+"'");
    System.out.println("22222222");
    if(pList.size()>0)
    {
    p=(List<TblRentInfo>) pList.get(0);
    //System.out.println(p.add(pList.get(0)));
    System.out.println("33333");
    System.out.println(p);
    }
    System.out.println("4444444");
    return p;

    }
    我这样调试了一下 后台显示结果为:
    Hibernate: select tblrentinf0_.id as id1_.....
    1111111111
    []
    22222222
    4444444
    也就是说 没有进入if 语句。 所以P 还是个null 值。
      

  9.   


    SQL 语句查出来了。 是我要的查的语句,但是就是返回值收不到,p=(List<TblRentInfo>) pList.get(0); P是空值你的库里面有对应的数据??直接copy到你的数据库去查,没数据当然查不到呀,前提你的sql没问题!数据库里有对应的数据,查出来的sql是正确的 试了。
      

  10.   


    SQL 语句查出来了。 是我要的查的语句,但是就是返回值收不到,p=(List<TblRentInfo>) pList.get(0); P是空值你的库里面有对应的数据??直接copy到你的数据库去查,没数据当然查不到呀,前提你的sql没问题!数据库里有对应的数据,查出来的sql是正确的 试了。findProjectByTitle这个方法的写法太繁琐了,而且p=(List<TblRentInfo>) pList.get(0);这句话你不觉得有问题?
      

  11.   


    SQL 语句查出来了。 是我要的查的语句,但是就是返回值收不到,p=(List<TblRentInfo>) pList.get(0); P是空值你的库里面有对应的数据??直接copy到你的数据库去查,没数据当然查不到呀,前提你的sql没问题!数据库里有对应的数据,查出来的sql是正确的 试了。其实就是这样的:  
    public String findHouserTitle(){
    try{
     findList= userService.findProjectByTitle(houseUser.getTitle());
     System.out.println("");
     return this.SUCCESS;
    }catch(Exception ex){
    this.addActionError(ex.getMessage());
    return this.ERROR;
    }

    }调用fandfindProjectByTitle方法
    public List<TblRentInfo>findProjectByTitle(String title) throws Exception
    {
    List<TblRentInfo>  p= null;
    List<TblRentInfo> pList=userDao.search("and title='"+title+"'");
    if(pList.size()>0)
    {
    p= pList.get(0);
    }
    return pList;

    }
     然后 调用search方法 public  List<TblRentInfo> search(String condition) throws Exception
    {
      List<TblRentInfo> userList=new ArrayList<TblRentInfo>();
      try{
    session=HibernateSessionFactory.getSession();
    Query query=session.createQuery("from TblRentInfo where 1=1 "+condition);
    userList=query.list();
    }catch(Exception ex)
    {
    ex.printStackTrace();
    throw new Exception("数据库错误");
    }finally{
    HibernateSessionFactory.closeSession();
    }
     
      return userList;
    }
    }
      

  12.   

    额都说了,你的Plist对应的数据库没有值
      

  13.   


    SQL 语句查出来了。 是我要的查的语句,但是就是返回值收不到,p=(List<TblRentInfo>) pList.get(0); P是空值你的库里面有对应的数据??直接copy到你的数据库去查,没数据当然查不到呀,前提你的sql没问题!数据库里有对应的数据,查出来的sql是正确的 试了。p=(List<TblRentInfo>) pList.get(0); 我就是觉得这行有问题 但是不知道怎么改 。反正就是把SQL查询出来的结果对象 怎么放到findHouserTitle()中的 findList 去就行了。 sql语句已经确定没问题了 我试过了。
      

  14.   


    SQL 语句查出来了。 是我要的查的语句,但是就是返回值收不到,p=(List<TblRentInfo>) pList.get(0); P是空值你的库里面有对应的数据??直接copy到你的数据库去查,没数据当然查不到呀,前提你的sql没问题!数据库里有对应的数据,查出来的sql是正确的 试了。我懂了,终于知道问题出在那了纠结了半天是的  sql 语句是对的,但是查出来的结果无值,因为条件没带过去。。 谢了O(∩_∩)O~
      

  15.   

    List<TblRentInfo> userList=new ArrayList<TblRentInfo>();
    直接改成List<TblRentInfo> userList = null;
    List<TblRentInfo> pList.get(0)返回的应该是TblRentInfo类型的,p= pList.get(0);这句p也是一个实体类型,为啥要转成一个List?
      

  16.   

    p=(List<TblRentInfo>) pList.get(0);
    p应该不是list 而是TblRentInfo
    改为p=(TblRentInfo) pList.get(0);
      

  17.   

    userList=query.list();这一句不需要强转吗
      

  18.   

    List<TblRentInfo> pList=userDao.search("and title='"+title+"'");
    pList 这个有值吗?这个有值的话,直接 return pList.get(0);