package com.connor.mail;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SqlUtil {
	
	public static Connection connection = null;
	public static PreparedStatement ps = null;
	public static ResultSet rs = null;
	
	public static Connection getMySqlConnection() {
		String driver = "com.mysql.cj.jdbc.Driver";//reg.getString("SQL.DRIVER");
		String url ="jdbc:mysql://10.128.11.45:3399/parametric_design?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false";// reg.getString("SQL.URL"); ?useSSL=false
		String user ="root";// reg.getString("SQL.USER");
		String password ="CH6#e7aEL$";// reg.getString("SQL.PASSWORD");
		if (connection != null) {
			try {
				connection.close();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			connection = null;
		}
		try {
			// 加载驱动程序
			Class.forName(driver);
			// 1.getConnection()方法，连接MySQL数据库！！
			connection = DriverManager.getConnection(url, user, password);
			connection.setAutoCommit(false);
		} catch (ClassNotFoundException e) {
			// 数据库驱动类异常处理
			e.printStackTrace();
		} catch (SQLException e) {
			// 数据库连接失败异常处理
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return connection;
	}
	/**
	 * 释放资源(全部释放)
	 */
	public final static void freeAll() {
		free(SqlUtil.rs, SqlUtil.ps, SqlUtil.connection);
	}
	/**
	 * 查询
	 */
	public final static ResultSet read(String sql) throws SQLException {
		return read(sql, null);
	}

	/**
	 * 查询
	 */
	public final static ResultSet read(Object[] argments, String sql) throws SQLException {
		return read(sql, argments);
	}

	/**
	 * 查询(所有查询最终都要调用此方法，方法里面不处理异常，抛出后由调用者处理，方便在finally语句中释放资源)
	 * 
	 * @throws SQLException
	 */
	public final static ResultSet read(String sql, Object[] argments) throws SQLException {
		return SqlUtil.rs = SqlUtil.getPs(argments, sql).executeQuery();
	}
	 /** 得到Statement
	 */
	public final static PreparedStatement getPs(Object[] argments, String sql) throws SQLException {
		return getPs(sql, argments);
	}

	/**
	 * 得到Statement
	 */
	public final static PreparedStatement getPs(String sql, Object[] argments) throws SQLException {
		SqlUtil.ps = SqlUtil.connection.prepareStatement(sql);
		if (argments != null) {
			for (int i = 0; i < argments.length; i++) {
				SqlUtil.ps.setObject(i + 1, argments[i]);
			}
		}
		return SqlUtil.ps;
	}

	/**
	 * 释放资源(传满参数也要全部释放)
	 */
	public final static void free(ResultSet resultSet, Statement statement, Connection connection) {
		try {
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		} finally {
			try {
				if (statement != null) {
					try {
						statement.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
			} finally {
				if (connection != null) {
					try {
						connection.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
}
