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 ConnectionUtil2 {

	private static Properties config;// 配置文件
	
	private static Properties getProperties() throws Exception{
		if (config == null) {
			config = new Properties();
			try {
				config.load(ConnectionUtil2.class.getClassLoader()
						.getResourceAsStream("config/jdbc_WCMBase.properties"));
			} 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() throws Exception {
		String driverClassName = getProperties().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 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 void closeConnection(Statement st,Connection conn) {
		if (st != null) {
			try {
				st.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
