import org.apache.log4j.Logger;
/**
* @author yanghx
* To change the template for this generated type comment go to Window -
* Preferences - Java - Code Generation - Code and Comments
*/
public class TestServlet extends HttpServlet {
private static Logger log = Logger.getLogger(TestServlet.class);
long start = System.currentTimeMillis();
for (int i = 0; i < 10; i++) {
UserInfo message = new UserInfo();
message.setId(String.valueOf(i));
message.setUserName(String.valueOf(i));
message.setPassword(String.valueOf(i));
try {
s.save(message);
s.flush();
//s.evict(message);
} catch (HibernateException e4) {
e4.printStackTrace();
log.error("Hibernate Insert 错误" + e4);
return;
}
}
long end = System.currentTimeMillis();
log.info(new Long(end - start));
}
catch(Exception e){
e.printStackTrace();
log.error("Hibernate Insert 错误" + e);
return;
}
finally {
try {
s.close();
} catch (HibernateException e4) {
e4.printStackTrace();
log.error("Hibernate Close 错误" + e4);
}
}
}
}
启动Tomcat,显示如下:
信息: Creating new Registry instance
2004-3-20 22:58:04 org.apache.commons.modeler.Registry getServer
信息: Creating MBeanServer
2004-3-20 22:58:05 org.apache.coyote.http11.Http11Protocol init
信息: Initializing Coyote HTTP/1.1 on port 80
Starting service Tomcat-Standalone
Apache Tomcat/4.1.27-LE-jdk14
2004-03-20 22:58:11,734 INFO [main] web.TestHBMServlet (TestHBMServlet.java:58) - Open Session OK
2004-03-20 22:58:11,890 INFO [main] web.TestHBMServlet (TestHBMServlet.java:77) - 140
2004-3-20 22:58:13 org.apache.struts.util.PropertyMessageResources <init>
信息: Initializing, config='org.apache.struts.util.LocalStrings', returnNull=true
2004-3-20 22:58:13 org.apache.struts.util.PropertyMessageResources <init>
这样 Hibernate配置利用Tomcat的连接池成功。
注意点:
l 不能利用cfg = new Configuration().configure("/hibernate.cfg.xml");创建Configure以后再利用cfg.addClass(…)这样配置以后只能利用配置文件中的<mapping resource=“”/>
l 因为已经利用了hibernate.properties所以在编写其他时候一定要用cfg = new Configuration().configure("/hibernate.cfg.xml")来配置Configure
l 删除文件hibernate.properties
现在我们开始编写实例Logon程序
利用Struts1.1
利用MyEclipse向导
第2步
创建Form,Action,以及相应的JSP文件
添加用户名属性
添加密码属性
下一步
下一步
分别添加Forward
最后
下面我们编写生成的文件
首先LogonForm.java文件
public ActionErrors validate(
ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors=super.validate(mapping,request);
if(errors==null){
errors=new ActionErrors();
}
if((password==null)||(password.trim().length()==0)){
errors.add("password",new ActionError("password"));
}
if((userName==null)||(userName.trim().length()==0)){
errors.add("userName",new ActionError("userName"));
}
return errors;
}
修改Action类
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LogonForm logonForm = (LogonForm) form;
Session s = null;
try {
s = YJBaseHBM.currentSession();
} catch (HibernateException e) {
e.printStackTrace();
ActionErrors errors = new ActionErrors();
errors.add("hbm.session.error",
new ActionError("hbm.session.error"));
saveErrors(request, errors);
return mapping.findForward("failure");
}
try {
Query q=s.createQuery("from UserInfo ui where ui.userName=:user and password=:password");
q.setString("user", logonForm.getUserName());
q.setString("password", logonForm.getPassword());
System.out.println(q.toString());
Iterator it = q.iterate();
if(it.hasNext()==false){
ActionErrors errors = new ActionErrors();
errors.add("no.user",
new ActionError("no.user"));
saveErrors(request, errors);
return mapping.findForward("failure");
}
YJBaseHBM.closeSession();
} catch (HibernateException e) {
e.printStackTrace();
ActionErrors errors = new ActionErrors();
errors.add("hbm.session.query",
new ActionError("hbm.session.query"));
saveErrors(request, errors);
return mapping.findForward("failure");
} catch (Exception e1){
e1.printStackTrace();
ActionErrors errors = new ActionErrors();
errors.add("hbm.session.query",
new ActionError("hbm.session.query"));
saveErrors(request, errors);
return mapping.findForward("failure");
}
对时间总结:
l 在调试Hibernate利用Tomcat中连接池的时候一直没有删除hibernate.properties,导致错误原因没有办法查找
l 在调试Demo时,没有将测试的Servlet在Web.xml中删除,出现插入时主键重复
l Eclipse3这个版本在右键XML文件时,老是导致我CPU100%