package com.langtech.plm.form;

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

public class DbPool {

    public static Connection connection = null;
    public static PreparedStatement ps = null;
    public static ResultSet rs = null;

    private static DbPool instance = null;

    public static synchronized Connection getConnection(String url, String name) {
        if (instance == null) {
            instance = new DbPool();
        }
        return instance._getConnection(url, name);
    }

    private Connection _getConnection(String url, String name) {
        try {
            String sDBDriver = null;
            String sConnection = null;
            String sUser = null;
            String sPassword = null;
            Properties p = new Properties();
//			InputStream is = getClass().getResourceAsStream(
//					"/sqlconn.properties");
//			p.load(is);
            sDBDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
            sConnection = "jdbc:sqlserver://" + url + ":1433; DatabaseName=" + name;// p.getProperty("Connection", "2");
            sUser = "infodba";// p.getProperty("User", "");
            sPassword = "infodba";// p.getProperty("Password", "");

            if (sDBDriver == null || sConnection == null || sUser == null || sPassword == null) {

                System.out.println("请检查数据库首选项是否配置正确");

            }

            Class.forName(sDBDriver);
            connection = DriverManager.getConnection(sConnection, sUser, sPassword);
            return connection;
        } catch (Exception se) {
            System.out.println(se);
        }
        return null;
    }

    public final static ResultSet read(String sql, Object[] argments) throws SQLException {
        return DbPool.rs = DbPool.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 {
        DbPool.ps = DbPool.connection.prepareStatement(sql);
        if (argments != null) {
            for (int i = 0; i < argments.length; i++) {
                DbPool.ps.setObject(i + 1, argments[i]);
            }
        }
        return DbPool.ps;
    }

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

    public static void executeUpdateSql(String sql) throws SQLException {
        Statement st = connection.createStatement();
        try {
            connection.setAutoCommit(false);
            // System.out.println("sql===" + sql);
            int i = st.executeUpdate(sql);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            if (st != null)
                st.close();
            connection.rollback();
            connection.close();
        } finally {
            if (st != null)
                st.close();
            // connection.close();
        }

    }

    public static void executeDeleteSql(String sql) throws SQLException {
        Statement st = connection.createStatement();
        try {
            connection.setAutoCommit(false);
            // System.out.println("sql===" + sql);
            st.executeUpdate(sql);
            connection.commit();
        } catch (SQLException e) {
            if (st != null)
                st.close();
            connection.rollback();
            connection.close();
        } finally {
            if (st != null)
                st.close();
            connection.close();
        }

    }

    /**
     * 增加
     */
    public final static int write(String sql, Object[] argments) {

        return update(sql, argments);
    }

    /**
     * 修改(增删改最终都要调用此方法，此处进行异常处理，关闭除Connection以为的对象)
     */
    public final static int update(String sql, Object[] argments) {
        int i = -1;
        try {
            i = DbPool.getPs(argments, sql).executeUpdate();
            DbPool.connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            free(DbPool.rs, DbPool.ps);
        }
        return i;
    }

    public static ResultSet executeSelectSql(String sql) throws SQLException {
        Statement st = connection.createStatement();
        try {
            connection.setAutoCommit(false);
            // System.out.println("sql===" + sql);
            rs = st.executeQuery(sql);
            // conn.commit();
        } catch (SQLException e) {
            if (st != null)
                st.close();
            connection.rollback();
            connection.close();
        }
        return rs;

    }

    /**
     * 释放资源
     */
    public final static void free(ResultSet resultSet) {
        free(resultSet, null, null);
    }

    /**
     * 释放资源
     */
    public final static void free(Statement statement) {
        free(null, statement, null);
    }

    /**
     * 释放资源
     */
    public final static void free(Connection connection) {
        free(null, null, connection);
    }

    /**
     * 释放资源
     */
    public final static void free(ResultSet resultSet, Statement statement) {
        free(resultSet, statement, null);
    }

    /**
     * 释放资源
     */
    public final static void free(Statement statement, Connection connection) {
        free(null, statement, connection);
    }

    /**
     * 释放资源(不传参数默认部分)
     */
    public final static void free() {
        free(DbPool.rs, DbPool.ps);
    }

    /**
     * 释放资源(全部释放)
     */
    public final static void freeAll() {
        free(DbPool.rs, DbPool.ps, DbPool.connection);
    }

    /**
     * 释放资源(传满参数也要全部释放)
     */
    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();
                    }
                }
            }
        }
    }
}
