/*
 * Decompiled with CFR 0.152.
 */
package com.hst.common.util;

import com.hst.common.exception.DBReleaseException;
import com.hst.common.exception.DPException;
import com.hst.common.util.Assert;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBUtil {
    public static void close(ResultSet rs, Statement st, Connection conn) throws DBReleaseException {
        if (rs != null) {
            try {
                rs.close();
                rs = null;
            }
            catch (SQLException sqle) {
                throw new DBReleaseException(sqle);
            }
        }
        if (st != null) {
            try {
                st.close();
                st = null;
            }
            catch (SQLException sqle) {
                throw new DBReleaseException(sqle);
            }
        }
        if (conn != null) {
            try {
                conn.close();
                conn = null;
            }
            catch (SQLException sqle) {
                throw new DBReleaseException(sqle);
            }
        }
    }

    public static void close(ResultSet rs, Statement st) throws DBReleaseException {
        if (rs != null) {
            try {
                rs.close();
                rs = null;
            }
            catch (SQLException sqle) {
                throw new DBReleaseException(sqle);
            }
        }
        if (st != null) {
            try {
                st.close();
                st = null;
            }
            catch (SQLException sqle) {
                throw new DBReleaseException(sqle);
            }
        }
    }

    public static void close(ResultSet rs, PreparedStatement st) throws DBReleaseException {
        if (rs != null) {
            try {
                rs.close();
                rs = null;
            }
            catch (SQLException sqle) {
                throw new DBReleaseException(sqle);
            }
        }
        if (st != null) {
            try {
                st.close();
                st = null;
            }
            catch (SQLException sqle) {
                throw new DBReleaseException(sqle);
            }
        }
    }

    public static void close(ResultSet rs, PreparedStatement st, Connection conn) throws DBReleaseException {
        if (rs != null) {
            try {
                rs.close();
                rs = null;
            }
            catch (SQLException sqle) {
                throw new DBReleaseException(sqle);
            }
        }
        if (st != null) {
            try {
                st.close();
                st = null;
            }
            catch (SQLException sqle) {
                throw new DBReleaseException(sqle);
            }
        }
        if (conn != null) {
            try {
                conn.close();
                conn = null;
            }
            catch (SQLException sqle) {
                throw new DBReleaseException(sqle);
            }
        }
    }

    public static void close(ResultSet rs, CallableStatement cs, Connection conn) throws DBReleaseException {
        if (rs != null) {
            try {
                rs.close();
                rs = null;
            }
            catch (SQLException sqle) {
                throw new DBReleaseException(sqle);
            }
        }
        if (cs != null) {
            try {
                cs.close();
                cs = null;
            }
            catch (SQLException sqle) {
                throw new DBReleaseException(sqle);
            }
        }
        if (conn != null) {
            try {
                conn.close();
                conn = null;
            }
            catch (SQLException sqle) {
                throw new DBReleaseException(sqle);
            }
        }
    }

    public static void close(ResultSet rs, CallableStatement cs) throws DBReleaseException {
        if (rs != null) {
            try {
                rs.close();
                rs = null;
            }
            catch (SQLException sqle) {
                throw new DBReleaseException(sqle);
            }
        }
        if (cs != null) {
            try {
                cs.close();
                cs = null;
            }
            catch (SQLException sqle) {
                throw new DBReleaseException(sqle);
            }
        }
    }

    public static void close(Connection conn) throws DBReleaseException {
        try {
            if (conn != null && !conn.isClosed()) {
                conn.close();
                conn = null;
            }
        }
        catch (SQLException sqle) {
            throw new DBReleaseException(sqle);
        }
    }

    public static void close(Connection conn1, Connection conn2) throws DBReleaseException {
        try {
            if (conn1 != null && !conn1.isClosed()) {
                conn1.close();
                conn1 = null;
            }
            if (conn2 != null && !conn2.isClosed()) {
                conn2.close();
                conn2 = null;
            }
        }
        catch (SQLException sqle) {
            throw new DBReleaseException(sqle);
        }
    }

    public static void close(Connection conn1, Connection conn2, Connection conn3) throws DBReleaseException {
        try {
            if (conn1 != null && !conn1.isClosed()) {
                conn1.close();
                conn1 = null;
            }
            if (conn2 != null && !conn2.isClosed()) {
                conn2.close();
                conn2 = null;
            }
            if (conn3 != null && !conn3.isClosed()) {
                conn3.close();
                conn3 = null;
            }
        }
        catch (SQLException sqle) {
            throw new DBReleaseException(sqle);
        }
    }

    public static void rollback(Connection conn) {
        try {
            if (conn != null && !conn.isClosed()) {
                conn.rollback();
                conn = null;
            }
        }
        catch (SQLException sqle) {
            sqle.printStackTrace();
        }
    }

    public static boolean loadDriver(String driverClassName) {
        try {
            Class.forName(driverClassName).newInstance();
            return true;
        }
        catch (ClassNotFoundException classNotFoundException) {
            return false;
        }
        catch (IllegalAccessException illegalAccessException) {
            return true;
        }
        catch (InstantiationException instantiationException) {
            return false;
        }
        catch (Throwable throwable) {
            return false;
        }
    }

    public static String getSequence(Connection conn, String tableName) throws DPException {
        if (conn == null) {
            throw new DPException("\u53c2\u6570 conn \u4e0d\u80fd\u4e3a\u7a7a");
        }
        if (tableName == null) {
            throw new DPException("\u53c2\u6570 tableName \u4e0d\u80fd\u4e3a\u7a7a");
        }
        StringBuffer SQL = new StringBuffer("SELECT " + tableName + ".NEXTVAL FROM DUAL");
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sequence = null;
        try {
            try {
                ps = conn.prepareStatement(SQL.toString());
                rs = ps.executeQuery();
                if (rs.next()) {
                    sequence = rs.getString(1);
                }
            }
            catch (SQLException se) {
                throw new DPException(se);
            }
            catch (Throwable t) {
                throw new DPException(t);
            }
        }
        catch (Throwable throwable) {
            try {
                DBUtil.close(rs, ps);
            }
            catch (DBReleaseException dre) {
                dre.printStackTrace();
            }
            throw throwable;
        }
        try {
            DBUtil.close(rs, ps);
        }
        catch (DBReleaseException dre) {
            dre.printStackTrace();
        }
        return sequence;
    }

    /*
     * Loose catch block
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    public static int getMaxXh(Connection yysConn, String sql, String[] paramValues) throws DPException {
        ResultSet rs;
        PreparedStatement pst;
        block13: {
            int n;
            Assert.checkNull("yysConn", yysConn);
            Assert.checkNullOrEmpty("sql", sql);
            Assert.checkNull("paramValues", paramValues);
            pst = null;
            rs = null;
            try {
                pst = yysConn.prepareStatement(sql.toString());
                int i = 0;
                while (true) {
                    if (i >= paramValues.length) {
                        rs = pst.executeQuery();
                        if (!rs.next()) break block13;
                        n = rs.getInt(1) + 1;
                        break;
                    }
                    pst.setString(i + 1, paramValues[i]);
                    ++i;
                }
            }
            catch (SQLException se) {
                try {
                    throw new DPException(se);
                    catch (Throwable t) {
                        throw new DPException(t);
                    }
                }
                catch (Throwable throwable) {
                    try {
                        DBUtil.close(rs, pst);
                        throw throwable;
                    }
                    catch (DBReleaseException se2) {
                        se2.printStackTrace();
                    }
                    throw throwable;
                }
            }
            try {
                DBUtil.close(rs, pst);
                return n;
            }
            catch (DBReleaseException se) {
                se.printStackTrace();
            }
            return n;
        }
        try {
            DBUtil.close(rs, pst);
            return 0;
        }
        catch (DBReleaseException se) {
            se.printStackTrace();
        }
        return 0;
    }
}
