HibernateUtil Hibernate数据库连接工具类
HibernateUtil.java
package cn.jsprun;import java.io.FileInputStream;import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.util.Properties;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;/** * Hibernate数据库连接工具 * 参考:JspRun 源码 * @author SUN * */public final class HibernateUtil { // final 不能被继承/** sf 只提供类内部访问,且只有一份实例。 */private static SessionFactory sessionFactory = null;private static String message = null;/** * 加载到内存时第一个被初始化,且只初始化一次。 */static {buildSessionFactory();// 构建SF}/** * 构建数据库全局会话工厂对象 */public static synchronized void buildSessionFactory() {// 检查sf不为null才去构建sf对象if (sessionFactory == null) { // true 为nulltry {// 读取数据库连接配置信息Properties properties = new Properties();InputStream fis = new FileInputStream("D:/workspaceee/netzz/src/config.properties");properties.load(fis);fis.close();String dbhost = properties.getProperty("dbhost");String dbport = properties.getProperty("dbport");String dbname = properties.getProperty("dbname");String dbuser = properties.getProperty("dbuser");String dbpw = properties.getProperty("dbpw");// 检查数据库配置信息是否可以连接if (mysql_connect(dbhost, dbport, dbname, dbuser, dbpw)) { // true 可用// 创建并设置hibernate属性对象Properties extraProperties = new Properties();extraProperties.setProperty("hibernate.connection.url", "jdbc:mysql://" + dbhost + ":" + dbport + "/" + dbname + "?zeroDateTimeBehavior=convertToNull");extraProperties.setProperty("hibernate.connection.username", dbuser);extraProperties.setProperty("hibernate.connection.password", dbpw);// 构建SessionFactoryConfiguration configuration = new Configuration(); // 创建hibernate配置对象configuration = configuration.configure("hibernate.cfg.xml");// 获得hibernate配置文件configuration = configuration.addProperties(extraProperties);// 添加属性对象sessionFactory = configuration.buildSessionFactory();// 构建SF数据库会话工厂对象// 释放不在使用的资源extraProperties = null;configuration = null;}properties = null;} catch (Exception e) {message = "Create sessionFactory Exception! " + e.getMessage();}}}/** * 获得sf * @return */public static SessionFactory getSessionFactory() {if (sessionFactory == null) {buildSessionFactory();}return sessionFactory;}/** * 获得session * @return */public static Session getSession() {if (sessionFactory == null) {buildSessionFactory();}return sessionFactory.getCurrentSession();}/** * 重新构建sf */public static void rebuildSessionFactory() {try {Properties properties = new Properties();InputStream fis = new FileInputStream("c:/config.properties");properties.load(fis);fis.close();String dbhost = properties.getProperty("dbhost");String dbport = properties.getProperty("dbport");String dbname = properties.getProperty("dbname");String dbuser = properties.getProperty("dbuser");String dbpw = properties.getProperty("dbpw");if (mysql_connect(dbhost, dbport, dbname, dbuser, dbpw)) {Properties extraProperties = new Properties();extraProperties.setProperty("hibernate.connection.url", "jdbc:mysql://" + dbhost + ":" + dbport + "/" + dbname+ "?zeroDateTimeBehavior=convertToNull");extraProperties.setProperty("hibernate.connection.username", dbuser);extraProperties.setProperty("hibernate.connection.password", dbpw);Configuration configuration = new Configuration();configuration = configuration.configure("hibernate.cfg.xml");configuration = configuration.addProperties(extraProperties);sessionFactory = configuration.buildSessionFactory();extraProperties = null;configuration = null;}properties = null;} catch (Exception e) {message = "Create sessionFactory Exception! " + e.getMessage();}}/** * 测试MySQL数据库连接,成功返回true,其他返回false. * * @param dbhost * @param dbport * @param dbname * @param dbuser * @param dbpw * @return */public static boolean mysql_connect(String dbhost, String dbport, String dbname, String dbuser, String dbpw) {Connection conn = null;boolean flag = false;try {Class.forName("com.mysql.jdbc.Driver");conn = DriverManager.getConnection("jdbc:mysql://" + dbhost + ":" + dbport + "/" + dbname, dbuser, dbpw);if (conn != null) {if (!conn.isClosed()) {conn.close();conn = null;}return true;}} catch (Exception ex) {message = ex.getMessage();}return flag;}public static String getMessage() {return message;}/** * 测试数据库连接,获得session * @param args */public static void main(String[] args) {System.out.println("start connection ...");HibernateUtil.getSession();System.out.println("connected success!");}}?
config.properties
dbhost = localhostdbport=3306dbuser = rootdbpw = rootdbname = netzzpconnect = 0tablepre = jrun_database = mysqldbcharset =charset = utf-8headercharset = 0tplrefresh = 1version=5.1
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" ><hibernate-configuration><session-factory><property name="connection.driver_class">com.mysql.jdbc.Driver</property><property name="dialect">org.hibernate.dialect.MySQLDialect</property><property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> <property name="hibernate.c3p0.acquireIncrement">10</property><property name="hibernate.c3p0.max_size">250</property><property name="hibernate.c3p0.min_size">10</property><property name="hibernate.c3p0.timeout">1800</property><property name="hibernate.c3p0.max_statements">100</property><property name="hibernate.c3p0.numHelperThreads">3</property><property name="hibernate.c3p0.idle_test_period">300</property><property name="hibernate.c3p0.acquire_increment">5</property><property name="hibernate.c3p0.maxIdleTime">600</property> <property name="hibernate.c3p0.idleConnectionTestPeriod">60</property> <property name="current_session_context_class">thread</property><property name="hbm2ddl.auto">none</property><property name="show_sql">false</property><property name="connection.useUnicode">true</property><property name="connection.characterEncoding">UTF-8</property><property name="connection.characterSetResults">UTF-8</property><mapping resource="com/mdaxue/storage/model/TArticle.hbm.xml" /></session-factory></hibernate-configuration>
??
?
?