package com.seeyon.apps.src_gettaxrate.util;

import java.io.FileInputStream;
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;

import com.seeyon.ctp.common.SystemEnvironment;



	public class JdbcUtil {
	    private static String url;
	    private static String user;
	    private static String password;


	    // 获取数据库连接
	    public static Connection getConnection() {
	        Connection conn = null;
	        // 配置文件路径  
            String configFilePath = SystemEnvironment.getApplicationFolder() + "/WEB-INF/cfgHome/plugin/src_gettaxrate/pa.properties";
            // 加载配置文件  
            Properties properties = new Properties();
	        try (FileInputStream inputStream = new FileInputStream(configFilePath)) {  
	            properties.load(inputStream);  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        }
	        try {
	        	String driverClassName = properties.getProperty("jdbc_driver");
	            Class.forName(driverClassName);
	        	url = properties.getProperty("jdbc_url");
	        	user = properties.getProperty("jdbc_user");
	        	password = properties.getProperty("jdbc_password");
	            conn = DriverManager.getConnection(url, user, password);
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        return conn;
	    }

	    // 释放资源
	    public static void close(Connection conn, Statement stat, ResultSet rs) {
	        close(conn, stat);
	        if (rs != null) {
	            try {
	                rs.close();
	            } catch (SQLException e) {
	                e.printStackTrace();
	            }
	        }
	    }

	    // 释放资源
	    public static void close(Connection conn, Statement stat) {
	        if (conn != null) {
	            try {
	                conn.close();
	            } catch (SQLException e) {
	                e.printStackTrace();
	            }
	        }
	        if (stat != null) {
	            try {
	                stat.close();
	            } catch (SQLException e) {
	                e.printStackTrace();
	            }
	        }
	    }
	
}
