我的程序时这样的:
public boolean isLogin(String username, String password){
List<User> list = sessionFactory.getCurrentSession().createQuery("from User u where u.username =? and u.password=?",new Object[]{username,password});
System.out.println("list的大小为:"+list.size());
if(null!=list&&list.size()>0)
{
return true;
}
return false;
}在createQuery这个地方错了  createQuery这个方法不对    我想问下在执行这条HQL语句的时候要用什么方法呢如果能再说说常用的方法更好了
不差分   尽管回帖

解决方案 »

  1.   


    public boolean isLogin(String username, String password) {
    Query query = sessionFactory.getCurrentSession().createQuery("from User u where u.username =? and u.password=?");
    query.setString(1, username);
    query.setString(2, password);
    List<User> list = query.list();
    System.out.println("list的大小为:" + list.size());
    if (null != list && list.size() > 0) {
    return true;
    }
    return false;
    }
      

  2.   

    Java code
    public boolean isLogin(String username, String password) {
            Query query = sessionFactory.getCurrentSession().createQuery(" select u from User u where u.username =? and u.password=?");
            query.setString(1, username);
            query.setString(2, password);
            List<User> list = query.list();
            System.out.println("list的大小为:" + list.size());
            if (null != list && list.size() > 0) {
                return true;
            }
            return false;
        }
      

  3.   

    select u from User u where u.username =? and u.password=?"); 返回的是一个User对象,若没有则为空
      

  4.   

    2楼的 我试了   报错 2009-12-9 17:10:51 org.apache.catalina.core.StandardWrapperValve invoke
    严重: Servlet.service() for servlet struts threw exception
    org.hibernate.hql.ast.QuerySyntaxException: User is not mapped [from User u where u.username =? and u.password=?]
    3楼的我再看看
      

  5.   

        query.setString(1, username); 
        query.setString(2, password); 
    应该是  
       query.setString(0, username); 
       query.setString(1, password);   吧 
    3楼的好使了   一会再看看2楼的   原因是bean.xml里少写了     <value>cn/itcast/bean/User.hbm.xml</value>大意了
      

  6.   

    public boolean userLogin(String hql){
    Session session=HibernateSessionFactory.getSession();
    List   arr=null;
    try{
    Query query = session.createQuery(sql); }catch(Exception ex){
    ex.printStackTrace();
    }finally{
    session.close();
    }
    arr   =  query.list();
            if(arr!=null && arr.size()>0){
             return true;
    }else{
    return false;   
    }
       }
      

  7.   

    7楼的太繁琐了  又try又close的