package com.trs.web2frame.tool.tyjyh;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class ConnectionUtil {

	private static Properties config;// 配置文件
	
	private static Properties getProperties(String type) throws Exception{
		if (config == null) {
			config = new Properties();
			try {
				String path = getPath(type);
				config.load(ConnectionUtil.class.getClassLoader()
						.getResourceAsStream(path));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return config;
	}

	/**
	 * 读取proper
	 * 
	 * @param url
	 * @return
	 */
	public static Properties getProperties(InputStream inputStream) {
		if (config == null) {
			config = new Properties();
			try {
				config.load(inputStream);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return config;
	}

	public static void setProperties() {
	}

	public static void setProperties(InputStream inputStream) {
	}

	public static void setProperties(Properties properties) {
	}
	
	/**
	 * 创建数据库的连接
	 */
	public static Connection getConnection(String type) throws Exception {
		String driverClassName = getProperties(type).getProperty("jdbc.driver");
		String url = config.getProperty("jdbc.url");
		Class.forName(driverClassName);
		return DriverManager.getConnection(url,
				config.getProperty("jdbc.username"),
				config.getProperty("jdbc.password"));
	}

	public static Connection getConnectionBenji(String type) throws Exception {
		String driverClassName = getProperties(type).getProperty("jdbc.driver");
		String url = config.getProperty("jdbc.urlbj");
		Class.forName(driverClassName);
		return DriverManager.getConnection(url,
				config.getProperty("jdbc.usernamebj"),
				config.getProperty("jdbc.passwordbj"));
	}

	public static Connection getConnectionHy(String type) throws Exception {
		String driverClassName = getProperties(type).getProperty("jdbc.driver");
		String url = config.getProperty("jdbc.urlhy");
		Class.forName(driverClassName);
		return DriverManager.getConnection(url,
				config.getProperty("jdbc.usernamehy"),
				config.getProperty("jdbc.passwordhy"));
	}

	public static Connection getConnectionHyTest(String type) throws Exception {
		String driverClassName = getProperties(type).getProperty("jdbc.driver");
		String url = config.getProperty("jdbc.urlhytest");
		Class.forName(driverClassName);
		return DriverManager.getConnection(url,
				config.getProperty("jdbc.usernamehytest"),
				config.getProperty("jdbc.passwordhytest"));
	}

	public static Connection getConnectionLbFbj(String type) throws Exception {
		String driverClassName = getProperties(type).getProperty("jdbc.driver");
		String url = config.getProperty("jdbc.urltlbfbj");
		Class.forName(driverClassName);
		return DriverManager.getConnection(url,
				config.getProperty("jdbc.usernametlbfbj"),
				config.getProperty("jdbc.passwordtlbfbj"));
	}
	/**
	 * 关闭连接
	 */

	public static void closeConnection(ResultSet rs, Statement st,
			Connection conn) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				if (st != null) {
					try {
						st.close();
					} catch (SQLException e) {
						e.printStackTrace();
					} finally {
						try {
							conn.close();
						} catch (SQLException e) {
							e.printStackTrace();
						}
					}
				}
			}
		}
	}
	
	public static String getPath(String type)throws Exception{
		String path = null;
		if(type.equals("mysql")){
			path = "config/jdbc_mysql.properties";
		}else if(type.equals("oracle")){
			path = "config/jdbc_oracle.properties";
		}else{
			path = "config/jdbc_sqlserver.properties";
		}
		return path;
	}


}
