81,092
社区成员
发帖
与我相关
我的任务
分享
public class ReportDaoImpl implements ReportDao {
/**
* queryTable
*
* @param sql QueryPageVO
* @param isPage boolean 是否用分页
* @return List
* @todo Implement this com.aspire.omr.dao.ReportDao method
* @throws OmrException
*/
public List queryTable(String sql, boolean isPage) throws OmrException {
OmrLog.sql(sql);
long startTime = System.currentTimeMillis();
if (sql == null)
return new ArrayList();
List list = new ArrayList();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
Session session = null;
try {
session = HibernateSessionFactory.currentSession();
conn = session.connection();
} catch (OmrException oe) {
throw oe;
}
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
int size = rs.getMetaData().getColumnCount();
if (isPage)
--size;
String[] col = null;
while (rs.next()) {
col = new String[size];
for (int i = 0; i < size; ++i) {
col[i] = rs.getString(i + 1);
}
list.add(col);
}
} catch (SQLException ex) {
OmrLog.error("查询数据库失败,SQLExcetion!", ex);
throw new OmrException(ExceptionConstant.OMR_QUERY);
} finally {
DBConnection.close(conn, stmt, rs);
HibernateSessionFactory.closeSession();
}
OmrLog.sql("time:" + (System.currentTimeMillis() - startTime));
return list;
}
}
public class HibernateInit
implements ServletContextListener {
public void contextDestroyed(ServletContextEvent servletContextEvent) {
}
public void contextInitialized(ServletContextEvent servletContextEvent) {
try {
Configuration conf = new Configuration().configure();
SessionFactory sf = conf.buildSessionFactory();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
public class HibernateSessionFactory {
/**
* Location of hibernate.cfg.xml file.
* NOTICE: Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file. That
* is place the config file in a Java package - the default location
* is the default Java package.<br><br>
* Examples: <br>
* <code>CONFIG_FILE_LOCATION = "/hibernate.conf.xml".
* CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".</code>
*/
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";//ServerInfo.getWebLogicConfigPath()+"database"+System.getProperty("file.separator")+"hibernate.cfg.xml";
/** Holds a single instance of Session */
private static final ThreadLocal threadLocal = new ThreadLocal();
/** The single instance of hibernate configuration */
private static final Configuration cfg = new Configuration();
/** The single instance of hibernate SessionFactory */
private static SessionFactory sessionFactory;
/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
try {
//cfg.configure(CONFIG_FILE_LOCATION);
//sessionFactory = cfg.buildSessionFactory();
Context ctx = new InitialContext();
sessionFactory = (SessionFactory) ctx.lookup("hibernate/session_factory");
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}
return session;
}
/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
/**
* Default constructor.
*/
private HibernateSessionFactory() {
}
}