Hibernate Utility : Hibernate Utility « Hibernate « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Hibernate » Hibernate UtilityScreenshots 
Hibernate Utility

/////////////////////////////////////////////////////////////////////////
package util;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

  public static final SessionFactory sessionFactory;

  static {
    try {
      // Create the SessionFactory from hibernate.cfg.xml
      Configuration config = new Configuration().configure();
      sessionFactory = config.buildSessionFactory();
    catch (Throwable ex) {
      // Make sure you log the exception, as it might be swallowed
      System.err.println("Initial SessionFactory creation failed." + ex);
      throw new ExceptionInInitializerError(ex);
    }
  }

  public static final ThreadLocal session = new ThreadLocal();

  public static Session currentSession() throws HibernateException {
    Session s = (Sessionsession.get();
    // Open a new Session, if this thread has none yet
    if (s == null) {
      s = sessionFactory.openSession();
      // Store it in the ThreadLocal variable
      session.set(s);
    }
    return s;
  }

  public static void closeSession() throws HibernateException {
    Session s = (Sessionsession.get();
    if (s != null)
      s.close();
    session.set(null);
  }
}


/////////////////////////////////////////////////////////////////////////


package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

/**
 @author joeyin
 *  
 */
public class ConnectionUtil {
  static Connection conn = null;

  private static Statement st = null;

  public static void openConnection() {
    try {
      Class.forName("org.hsqldb.jdbcDriver");
      System.out.println("Driver Loaded.");
      conn = DriverManager.getConnection("jdbc:hsqldb:mem:testdb""sa""");
      System.out.println("Got Connection.");
      st = conn.createStatement();
    catch (Exception e) {
      System.err.println("Got an exception! ");
      System.err.println(e.getMessage());
      e.printStackTrace();
      System.exit(0);
    }

  }
  public static String[] select(String sql) {
    ArrayList data = new ArrayList();
    try {
      ResultSet rs = st.executeQuery(sql);
      while (rs.next()) {
        data.add(rs.getString(1));
      }
    catch (Exception e) {
      System.out.println(sql);
      e.printStackTrace();
    }
    if (data.size() == 0) {
      return new String[0];
    }
    return (String[]) data.toArray(new String[data.size()]);
  }

  public static void update(String sql) {
    try {
      st.executeUpdate(sql);
    catch (Exception e) {
      e.printStackTrace();
      System.out.println(sql);
      //      System.exit(0);
    }
  }

  public static void closeConnection() {
    try {
      conn.close();
      System.out.println("Connection closed.");
    catch (Exception e) {
      e.printStackTrace();
    }
  }

}
           
       
Hibernate-Util.zip( 4,576 k)
Related examples in the same category
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.