熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

在Tomcat下配置Hibernate的開發環境[3]

2013-11-23 20:45:26  來源: Java開源技術 

  第四個文件HibernateUtiljava

package hb;
import netsfhibernate*;
import netsfhibernatecfg*;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration()configure()buildSessionFactory();
} catch (HibernateException ex) {
throw new RuntimeException(Exception building SessionFactory:
+ exgetMessage() ex);
}
}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() throws HibernateException {
// Session s;
Session s = (Session) sessionget();
// Open a new Session if this Thread has none yet
if (s == null) {
s = sessionFactoryopenSession();
sessionset(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s = (Session) sessionget();
sessionset(null);
if (s != null)
sclose();
}
}

  第五個文件 addCatjsp

<%@ page contentType=text/html; charset=GB %>
<%@ page import=hb* netsfhibernate* %>
<html>
<head>
<title>test</title>
</head>
<body bgcolor=#ffffff]
<h>Test Hibernate</h>
<a href=getCatsjsp]View Cats[/url]<br>
<%
if(requestgetParameter(Go) != null) {
String name = requestgetParameter(name);
if(name == null || namelength() < ) {
outprintln(Name can not be empty!);
return;
}
SessionFactory sessionFactory;
netsfhibernateSession hsession = HibernateUtilcurrentSession();
Transaction tx = hsessionbeginTransaction();

[]  []  []  []  


From:http://tw.wingwit.com/Article/program/Java/ky/201311/29029.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.