日期:2014-05-18  浏览次数:20787 次

自己看视频学struts+hibernate仿照着写个最简单的例子都报错啊救我啊!
主要是这里啊

/*
  *   Generated   by   MyEclipse   Struts
  *   Template   path:   templates/java/JavaClass.vtl
  */
package   com.jhkj.struts.action;

import   java.util.Iterator;

import   javax.servlet.http.HttpServletRequest;
import   javax.servlet.http.HttpServletResponse;

import   org.apache.struts.action.Action;
import   org.apache.struts.action.ActionForm;
import   org.apache.struts.action.ActionForward;
import   org.apache.struts.action.ActionMapping;
import   org.hibernate.Query;
import   org.hibernate.Session;
import   org.hibernate.Transaction;

import   com.jhkj.HibernateSessionFactory;
import   com.jhkj.Userlist;

/**  
  *   MyEclipse   Struts
  *   Creation   date:   06-22-2007
  *  
  *   XDoclet   definition:
  *   @struts.action   validate= "true "
  */
public   class   LoginAction   extends   Action   {
/*
  *   Generated   Methods
  */

/**  
  *   Method   execute
  *   @param   mapping
  *   @param   form
  *   @param   request
  *   @param   response
  *   @return   ActionForward
  */
public   ActionForward   execute(ActionMapping   mapping,   ActionForm   form,
HttpServletRequest   request,   HttpServletResponse   response)   {
//   TODO   Auto-generated   method   stub
//获取提交的用户名称和密码
String   userName=request.getParameter( "userName ");
String   userPwd=request.getParameter( "userPwd ");
System.out.println( "用户名 "+userName+ "密码 "+userPwd);
String   mUserPwd=null;
Userlist   ul=null;
//创建连接
Session   session=HibernateSessionFactory.getSession();
//创建事务
Transaction   tx=session.beginTransaction();
//创建对话
Query   query=session.createQuery( "select   u   from   Userlist   as   u   where   username= ' "   +
userName   +   " ' ");
try{
// Iterator   it   =   query.iterate();
// ul   =   (Userlist)   it.next();
// mUserPwd=ul.getUserpwd();
        Userlist   uk=(Userlist)query.uniqueResult();  
        mUserPwd=uk.getUserpwd();
}catch   (Exception   e)   {
System.out.println( "异常信息: "+e.getMessage());
}
if(userPwd.equals(mUserPwd))
request.setAttribute( "loginOk ", "Hello     "   +   ul.getUsername());
else
request.setAttribute( "loginOk ", "Login   Error! ");
//事务提交
tx.commit();
//关闭连接
HibernateSessionFactory.closeSession();
return   mapping.findForward( "success ");
}
}


// Iterator   it   =   query.iterate();
// ul   =   (Userlist)   it.next();
// mUserPwd=ul.getUserpwd();

用上面这个获得密码报
could     not   execute   query   using   iterate