public class AdminAction extends Action {       public ActionForward execute(ActionMapping mapping, ActionForm form, 
                 HttpServletRequest request, HttpServletResponse response) throws Exception {
        // TODO Auto-generated method stub
        String userName=request.getParameter("userName");
        String userPwd=request.getParameter("userPwd");        String uPwd=null;
        Session session=SessionFactory.currentSession();
        Transaction tx=session.beginTransaction();
        Query query=session.createQuery("select * from myhibernate where username='"+ userName +"'");
        try{
           Iterator it=query.iterate();  //debug 直接转到 catch(Exception e)  捕获e的信息如下
            AdminTable admin=(AdminTable) it.next();
           uPwd=admin.getUserPwd();
        }catch(Exception e){
            return mapping.findForward("error");
        }
        tx.commit();
        SessionFactory.closeSession();
        if(uPwd.equals(userPwd)){
            return mapping.findForward("show");
        }else{
        return mapping.findForward("error");
    }
    }
    
}
捕获e的信息: net.sf.hibernate.QueryException: unexpected token: as [select *  from myhibernate as a where a.username='1'] 
大家帮帮忙,我是新手,在线等指教

解决方案 »

  1.   

     我的工程里现在可以向数据库中插入数据,这说明我的连接池应该没有问题,SQL文在数据库中运行也正常,所以不知道问题到底出在哪里??  请多多指教啊 !!!!!! 急急
      

  2.   

    HQL是不允许直接使用*号的,你写的是SQL语句,可是却把它当HQL用了。第一个解决方法(从下边的代码来看,我猜你的实例类名叫AdminTable):
    Query query=session.createQuery("from AdminTable where username='"+ userName +"'");
    其中,粗体部分是你的“myhibernate”表所对应的实体类。
    你可能注意到了,没写“select”,是的,就是不需要,嘿嘿。第二个解决方法(使用本地SQL查询):
    SQLQuery query=session.createSQLQuery("select * from myhibernate where username='"+ userName +"'");