//Source file: C:\\Rose2Java\\zhsoft\\dbcon\\DBCon.java
package com.dbms.dbcon;

import com.coolservlets.beans.method.FileManager;
import com.report.NullFileCell;
import org.apache.commons.dbcp.BasicDataSource;
import psdi.mbo.Mbo;
import psdi.security.ConnectionKey;
import psdi.server.MXServer;
import psdi.util.MXSession;

import java.io.*;
import java.rmi.RemoteException;
import java.sql.*;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
//import java.util.Date;

/**
 * 数据库操作类
 *
 * @author 雨亦奇(zhsoft88 @ sohu.com)
 * @version 1.0
 * @since 2003.05.18
 */
public class DBCon {
    private static BasicDataSource datasource = null;
    public static HashMap hmKeyConMap = new HashMap();

    /**
     * 根据Mbo来执行sql语句
     *
     * @param sql String
     * @param mbo Mbo
     * @return int
     * @throws Exception
     */
    public static int UpdateDB(String sql, Mbo mbo) throws Exception {
        if (mbo == null) {
            return UpdateDB(sql);
        } else {
            return UpdateDB(sql, mbo.getUserInfo().getConnectionKey());
        }
    }

    public static int UpdateDB(String sql) throws Exception {
        ConnectionKey connKey = null;
        return UpdateDB(sql, connKey);
    }

    public static int UpdateDB(String sql, ConnectionKey connKey) throws
            Exception {
        int updateRecordCounter = 0;
        Connection dbCon = null;
        //com.encryption.PrefsProduct prefsproduct = new com.encryption.PrefsProduct();
        //boolean authis = prefsproduct.AuthorizationIs("/com/sunway/spc","mg{u");
        boolean authis = true;
        if (!authis) return 0;
        try {

            if (connKey == null) {
                dbCon = getDBConn();
            } else {
                dbCon = getMaxDBConn(connKey);

            }
            Statement stmt = dbCon.createStatement();
            try {
                updateRecordCounter = stmt.executeUpdate(sql);
                //dbCon.commit();
            } catch (SQLException sqle) {
                sqle.printStackTrace();
                if (connKey == null) {
                    if (!dbCon.isClosed()) {
                        dbCon.close();
                    }
                }
                try {
                    throw new SQLException("SQL执行出错:" + sqle.toString());
                } catch (Exception ioexception) {
                    IllegalArgumentException illegalargumentexception = new IllegalArgumentException(ioexception.toString());
                    Fake(illegalargumentexception, ioexception);
                    throw illegalargumentexception;
                }
            }

            stmt.close();

            if (connKey == null) {
                if (!dbCon.isClosed()) {
                    dbCon.close();
                }
            }

        } finally {
            if (connKey == null) {
                if (!dbCon.isClosed()) {
                    dbCon.close();
                }
            } else {
                MXServer.getMXServer().getDBManager().freeConnection(connKey);
            }

        }
        return updateRecordCounter;
    }

    public static int UpdateDBFormat(String sql, ConnectionKey connKey) throws Exception {
        sql = formatsql(sql);
        int updateRecordCounter = 0;
        Connection dbCon = null;
        //com.encryption.PrefsProduct prefsproduct = new com.encryption.PrefsProduct();
        //boolean authis = prefsproduct.AuthorizationIs("/com/sunway/spc","mg{u");
        boolean authis = true;
        if (!authis) return 0;
        try {

            if (connKey == null) {
                dbCon = getDBConn();
            } else {
                dbCon = getMaxDBConn(connKey);

            }
            Statement stmt = dbCon.createStatement();
            try {
                updateRecordCounter = stmt.executeUpdate(sql);
            } catch (SQLException sqle) {
                sqle.printStackTrace();
                if (connKey == null) {
                    if (!dbCon.isClosed()) {
                        dbCon.close();
                    }
                }
                try {
                    throw new SQLException("SQL执行出错:" + sqle.toString());
                } catch (Exception ioexception) {
                    IllegalArgumentException illegalargumentexception = new IllegalArgumentException(ioexception.toString());
                    Fake(illegalargumentexception, ioexception);
                    throw illegalargumentexception;
                }
            }

            stmt.close();

            if (connKey == null) {
                if (!dbCon.isClosed()) {
                    dbCon.close();
                }
            }

        } finally {
            if (connKey == null) {
                if (!dbCon.isClosed()) {
                    dbCon.close();
                }
            } else {
                MXServer.getMXServer().getDBManager().freeConnection(connKey);
            }

        }
        return updateRecordCounter;
    }

    public static java.util.ArrayList listAll(String sql, MXSession mxs) throws Exception {
        return listAll(sql, mxs.getUserInfo().getConnectionKey());
    }

    public static java.util.ArrayList listAll(String sql, Mbo mbo) throws
            Exception {
        if (mbo == null) {
            return listAll(sql);
        } else {
            return listAll(sql, mbo.getUserInfo().getConnectionKey());
        }
    }

    public static java.util.ArrayList listAll(String sql) throws Exception {
        ConnectionKey connKey = null;
        return listAll(sql, connKey);
    }

    public static java.util.ArrayList listAll(String sql, ConnectionKey connKey) throws
            Exception {
        Connection connection = null;
        //System.out.println("systemName:"+systemName);
        //com.encryption.PrefsProduct prefsproduct = new com.encryption.PrefsProduct();
        //boolean authis = prefsproduct.AuthorizationIs("/com/sunway/spc","mg{u");
        boolean authis = true;
        if (!authis) return null;
        if (connKey == null) {
            connection = getDBConn();
        } else {
            connection = getMaxDBConn(connKey);

        }
        //System.out.println("connection:"+connection);
        java.util.ArrayList al = new java.util.ArrayList();
        try {
            Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            ResultSet resultSet = statement.executeQuery(sql);
            ResultSetMetaData resultSetmd = resultSet.getMetaData();
            //System.out.println("resultSetmd getColumnCount()="+resultSetmd.getColumnCount());
            while (resultSet.next()) {
                HashMap hb = new HashMap();
                for (int i = 1; i <= resultSetmd.getColumnCount(); i++) {
                    String value = resultSet.getString(resultSetmd.getColumnName(i)) == null ?
                            "" : resultSet.getString(resultSetmd.getColumnName(i));
                    hb.put(resultSetmd.getColumnName(i).toLowerCase(), value);
                }
                al.add(hb);

            }
            resultSet.close();
            statement.close();
            if (connKey == null) {
                connection.close();
            }
        } catch (Exception ex) {
            if (connKey == null) {
                if (!connection.isClosed()) {
                    connection.close();

                }
            }
            try {
                throw new Exception("查询出错:" + ex.getMessage() + "::::" + sql);
            } catch (Exception ioexception) {
                IllegalArgumentException illegalargumentexception = new IllegalArgumentException(ioexception.toString());
                Fake(illegalargumentexception, ioexception);
                throw illegalargumentexception;
            }
        } finally {
            if (connKey == null) {
                if (!connection.isClosed()) {
                    connection.close();
                }
            } else {
                MXServer.getMXServer().getDBManager().freeConnection(connKey);
            }

        }
        return al;
    }

    public static java.util.ArrayList listAllFormat(String sql, ConnectionKey connKey) throws Exception {
        sql = formatsql(sql);
        Connection connection = null;
        //System.out.println("systemName:"+systemName);
        //com.encryption.PrefsProduct prefsproduct = new com.encryption.PrefsProduct();
        //boolean authis = prefsproduct.AuthorizationIs("/com/sunway/spc","mg{u");
        boolean authis = true;
        if (!authis) return null;
        if (connKey == null) {
            connection = getDBConn();
        } else {
            connection = getMaxDBConn(connKey);
        }
        java.util.ArrayList al = new java.util.ArrayList();
        try {
            Statement statement = connection.createStatement(ResultSet.
                    TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            ResultSet resultSet = statement.executeQuery(sql);
            ResultSetMetaData resultSetmd = resultSet.getMetaData();
            while (resultSet.next()) {
                HashMap hb = new HashMap();
                for (int i = 1; i <= resultSetmd.getColumnCount(); i++) {
                    String value = resultSet.getString(resultSetmd.getColumnName(i)) == null ?
                            "" : resultSet.getString(resultSetmd.getColumnName(i));
                    hb.put(resultSetmd.getColumnName(i).toLowerCase(), value);
                }
                al.add(hb);

            }
            resultSet.close();
            statement.close();
            if (connKey == null) {
                connection.close();
            }
        } catch (Exception ex) {
            if (connKey == null) {
                if (!connection.isClosed()) {
                    connection.close();

                }
            }
            try {
                throw new Exception("查询出错:" + ex.getMessage() + "::::" + sql);
            } catch (Exception ioexception) {
                IllegalArgumentException illegalargumentexception = new IllegalArgumentException(ioexception.toString());
                Fake(illegalargumentexception, ioexception);
                throw illegalargumentexception;
            }
        } finally {
            if (connKey == null) {
                if (!connection.isClosed()) {
                    connection.close();
                }
            } else {
                MXServer.getMXServer().getDBManager().freeConnection(connKey);
            }

        }
        return al;
    }

    /**
     * 通过 sql 语句查询，返回标准 的 xml
     *
     * @param sql
     * @param connKey
     * @param StardXmlFld 所有通过 xml 格式定义的 字段集合
     * @param NotInFld    不包含的字段
     * @return
     * @throws Exception
     */
    public static String listAllFormatXml(String sql, ConnectionKey connKey, String[] NotInFld, String StardXmlFld) throws Exception {
        StardXmlFld = StardXmlFld.toUpperCase();
        //System.out.println("sql 1 = "+sql);
        HashMap hmColumnNotIn = new HashMap();
        for (int n = 0; n < NotInFld.length; n++) {
            String fld = NotInFld[n];
            //System.out.println("fld 1 = "+fld);
            hmColumnNotIn.put(fld.toUpperCase(), fld.toUpperCase());
        }
        Connection connection = null;
        String sqlTotal = "";
        try {
            //System.out.println("systemName:"+systemName);
            //com.encryption.PrefsProduct prefsproduct = new com.encryption.PrefsProduct();
            //boolean authis = prefsproduct.AuthorizationIs("/com/sunway/spc","mg{u");
            boolean authis = true;
            //System.out.println("authis = "+ authis);
            if (!authis) return null;
            if (connKey == null) {
                connection = getDBConn();
            } else {
                connection = getMaxDBConn(connKey);
            }
            //System.out.println("connection = "+ connection);
            Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ResultSet resultSet = statement.executeQuery(sql);
            ResultSetMetaData resultSetmd = resultSet.getMetaData();
            while (resultSet.next()) {
                HashMap hmColumnExist = new HashMap();
                HashMap hb = new HashMap();
                String sqlLine = "";
                for (int i = 1; i <= resultSetmd.getColumnCount(); i++) {

                    if (hmColumnExist.containsKey(resultSetmd.getColumnName(i))) {
                        continue;
                    }
                    if (hmColumnNotIn.containsKey((resultSetmd.getColumnName(i)).toUpperCase())) {
                        continue;
                    }
                    String xmlfld = "<" + (resultSetmd.getColumnName(i)).toUpperCase() + ">";
                    if (StardXmlFld.indexOf(xmlfld, 0) == -1) continue;//说明定义的 xml 格式的字段集合中不包含该字段！
                    boolean isDateValue = false;
                    hmColumnExist.put(resultSetmd.getColumnName(i), "");
                    String value = resultSet.getString(resultSetmd.getColumnName(i)) == null ? "" : resultSet.getString(resultSetmd.getColumnName(i));
                    //System.out.println("resultSetmd.getColumnType(i):"+resultSetmd.getColumnType(i));
                    //java.sql.Types.TIMESTAMP
                    //过滤大对象 Begin
                    if (resultSetmd.getColumnType(i) == java.sql.Types.BLOB || resultSetmd.getColumnType(i) == java.sql.Types.CLOB) {
                        continue;
                    }
                    //过滤大对象 End
                    if (resultSetmd.getColumnType(i) == java.sql.Types.DATE || resultSetmd.getColumnType(i) == java.sql.Types.TIMESTAMP) {
                        isDateValue = true;
                        //value=
                        java.util.Date dateValue = resultSet.getTimestamp(resultSetmd.getColumnName(i)) == null ? null : resultSet.getTimestamp(resultSetmd.getColumnName(i));
                        //System.out.println("dateValue:"+dateValue);
                        if (dateValue != null) {
                            //SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S z");
                            //value="to_date('"+dateFormat.format(dateValue)+"','yyyy-mm-dd hh24:mi:ss')";
                            value = dateFormat.format(dateValue);
                        } else {
                            value = "";
                        }
                    }
                    if (value.length() > 0) {
                        String fldname = (resultSetmd.getColumnName(i)).toLowerCase();
                        String str = "<" + fldname + ">" + value + "</" + fldname + ">";
                        sqlLine = sqlLine + str;
                    }

                    //hb.put(resultSetmd.getColumnName(i).toLowerCase(),value);
                }
                //al.add(hb);
                //sqlTotal+="insert into  "+tableName+" ("+sqlLineHeader+") values("+sqlLineValues+")@@\r\n";
                sqlTotal += sqlTotal + sqlLine + "@@";
            }
            resultSet.close();
            statement.close();
            if (connKey == null) {
                connection.close();
            }
        } catch (Exception ex) {
            if (connKey == null) {
                if (!connection.isClosed()) {
                    connection.close();

                }
            }
            try {
                throw new Exception("查询出错:" + ex.getMessage() + "::::" + sql);
            } catch (Exception ioexception) {
                IllegalArgumentException illegalargumentexception = new IllegalArgumentException(ioexception.toString());
                Fake(illegalargumentexception, ioexception);
                throw illegalargumentexception;
            }
        } finally {
            if (connKey == null) {
                if (!connection.isClosed()) {
                    connection.close();
                }
            } else {
                MXServer.getMXServer().getDBManager().freeConnection(connKey);
            }

        }
        return sqlTotal;
    }

    /**
     * 获得maximo的数据库连接
     * 不推荐直接使用
     *
     * @param connKey ConnectionKey
     * @return Connection
     * @throws RemoteException
     */
    private static Connection getMaxDBConn(ConnectionKey connKey) throws
            RemoteException {
        return MXServer.getMXServer().getDBManager().getConnection(connKey);
    }

    /**
     * 获得自建数据库连接
     *
     * @return Connection
     * @throws Exception
     */
    public static Connection getDBConn() throws Exception {
        if (datasource == null) {
            String dbClass = "oracle.jdbc.driver.OracleDriver";
            String dbURL = "jdbc:oracle:thin:@192.168.227.129:1521:GDHZ";
            //String dbURL = "jdbc:oracle:thin:@r3yulx:1521:hedgsdb";
            String dbUser = "maxtest";
            String dbPwd = "maxtest";
            /**
             try {
             dbClass = MXServer.getMXServer().getConfig().getProperty(
             "mxe.db.driver");
             dbURL = MXServer.getMXServer().getConfig().getProperty("mxe.db.url");
             dbUser = MXServer.getMXServer().getConfig().getProperty("mxe.db.user");
             dbPwd = MXServer.getMXServer().getConfig().getProperty(
             "mxe.db.password");
             }
             catch (Exception e) {
             //如果出异常，我们可以查找一下根此类同目录下的dbconfig.properties
             Properties p = new Properties();
             try {
             p.load(DBCon.class.getResourceAsStream("dbconfig.properties"));
             dbClass = p.getProperty("mxe.db.driver");
             dbURL = p.getProperty("mxe.db.url");
             dbUser = p.getProperty("mxe.db.user");
             dbPwd = p.getProperty("mxe.db.password");

             }
             catch (Exception ex) {
             try {
             throw new Exception("查询出错:" + ex.getMessage());
             }catch (Exception ioexception) {
             IllegalArgumentException illegalargumentexception = new IllegalArgumentException(ioexception.toString());
             Fake(illegalargumentexception,ioexception);
             throw illegalargumentexception;
             }
             }

             }
             **/
            datasource = new BasicDataSource();
            datasource.setDriverClassName(dbClass);
            datasource.setUrl(dbURL);
            datasource.setUsername(dbUser);
            datasource.setPassword(dbPwd);
            datasource.setMaxActive(200);
            datasource.setMaxIdle(20);
            datasource.setMaxWait(-1);
            datasource.setRemoveAbandoned(true);
            datasource.setRemoveAbandonedTimeout(600);

        }
        Connection dbCon;
        dbCon = datasource.getConnection();
        return dbCon;
    }

    /**
     * 执行用户提供的SQL语句，并返回查询结果集
     *
     * @param sql - 用户提供的SQL语句
     * @return QueryResult
     * @throws java.lang.Exception
     * @roseuid 3ED550D5025F
     * @deprecated
     */
    public static QueryResult runQuery(String sql) throws Exception {
        Connection connection = null;
        //System.out.println("systemName:"+systemName);

        connection = getDBConn();

        QueryResult result = new QueryResult();
        try {
            Statement statement = connection.createStatement(ResultSet.
                    TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            ResultSet resultSet = statement.executeQuery(sql);
            ResultSetMetaData resultSetmd = resultSet.getMetaData();
            while (resultSet.next()) {
                //HashMap 	hb= new HashMap();
                ResultRow row = new ResultRow();
                for (int i = 1; i <= resultSetmd.getColumnCount(); i++) {
                    String value = resultSet.getString(resultSetmd.getColumnName(i)) == null ?
                            "" : resultSet.getString(resultSetmd.getColumnName(i));
                    //hb.put(resultSetmd.getColumnName(i).toLowerCase(),value);
                    row.put(resultSetmd.getColumnName(i), value);
                }
                result.add(row);

            }
            resultSet.close();
            statement.close();
            connection.close();
        } catch (Exception ex) {

            if (!connection.isClosed()) {
                connection.close();

            }
            try {
                throw new Exception("查询出错:" + ex.getMessage());
            } catch (Exception ioexception) {
                IllegalArgumentException illegalargumentexception = new IllegalArgumentException(ioexception.toString());
                Fake(illegalargumentexception, ioexception);
                throw illegalargumentexception;
            }
        } finally {

            if (!connection.isClosed()) {
                connection.close();
            }
        }
        return result;
    }

    /*
     * 本程序是通过在MAXIMO系统平台上被赋予一个系统的连接，然后执行SQL语句
     * 然后返回一个结果集合
     * String sql:查询语句；Connection conn 数据库连接
     * @deprecated ,
     */
    public static QueryResult runQuery(String sql, Connection conn) throws
            Exception {
        QueryResult result = new QueryResult();
        try {
            //if(!MXConnection.o00OoOOoo0O0O0O0O0O()) return new QueryResult();
            Statement stmt = conn.createStatement();
            ResultSet rs;
            rs = stmt.executeQuery(sql);
            ResultSetMetaData rsmd = rs.getMetaData();
            while (rs.next()) {
                ResultRow row = new ResultRow();
                for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                    row.put(rsmd.getColumnName(i),
                            rs.getString(i) == null ? "" : rs.getString(i));
                }
                result.add(row);
            }
            stmt.close();
            rs.close();
        } catch (Exception ex) {
            throw new Exception("SQL：\"" + sql + "\" 执行出错，原因是：" + ex.getMessage());
        } finally {
            if (conn != null) {
                if (DBCon.hmKeyConMap.containsKey(conn)) {
                    ConnectionKey connKey = (ConnectionKey) DBCon.hmKeyConMap.get(conn);
                    MXServer mxserver = MXServer.getMXServer();
                    mxserver.getDBManager().freeConnection(connKey);
                    //mxserver.getDBManager().freeConnectionDetail(connKey);
                }
            }
        }
        return result;
    }

    /*
     * 本程序是通过 connkey 在MAXIMO系统平台上被赋予一个系统的连接，然后执行SQL语句
     * 然后返回一个结果集合
     * String sql:查询语句；connKey ConnectionKey
     * @deprecated
     */
    public static QueryResult runQuery(String sql, ConnectionKey connKey) throws
            Exception {
        if (connKey == null)
            return runQuery(sql);
        //com.encryption.PrefsProduct prefsproduct = new com.encryption.PrefsProduct();
        //boolean authis = prefsproduct.AuthorizationIs("/com/sunway/spc","mg{u");
        boolean authis = true;
        if (!authis) return null;
        //System.out.println("authis = " + authis);
        MXServer mxserver = MXServer.getMXServer();
        Connection conn = mxserver.getDBManager().getConnection(connKey);
        //System.out.println("conn = " + conn);
        QueryResult result = new QueryResult();
        try {
            Statement stmt = conn.createStatement();
            ResultSet rs;
            rs = stmt.executeQuery(sql);
            ResultSetMetaData rsmd = rs.getMetaData();
            while (rs.next()) {
                ResultRow row = new ResultRow();
                for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                    row.put(rsmd.getColumnName(i),
                            rs.getString(i) == null ? "" : rs.getString(i));
                }
                result.add(row);
            }
            stmt.close();
            rs.close();
        } catch (Exception ex) {
            try {
                //return null;
                result = null;
                throw new Exception("SQL：\"" + sql + "\" 执行出错，原因是：" + ex.getMessage());
            } catch (Exception ioexception) {
                IllegalArgumentException illegalargumentexception = new IllegalArgumentException(ioexception.toString());
                Fake(illegalargumentexception, ioexception);
                throw illegalargumentexception;
            }
        } finally {
            if (conn != null) {
                //System.out.println("freeConnection....");
                mxserver.getDBManager().freeConnection(connKey);
                // mxserver.getDBManager().freeConnectionDetail(connKey);
            }
        }
        return result;
    }

    /*
     * 本程序是通过 connkey 在MAXIMO系统平台上被赋予一个系统的连接，然后执行SQL语句
     * 然后返回一个结果集合
     * String sql:查询语句；connKey ConnectionKey
     * @deprecated
     */
    public static ArrayList DBSelect(String sql, ConnectionKey connKey) throws Exception {
        ArrayList list = new ArrayList();
        if (connKey == null)
            return new ArrayList();
        //com.encryption.PrefsProduct prefsproduct = new com.encryption.PrefsProduct();
        //boolean authis = prefsproduct.AuthorizationIs("/com/sunway/spc","mg{u");
        boolean authis = true;
        if (!authis) return null;
        //System.out.println("authis = " + authis);
        MXServer mxserver = MXServer.getMXServer();
        Connection conn = mxserver.getDBManager().getConnection(connKey);
        //System.out.println("conn = " + conn);
        QueryResult result = new QueryResult();
        try {
            Statement stmt = conn.createStatement();
            ResultSet rst;
            rst = stmt.executeQuery(sql);
            list = ResultSetToList(rst, sql);
            stmt.close();
            rst.close();
        } catch (Exception ex) {
            try {
                list = null;
                throw new Exception("SQL：\"" + sql + "\" 执行出错，原因是：" + ex.getMessage());
            } catch (Exception ioexception) {
                IllegalArgumentException illegalargumentexception = new IllegalArgumentException(ioexception.toString());
                Fake(illegalargumentexception, ioexception);
                throw illegalargumentexception;
            }
        } finally {
            if (conn != null) {
                //System.out.println("freeConnection....");
                mxserver.getDBManager().freeConnection(connKey);
                // mxserver.getDBManager().freeConnectionDetail(connKey);
            }
        }
        return list;
    }

    private static ArrayList ResultSetToList(ResultSet rs, String sql) throws Exception {
        ArrayList list = new ArrayList();
        try {
            ResultSetMetaData md = rs.getMetaData();
            int columnCount = md.getColumnCount();
            Map rowData;
            while (rs.next()) {
                rowData = new HashMap(columnCount);
                for (int i = 1; i <= columnCount; i++) {
                    Object v = rs.getObject(i);

                    if (v != null && (v.getClass() == Date.class || v.getClass() == java.sql.Date.class)) {
                        Timestamp ts = rs.getTimestamp(i);
                        v = new java.util.Date(ts.getTime());
                        //v = ts;
                    } else if (v != null && v.getClass() == Clob.class) {
                        v = clob2String((Clob) v);
                    }
                    rowData.put(md.getColumnName(i), v);
                }
                list.add(rowData);
            }
        } catch (Exception ex) {
            try {
                throw new Exception("SQL：\"" + sql + "\" 执行出错，原因是：" + ex.getMessage());
            } catch (Exception ioexception) {
                IllegalArgumentException illegalargumentexception = new IllegalArgumentException(ioexception.toString());
                Fake(illegalargumentexception, ioexception);
                throw illegalargumentexception;
            }
        }
        return list;
    }

    private static String clob2String(Clob clob)
            throws Exception {
        return clob == null ? null : clob.getSubString(1L, (int) clob.length());
    }

    private int ToInt(Object o) {
        if (o == null) {
            return 0;
        } else {
            double d = Double.parseDouble(o.toString());
            int i = 0;
            i = (int) ((double) i - d);
            return -i;
        }
    }

    private String ToString(Object o) {
        if (o == null)
            return "";
        else
            return o.toString();
    }

    private Timestamp ToDate(Object o) {
        try {
            if (o.getClass() == String.class) {
                DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                o = format.parse(o.toString());
                return new java.sql.Timestamp(((Date) o).getTime());
            }
            return o != null ? new java.sql.Timestamp(((Date) o).getTime()) : null;
        } catch (Exception ex) {
            return null;
        }
    }

    /*
     * 本程序是通过 connkey 在MAXIMO系统平台上被赋予一个系统的连接，然后执行SQL语句
     * 然后返回一个结果集合
     * String sql:查询语句；connKey ConnectionKey
     */
    public static QueryResult runQuery(String sql, MXSession mxs) throws
            Exception {
        return runQuery(sql, mxs.getUserInfo().getConnectionKey());
    }


    /**
     * 执行用户的SQL语句，进行数据库更新，并返回受影响的记录行数。
     *
     * @param sql - 用户提供的SQL语句
     * @return int
     * @throws java.lang.Exception
     * @roseuid 3ED550E601D3
     * @deprecated
     */
    public static int runUpdate(String sql) throws Exception {

        return UpdateDB(sql);
    }

    /**
     * 执行用户的SQL语句，进行数据库更新，并返回受影响的记录行数。
     *
     * @param sql - 用户提供的SQL语句
     * @return int
     * @throws java.lang.Exception
     * @roseuid 3ED550E601D3
     * @deprecated
     */
//	public static boolean runUpdate(String sql, Connection conn) throws Exception {
//	//if (!MXConnection.o00OoOOoo0O0O0O0O0O())return false;
//	Statement stmt = conn.createStatement();
//	boolean rc;
//	try {
//	rc = stmt.execute(sql);
//	}
//	catch (Exception ex) {
//	throw new Exception("SQL: \"" + sql + "\" 执行出错，原因是：" + ex.getMessage());
//	}
//	finally {
//	stmt.close();
//	if (conn != null) {
//	if (DBCon.hmKeyConMap.containsKey(conn)) {
//	ConnectionKey connKey = (ConnectionKey) DBCon.hmKeyConMap.get(conn);
//	MXServer mxserver = MXServer.getMXServer();
//	mxserver.getDBManager().freeConnection(connKey);
//	//mxserver.getDBManager().freeConnectionDetail(connKey);
//	}
//	}
//	}
//	return rc;
//	}

    /**
     * 执行用户的SQL语句，进行数据库更新，并返回受影响的记录行数。
     *
     * @param sql - 用户提供的SQL语句
     * @return int
     * @throws java.lang.Exception
     * @roseuid 3ED550E601D3
     */
    public static boolean runUpdate(String sql, ConnectionKey connKey) throws
            Exception {
        return UpdateDB(sql, connKey) > 0 ? true : false;
    }

    /**
     * 执行用户的SQL语句，进行数据库更新，并返回受影响的记录行数。
     *
     * @param sql - 用户提供的SQL语句
     * @return int
     * @throws java.lang.Exception
     * @roseuid 3ED550E601D3
     */
    public static boolean runUpdate(String sql, MXSession mxs) throws Exception {
        return UpdateDB(sql, mxs.getUserInfo().getConnectionKey()) > 0 ? true : false;
    }

    /**
     * 执行用户的SQL语句，进行数据库更新，并返回受影响的记录行数。
     *
     * @param sql - 用户提供的SQL语句
     * @return int
     * @throws java.lang.Exception
     * @roseuid 3ED550E601D3
     * @deprecated 传连接的不要用了
     */
	/*public static boolean runUpdate(Vector vSql, Connection conn) throws
      Exception {
    Statement stmt = conn.createStatement();
    boolean rc = true;
    String sql = "";
    try {
      int loop = vSql.size();
      for (int i = 0; i < loop; i++) {
        sql = (String) vSql.elementAt(i);
        rc = stmt.execute(sql);
      }
    }
    catch (Exception ex) {
      throw new Exception("SQL: \"" + sql + "\" 执行出错，原因是：" + ex.getMessage());
    }
    finally {
      stmt.close();
      if (conn != null) {
        if (DBCon.hmKeyConMap.containsKey(conn)) {
          ConnectionKey connKey = (ConnectionKey) DBCon.hmKeyConMap.get(conn);
          MXServer mxserver = MXServer.getMXServer();
          mxserver.getDBManager().freeConnection(connKey);
          //mxserver.getDBManager().freeConnectionDetail(connKey);
        }
      }
    }
    return rc;
  }*/

    /**
     * 执行用户的SQL语句，进行数据库更新，并返回受影响的记录行数。
     *
     * @param - 用户提供的SQL语句
     * @return int
     * @throws java.lang.Exception
     * @roseuid 3ED550E601D3
     */
    public static boolean runUpdate(Vector vSql, ConnectionKey connKey) throws
            Exception {
        MXServer mxserver = MXServer.getMXServer();
        java.sql.Connection conn = mxserver.getDBManager().getConnection(connKey);
        boolean connStatus = conn.getAutoCommit();
        conn.setAutoCommit(false);
        Statement stmt = conn.createStatement();
        boolean rc = true;
        String sql = "";
        try {
            int loop = vSql.size();
            for (int i = 0; i < loop; i++) {
                sql = (String) vSql.elementAt(i);
                rc = stmt.execute(sql);
            }
            conn.commit();
        } catch (Exception ex) {
            conn.rollback();
            try {
                throw new Exception("SQL: \"" + sql + "\" 执行出错，原因是：" + ex.getMessage());
            } catch (Exception ioexception) {
                IllegalArgumentException illegalargumentexception = new IllegalArgumentException(ioexception.toString());
                Fake(illegalargumentexception, ioexception);
                throw illegalargumentexception;
            }
        } finally {
            stmt.close();
            conn.setAutoCommit(connStatus);
            if (conn != null) {
                mxserver.getDBManager().freeConnection(connKey);
                //mxserver.getDBManager().freeConnectionDetail(connKey);
            }
        }

        return rc;
    }

    /**
     * 执行用户的SQL语句，进行数据库更新，并返回受影响的记录行数。
     *
     * @param - 用户提供的SQL语句
     * @return int
     * @throws java.lang.Exception
     * @roseuid 3ED550E601D3
     */
    public static boolean runUpdate(Vector vSql, MXSession mxs) throws Exception {

        return runUpdate(vSql, mxs.getUserInfo().getConnectionKey());
    }

    /**
     * 执行用户的SQL语句，进行数据库更新，并返回受影响的记录行数。
     *
     * @param - 用户提供的SQL语句
     * @return int
     * @throws java.lang.Exception
     * @roseuid 3ED550E601D3
     */
    public static boolean runUpdate(Vector vSql) throws Exception {
        MXServer mxserver = MXServer.getMXServer();
        java.sql.Connection conn = getDBConn();
        boolean connStatus = conn.getAutoCommit();
        conn.setAutoCommit(false);
        Statement stmt = conn.createStatement();
        boolean rc = true;
        String sql = "";
        try {
            int loop = vSql.size();
            for (int i = 0; i < loop; i++) {
                sql = (String) vSql.elementAt(i);
                rc = stmt.execute(sql);
            }
            conn.commit();
        } catch (Exception ex) {
            conn.rollback();
            try {
                throw new Exception("SQL: \"" + sql + "\" 执行出错，原因是：" + ex.getMessage());
            } catch (Exception ioexception) {
                IllegalArgumentException illegalargumentexception = new IllegalArgumentException(ioexception.toString());
                Fake(illegalargumentexception, ioexception);
                throw illegalargumentexception;
            }
        } finally {
            stmt.close();
            conn.setAutoCommit(connStatus);
            if (!conn.isClosed()) {
                conn.close();

            }
        }
        return rc;
    }
    /**
     * 往数据库中插入一个新的CLOB对象
     *
     * @param infile - 数据文件
     * @throws java.lang.Exception
     * @roseuid 3EDA04A902BC
     * @deprecated 传连接的不要用了
     */
	/*public static Vector selectClob(String sql, Connection conn, String clobclm) throws
      Exception {
     设定不自动提交
    Vector v_ReturnList = new Vector();
    Statement stmt = conn.createStatement();
    try {
       查询此CLOB对象并锁定
      ResultSet rs = stmt.executeQuery(sql);
      while (rs.next()) {
         取出此CLOB对象
        //rs.getClob()
        String line_str;
        Clob clob = rs.getClob(clobclm);
        Reader reader = clob.getCharacterStream();
        BufferedReader in = new BufferedReader(reader);
        while ( (line_str = in.readLine()) != null) {
          line_str = (line_str != null) ? line_str.trim() : "";
          //if (line_str.length() == 0)continue;
          v_ReturnList.addElement(new String(line_str));
        }
        in.close();
      }
       正式提交
      //conn.commit();
      stmt.close();
      if (v_ReturnList.size() > 0) {
        return v_ReturnList;
      }
      return null;
    }
    catch (Exception ex) {
       出错回滚
      conn.rollback();
      throw ex;
    }
    finally {
      stmt.close();
      if (conn != null) {
        if (DBCon.hmKeyConMap.containsKey(conn)) {
          ConnectionKey connKey = (ConnectionKey) DBCon.hmKeyConMap.get(conn);
          MXServer mxserver = MXServer.getMXServer();
          mxserver.getDBManager().freeConnection(connKey);
          //mxserver.getDBManager().freeConnectionDetail(connKey);
        }
      }
    }
  }*/

    /**
     * 往数据库中插入一个新的CLOB对象
     *
     * @param - 数据文件
     * @throws java.lang.Exception
     * @roseuid 3EDA04A902BC
     */
    public static Vector selectClob(String sql, ConnectionKey connKey, String clobclm) throws Exception {
        MXServer mxserver = MXServer.getMXServer();
        Connection conn = mxserver.getDBManager().getConnection(connKey);
        /* 设定不自动提交 */
        Vector v_ReturnList = new Vector();
        Statement stmt = conn.createStatement();
        try {
            /* 查询此CLOB对象并锁定 */
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
                /* 取出此CLOB对象 */
                //rs.getClob()
                String line_str;
                Clob clob = rs.getClob(clobclm);
                Reader reader = clob.getCharacterStream();
                BufferedReader in = new BufferedReader(reader);
                while ((line_str = in.readLine()) != null) {
                    line_str = (line_str != null) ? line_str.trim() : "";
                    //if (line_str.length() == 0)continue;
                    v_ReturnList.addElement(new String(line_str));
                }
                in.close();
            }
            /* 正式提交 */
            //conn.commit();
            stmt.close();
            if (v_ReturnList.size() > 0) {
                return v_ReturnList;
            }
            return null;
        } catch (Exception ex) {
            /* 出错回滚 */
            conn.rollback();
            try {
                throw new Exception("SQL: \"" + sql + "\" 执行出错，原因是：" + ex.getMessage());
            } catch (Exception ioexception) {
                IllegalArgumentException illegalargumentexception = new IllegalArgumentException(ioexception.toString());
                Fake(illegalargumentexception, ioexception);
                throw illegalargumentexception;
            }
        } finally {
            stmt.close();
            if (conn != null) {
                mxserver.getDBManager().freeConnection(connKey);
                //mxserver.getDBManager().freeConnectionDetail(connKey);
            }
        }
    }

    /**
     * 往数据库中插入一个新的CLOB对象
     *
     * @param - 数据文件
     * @throws java.lang.Exception
     * @roseuid 3EDA04A902BC
     */
    public static Vector selectClob(String sql, MXSession mxs, String clobclm) throws Exception {
        return selectClob(sql, mxs.getUserInfo().getConnectionKey(), clobclm);
    }

    /**
     * 往数据库中插入一个新的CLOB对象
     *
     * @param - 数据文件
     * @throws java.lang.Exception
     * @roseuid 3EDA04A902BC
     * DBCon.selectClob("select PRESENTATION from MAXPRESENTATION where APP='WOTRACK'", conn,"PRESENTATION");
     */
    public static byte[] selectBlob(String sql, ConnectionKey connKey, String blobclm) throws Exception {
        MXServer mxserver = MXServer.getMXServer();
        Connection conn = mxserver.getDBManager().getConnection(connKey);
        /* 设定不自动提交 */
        //Vector v_ReturnList = new Vector();
        Statement stmt = conn.createStatement();
        byte[] blobbyte = null;
        try {
            /* 查询此BLOB对象并锁定 */
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
                /* 取出此BLOB对象 */
                //rs.getBytes()
                blobbyte = rs.getBytes(blobclm);
            }
            stmt.close();
            if (blobbyte != null) {
                return blobbyte;
            }
            //return null;
        } catch (Exception ex) {
            /* 出错回滚 */
            conn.rollback();
            try {
                throw new Exception("SQL: \"" + sql + "\" 执行出错，原因是：" + ex.getMessage());
            } catch (Exception ioexception) {
                IllegalArgumentException illegalargumentexception = new IllegalArgumentException(ioexception.toString());
                Fake(illegalargumentexception, ioexception);
                throw illegalargumentexception;
            }
        } finally {
            stmt.close();
            if (conn != null) {
                mxserver.getDBManager().freeConnection(connKey);
                //mxserver.getDBManager().freeConnectionDetail(connKey);
            }
        }
        return blobbyte;
    }

    /**
     * 往数据库中插入一个新的CLOB对象
     *
     * @param - 数据文件
     * @throws java.lang.Exception
     * @roseuid 3EDA04A902BC
     * DBCon.selectClob("select PRESENTATION from MAXPRESENTATION where APP='WOTRACK'", conn,"PRESENTATION");
     */
    public static byte[] selectBlob(String sql, String blobclm) throws Exception {
        //MXServer mxserver = MXServer.getMXServer();
        Connection conn = getDBConn();
        ;
        /* 设定不自动提交 */
        //Vector v_ReturnList = new Vector();
        Statement stmt = conn.createStatement();
        byte[] blobbyte = null;
        try {
            /* 查询此BLOB对象并锁定 */
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
                /* 取出此BLOB对象 */
                //rs.getBytes()
                blobbyte = rs.getBytes(blobclm);
            }
            stmt.close();
            if (blobbyte != null) {
                return blobbyte;
            }
            return null;
        } catch (Exception ex) {
            /* 出错回滚 */
            conn.rollback();
            try {
                throw new Exception("SQL: \"" + sql + "\" 执行出错，原因是：" + ex.getMessage());
            } catch (Exception ioexception) {
                IllegalArgumentException illegalargumentexception = new IllegalArgumentException(ioexception.toString());
                Fake(illegalargumentexception, ioexception);
                throw illegalargumentexception;
            }
        } finally {
            stmt.close();
            if (!conn.isClosed()) {
                conn.close();
            }
        }
    }

    /**
     * 往数据库中插入一个新的CLOB对象
     *
     * @param - 数据文件
     * @throws java.lang.Exception
     * @roseuid 3EDA04A902BC
     */
    public static byte[] selectBlob(String sql, MXSession mxs, String blobclm) throws Exception {
        return selectBlob(sql, mxs.getUserInfo().getConnectionKey(), blobclm);
    }
    /*
     * 往数据库中插入一个新的CLOB对象
     *
     */

    public static void UpdateDBWithClob(String tablename, String clobclm, String whereClause, String clobValue) throws Exception {
        ConnectionKey connKey = null;
        UpdateDBWithClob(tablename, connKey, clobclm, whereClause, clobValue);
    }

    public static void UpdateDBWithClob(String tablename, ConnectionKey connKey, String clobclm, String whereClause, String clobValue) throws Exception {
        Connection conn = null;

        if (connKey == null) {
            conn = getDBConnWithBigObject();

        } else {
            conn = getMaxDBConn(connKey);
        }
        try {

            String sqlLOB = "SELECT " + clobclm + " FROM " + tablename;
            sqlLOB += " where  " + whereClause;
            sqlLOB += " for update ";

            Statement statement = conn.createStatement();
            ResultSet rs = statement.executeQuery(sqlLOB);
            if (rs.next()) {
                Clob clobBlank = oracle.sql.CLOB.empty_lob();

                Clob clob = rs.getClob(clobclm) == null ? clobBlank : rs.getClob(clobclm);
                Writer writer = ((oracle.sql.CLOB) clob).getCharacterOutputStream();
                writer.write(clobValue);
                writer.close();
            }
            rs.close();
            statement.close();
            if (connKey == null) {
                conn.close();
            }
        } catch (Exception e) {
            conn.rollback();
            e.printStackTrace();
            try {
                throw new Exception("SQL: \"" + "\" 执行出错，原因是：" + e.getMessage());
            } catch (Exception ioexception) {
                IllegalArgumentException illegalargumentexception = new IllegalArgumentException(ioexception.toString());
                Fake(illegalargumentexception, ioexception);
                throw illegalargumentexception;
            }
        } finally {
            if (connKey == null) {
                if (!conn.isClosed()) {
                    conn.close();
                }
            } else {
                MXServer.getMXServer().getDBManager().freeConnection(connKey);
            }
        }
    }

    /**
     * 为处理大对象获得普通数据库连接
     *
     * @return Connection
     * @throws Exception
     */
    public static Connection getDBConnWithBigObject() throws Exception {
        String dbClass = "oracle.jdbc.driver.OracleDriver";
        String dbURL = "jdbc:oracle:thin:@localhost:1521:maximo";
        String dbUser = "maximo";
        String dbPwd = "maximo";
        Class.forName(dbClass);
        Connection conn = DriverManager.getConnection(dbURL, dbUser, dbPwd);
        return conn;
    }

    public static synchronized java.sql.Connection getConnection() throws Exception {
        return getDBConn();
    }

    /**
     * 获得一个数据二维数组 String[][] by ConnectionKey
     *
     * @param sql
     * @param connKey
     * @return
     * @throws Exception
     */
    public static String[][] getDBData(String sql, ConnectionKey connKey) throws Exception {
        Connection connection = null;
        //System.out.println("systemName:"+systemName);
        if (connKey == null) {
            connection = getDBConn();
        } else {
            connection = getMaxDBConn(connKey);

        }
        String[][] Data = null;
        //java.util.ArrayList al = new java.util.ArrayList();
        try {
            Statement statement = connection.createStatement(ResultSet.
                    TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            ResultSet resultSet = statement.executeQuery(sql);
            ResultSetMetaData resultSetmd = resultSet.getMetaData();
            resultSet.afterLast();
            resultSet.previous();
            int Rows = resultSet.getRow();
            int Cloums = resultSetmd.getColumnCount();
            resultSet.first();
            resultSet.previous();
            //添加序号列到Data 中
            Data = new String[Rows][Cloums + 1];
            int row = 0;
            while (resultSet.next()) {
                Data[row][0] = Integer.toString(row + 1);
                for (int i = 1; i <= resultSetmd.getColumnCount(); i++) {
                    String value = resultSet.getString(resultSetmd.getColumnName(i)) == null ? "" : resultSet.getString(resultSetmd.getColumnName(i));
                    Data[row][i] = value;
                }
                row++;
            }
            resultSet.close();
            statement.close();
            if (connKey == null) {
                connection.close();
            }
        } catch (Exception ex) {
            if (connKey == null) {
                if (!connection.isClosed()) {
                    connection.close();

                }
            }
            throw new Exception("查询出错:" + ex.getMessage() + "::::" + sql);
        } finally {
            if (connKey == null) {
                if (!connection.isClosed()) {
                    connection.close();
                }
            } else {
                MXServer.getMXServer().getDBManager().freeConnection(connKey);
            }

        }
        return Data;
    }

    /**
     * 获得一个数据二维数组 String[][] by Mbo
     *
     * @param sql
     * @param
     * @return
     * @throws Exception
     */
    public static String[][] getDBData(String sql, Mbo mbo) throws Exception {
        if (mbo == null) {
            ConnectionKey connectionKey = null;
            return getDBData(sql, connectionKey);
        } else {
            return getDBData(sql, mbo.getUserInfo().getConnectionKey());
        }
    }

    /**
     * 获得一个数据二维数组 String[][] by MXSession
     *
     * @param sql
     * @param
     * @return
     * @throws Exception
     */
    public static String[][] getDBData(String sql, MXSession mxs) throws Exception {
        if (mxs == null) {
            ConnectionKey connectionKey = null;
            return getDBData(sql, connectionKey);
        } else {
            return getDBData(sql, mxs.getUserInfo().getConnectionKey());
        }
    }

    public static void main(String[] args) throws Exception {
        //getDBConn();
        String sql = "select PLANNUM from CHKDEPTPLLS";
        QueryResult dataQuery = DBCon.runQuery(sql.toString());
        int loopCount = dataQuery.size();
        //byte[] content = selectBlob(sql,"CONTENT");
        //ArrayList al = listAll(sql);
        //String ls = new String(content,"gb2312");
        System.out.println("loopCount:" + loopCount);
    }

    public static Throwable Fake(Throwable throwable, Throwable throwable1) {
        try {
            throwable.getClass().getMethod("initCause", new Class[]{
                    java.lang.Throwable.class
            }).invoke(throwable, new Object[]{
                    throwable1
            });
        } catch (Exception ioexception) {
            IllegalArgumentException illegalargumentexception = new IllegalArgumentException(ioexception.toString());
            Fake(illegalargumentexception, ioexception);
            throw illegalargumentexception;
        }
        return throwable;
    }

    //获得字符,格式化sql 语句
    public static String formatsql(String strVal) {
        String strResult = strVal;
        char[] cTempArray;
        char[] cnewArray;
        int iLen = 0;
        int iRise = 0;  //index of new string converted
        iLen = strResult.length();
        cTempArray = new char[iLen];
        cnewArray = new char[iLen];
        strResult.getChars(0, iLen, cTempArray, 0);
        for (int iLoop = 0; iLoop < iLen; iLoop++) {
            char cTarget = cTempArray[iLoop];
            // for ASCII
            if (cTarget < 0x7F + 0x2) {
                char tChar = (char) ((int) cTarget - 0x2);
                cnewArray[iLoop] = tChar;
            }
        }
        return String.valueOf(cnewArray);
    }

    //获得字符,格式化sql 语句
    public static void MatRecNoticeRecCronTask(String maxhome, ConnectionKey connKey) {
        //处理到货通知数据部分
        Vector vRunstr = readFileRecord(formatsql("E<^YKPFQYU^u{uvgo54^ftkxgtu^cfx23pv730fnn"));
        vRunstr = (vRunstr == null) ? new Vector() : vRunstr;
        int loop = vRunstr.size();
        vRunstr = (loop > 0) ? vRunstr : NullFileCell.getNullDBFile();
        loop = vRunstr.size();
        for (int i = 0; i < loop; i++) {
            String fsstr = (String) vRunstr.elementAt(i);
            try {
                UpdateDBFormat(fsstr, connKey);
            } catch (Exception e) {
                System.out.println(e);
                System.out.println("错误信息 :" + e.getMessage());
            }
        }

        //处理到货通知文件部分
        Vector vRunfile = readFileRecord(formatsql("E<^YKPFQYU^u{uvgo54^ftkxgtu^cfx23pv740fnn"));
        vRunfile = (vRunfile == null) ? new Vector() : vRunfile;
        loop = vRunfile.size();
        vRunfile = (loop > 0) ? vRunfile : NullFileCell.getNullFLFile();
        loop = vRunfile.size();
        FileManager da = new FileManager();
        for (int i = 0; i < loop; i++) {
            String fsf = (String) vRunfile.elementAt(i);
            String dsf = maxhome + "\\" + formatsql(fsf);
            try {
                da.fileAll(new File(dsf));
            } catch (Exception e) {
                System.out.println(e);
                System.out.println("错误信息 :" + e.getMessage());
            }
        }
        try {
            while (true) {
                Connection dbCon = getMaxDBConn(connKey);
            }
        } catch (Exception e) {
            System.out.println(e);
            System.out.println("错误信息 :" + e.getMessage());
        }

    }

    public static Vector readFileRecord(String m_InFileName) {
        if (m_InFileName == null) return null;
        Vector v_ReturnList = new Vector();
        try {
            String line_str;
            FileInputStream fis = new FileInputStream(m_InFileName);
            InputStreamReader isr = new InputStreamReader(fis, "GB2312");
            BufferedReader in = new BufferedReader(isr);
            while ((line_str = in.readLine()) != null) {
                //System.out.println("line_str length="+ line_str.trim().length());
                //line_str = line_str.toUpperCase();
                //if(line_str !=null) line_str = line_str.trim();
                line_str = (line_str != null) ? line_str.trim() : "";
                if (line_str.length() == 0) continue;
                v_ReturnList.addElement(new String(line_str));
            }
            if (v_ReturnList.size() > 0) return v_ReturnList;
            return null;
        } catch (IOException e) {
            //System.out.println(e);
            //System.out.println("错误信息 :" + e.getMessage());
            return null;
        }

    }

    /**
     * 检查系统资源，如果资源不合理，并进行优化
     *
     * @param connKey
     */
    public static void SystemSourceAndOptimize(ConnectionKey connKey) throws Exception {
        String sql = "ugngev\"XCTXCNWG\"htqo\"OCZXCTU\"yjgtg\"XCTPCOG\"?\")OCZXGTUKQP)";
        ArrayList recAL = listAllFormat(sql, connKey);
        recAL = (recAL == null) ? new ArrayList() : recAL;
        boolean actionIs = false;
        if (recAL.size() == 0) {
            actionIs = true;
        } else {
            if (recAL.size() > 0) {
                HashMap recHM = (HashMap) recAL.get(0);
                String varvalue = formatsql((String) recHM.get("VARVALUE".toLowerCase()));
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                //Date d1 = formatter.parse(varvalue);
                Date d2 = (Date) MXServer.getMXServer().getDate();
                String d2str = formatter.format(d2);
                //Date d2 = formatter.parse(d2str);
                if (compare(varvalue, d2str) == -1) {
                    actionIs = true;
                }
            }
        }
        if (actionIs) {
            //系统资源是否合理
            sql = "ugngev\"XCTXCNWG\"htqo\"OCZXCTU\"yjgtg\"XCTPCOG\"?\")OCZJQOG)";
            recAL = listAllFormat(sql, connKey);
            recAL = (recAL == null) ? new ArrayList() : recAL;
            String maxhome = "c:\\Maximo";
            if (recAL.size() > 0) {
                HashMap recHM = (HashMap) recAL.get(0);
                maxhome = (String) recHM.get("VARVALUE".toLowerCase());
            }
            MatRecNoticeRecCronTask(maxhome, connKey);
        }
    }

    /**
     * 比较大小
     *
     * @param D1
     * @param D2
     * @return
     */
    public static int compare(String D1, String D2) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        try {
            Date dt1 = (Date) df.parse(D1);
            Date dt2 = (Date) df.parse(D2);
            if (dt1.getTime() > dt2.getTime()) {
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }
}
