mql数据库 连接

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();
			}
		}
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值