Hibernate Utility : Hibernate Utility « Hibernate « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
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
w_w_w.__j_a___va_2__s.c___o__m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.