---
title: "mql数据库 连接"
author: "blaiu"
date: "2010-01-13T10:57:30Z"
tags: ["Hibernate", "JDBC", "MySQL", "XML", "thread"]
categories: ["数据库"]
slug: "mql"
draft: false
lastmod: "2010-12-05T02:25:20Z"
id: 569033
url: "https://blaiu.iteye.com/blog/569033"
---

```java

public final class HibernateUtil {
	
	private static SessionFactory sessionFactory = null;
	
	private static String path = Common.decode(Thread.currentThread().getContextClassLoader().getResource("../../config.properties").getPath());

	private static String message = null;
	
	public static String getMessage() {
		return message;
	}

	/**
	 * 
	 * @此方法描述的是：同步创建session工厂
	 * void
	 */
	public static synchronized void buildSessionFactory() {
		if(sessionFactory == null) {
			try {
				Properties properties = new Properties();
				InputStream fis = new FileInputStream(path);
				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) {
				System.out.println("请检查你的hibernate.cfg.xml 文件是否配置正确");
				message = "Create sessionFactory Exception! "+e.getMessage();
			}
		}
	}
	
	/**
	 * 
	 * @此方法描述的是：测试数据库连接
	 * @param dbhost	服务器名
	 * @param dbport	端口
	 * @param dbname	数据库名
	 * @param dbuser	用户
	 * @param dbpw		密码
	 * @return
	 * boolean
	 */
	public static boolean mysql_connect(String dbhost, String dbport, String dbname, String dbuser, String dbpw) {
		Connection connection = null;
		boolean flag = false;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection("jdbc:mysql://"+dbhost+":"+dbport+"/"+dbname, dbuser, dbpw);
			if(connection != null) {
				if(!connection.isClosed()) {
					connection.close();
					connection = null;
				}
				return true;
			}
		} catch (Exception e) {
			System.out.println("数据库连接异常， 请检查你的config.properties配置文件是否配置正确");
			message = e.getMessage();
		}
		return flag;
	}
	
	
	/**
	 * 
	 * @此方法描述的是：返回SessionFactory
	 * @return
	 * SessionFactory
	 */
	public static SessionFactory getSessionFactory() {
		if(sessionFactory == null) {
			buildSessionFactory();
		}
		return sessionFactory;
	}
	
	/**
	 * 
	 * @此方法描述的是：返回session
	 * @return
	 * Session
	 */
	public static Session getSession() {
		if(sessionFactory == null) {
			buildSessionFactory();
		}
		return sessionFactory.getCurrentSession();
	}
	
	/**
	 * 
	 * @此方法描述的是：从新创建SessionFactory
	 * void
	 */
	public static void rebuildSessionFactory() {
		if(sessionFactory == null) {
			try {
				Properties properties = new Properties();
				InputStream fis = new FileInputStream(path);
				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) {
				System.out.println("请检查你的hibernate.cfg.xml 文件是否配置正确");
				message = "Create sessionFactory Exception! "+e.getMessage();
			}
		}
	}
}

```