Hibernate ,Serverlet POST查询

Hibernate ,Serverlet POST查询
         try  {
            String username 
=  request.getParameter( " username " );
            String password 
=  request.getParameter( " password " );
            SessionFactory sf 
=  DefaultDataUtil.getSessionFactory();
            Session s 
=  sf.openSession();
            Query query 
=  s.createQuery( " from Users where username=:name " );
            query.setParameter(
" name " , username);
            List list 
=  query.list();
            
if (list.size()  ==   1 )
            {
                Object o 
=  list.get( 0 );
                Users user 
=  (Users)o;
                
if (user.getPassword().equals(password) )
                {
                    out.print(
" 验证成功。 " );
                }
                
else
                {
                    out.print(
" 密码错误。 " );
                }
            }
            
else
            {
                out.print(
" 用户名错误。 " );
            }
        }
        
catch (Exception ex){
            out.print(ex.getMessage());
        }
        
finally  {
            out.close();
        }

HQL语句对于表名有大小写匹配的关系,匹配的映射过来的类名。否则会报xxx not mapped....错误。其它的还是很简单的。

你可能感兴趣的:(Hibernate ,Serverlet POST查询)