Java/Hibernate/Hibernate Utility

Материал из Java эксперт
Перейти к: навигация, поиск

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 = (Session) session.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 = (Session) session.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();
    }
  }
}