package com.seeyon.apps.src_contract.lease.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.seeyon.apps.src_contract.lease.kit.PropJDBCKit;


/**
 * jdbc工具类
 * 2021-06-08
 * @author huangzhengguo
 *
 */
	public class JdbcUtil {
	    private static String url;
	    private static String user;
	    private static String password;

	    static {
	        // 使用properties加载属性文件
//	        Properties prop = new Properties();
	        try {
//	            InputStream is = JdbcUtil.class.getClassLoader().getResourceAsStream("pa.properties");
//	            prop.load(is);
	            // 注册驱动（获取属性文件中的数据）
	            String driverClassName = PropJDBCKit.getProp(PropJDBCKit.JDBCDRIVER);
	            Class.forName(driverClassName);
	            // 获取属性文件中的url,username,password
	            url = PropJDBCKit.getProp(PropJDBCKit.JDBCURL);
	            user = PropJDBCKit.getProp(PropJDBCKit.JDBCUSER);
	            password = PropJDBCKit.getProp(PropJDBCKit.JDBCPASSWORD);
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	    }

	    // 获取数据库连接
	    public static Connection getConnection() {
	        Connection conn = null;
	        try {
	            conn = DriverManager.getConnection(url, user, password);
	        } catch (SQLException 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();
	            }
	        }
	    }
	
}
