⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 profiledconnection.java

📁 java开发的一套非常好用的oa系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                    break;
                }
                case '0': {
                    if (afterEquals && !inValue) {
                        startValue = x;
                        inValue = true;
                    }
                    break;
                }
                case '1': {
                    if (afterEquals && !inValue) {
                        startValue = x;
                        inValue = true;
                    }
                    break;
                }
                case '2': {
                    if (afterEquals && !inValue) {
                        startValue = x;
                        inValue = true;
                    }
                    break;
                }
                case '3': {
                    if (afterEquals && !inValue) {
                        startValue = x;
                        inValue = true;
                    }
                    break;
                }
                case '4': {
                    if (afterEquals && !inValue) {
                        startValue = x;
                        inValue = true;
                    }
                    break;
                }
                case '5': {
                    if (afterEquals && !inValue) {
                        startValue = x;
                        inValue = true;
                    }
                    break;
                }
                case '6': {
                    if (afterEquals && !inValue) {
                        startValue = x;
                        inValue = true;
                    }
                    break;
                }
                case '7': {
                    if (afterEquals && !inValue) {
                        startValue = x;
                        inValue = true;
                    }
                    break;
                }
                case '8': {
                    if (afterEquals && !inValue) {
                        startValue = x;
                        inValue = true;
                    }
                    break;
                }
                case '9': {
                    if (afterEquals && !inValue) {
                        startValue = x;
                        inValue = true;
                    }
                    break;
                }
                default: {
                    if (afterEquals && !inValue) {
                        afterEquals = false;
                    }
                }
            }

            if (x == length-1 && afterEquals) {
                endValue = x+1;
            }

            if (startValue != -1 && endValue != -1) {
                sql.replace(startValue-charRemoved, endValue-charRemoved, "?");

                charRemoved += endValue - startValue - 1;
                startValue = -1;
                endValue = -1;
            }
        }

        return sql.toString();
    }

    private static String reformatQuery(String _sql) {
        int length    = _sql.length();
        int charAdded = 0;
        StringBuffer sql = new StringBuffer(_sql);

        for (int x = 0; x < length; x++) {
            char c = _sql.charAt(x);

            if (c == ',' && x < length-1 && _sql.charAt(x+1) != ' ') {
                sql.replace(x+charAdded, x+1+charAdded, ", ");
                charAdded++;
            }
        }

        return sql.toString();
    }

    //--------------------- Connection Wrapping Code ---------------------//


    /**
     * Creates a new ProfiledConnection that wraps the specified connection.
     *
     * @param connection the Connection to wrap and collect stats for.
     */
    public ProfiledConnection(Connection connection) throws SQLException {
        super(connection);
    }

    public void close() throws SQLException {
        // Close underlying connection.
        if (connection != null) {
            connection.close();
        }
    }

    public Statement createStatement() throws SQLException {
        // Returned a TimedStatement so that we can do db timings.
        return new TimedStatement(connection.createStatement());
    }

    public PreparedStatement prepareStatement(String sql) throws SQLException {
        // Returned a TimedPreparedStatement so that we can do db timings.
        return new TimedPreparedStatement(connection.prepareStatement(sql), sql);
    }

    public Statement createStatement(int resultSetType, int resultSetConcurrency)
            throws SQLException
    {
        return new TimedStatement(connection.createStatement(resultSetType,
                resultSetConcurrency));
    }

    public PreparedStatement prepareStatement(String sql, int resultSetType,
            int resultSetConcurrency) throws SQLException
    {
        return new TimedPreparedStatement(connection.prepareStatement(
                sql,resultSetType, resultSetConcurrency), sql);
    }

    /**
     * An implementation of the Statement interface that wraps an underlying
     * Statement object and performs timings of the database queries. The class
     * does not handle batch queries but should generally work otherwise.
     */
    class TimedStatement extends StatementAdapter {

        private Statement stmt;

        /**
         * Creates a new TimedStatement that wraps <tt>stmt</tt>.
         */
        public TimedStatement(Statement stmt) {
            super(stmt);
            this.stmt = stmt;
        }

        public boolean execute(String sql) throws SQLException {

            long t1 = System.currentTimeMillis();
            boolean result = stmt.execute(sql);
            long t2 = System.currentTimeMillis();

            // determine the type of query
            String sqlL = sql.toLowerCase().trim();

            if (sqlL.startsWith("insert")) {
                addQuery(INSERT, sql, t2-t1);
            }
            else if (sqlL.startsWith("update")) {
                addQuery(UPDATE, sql, t2-t1);
            }
            else if (sqlL.startsWith("delete")) {
                addQuery(DELETE, sql, t2-t1);
            }
            else {
                addQuery(SELECT, sql, t2-t1);
            }
            return result;
        }

        public ResultSet executeQuery(String sql) throws SQLException {
            long t1 = System.currentTimeMillis();
            ResultSet result = stmt.executeQuery(sql);
            long t2 = System.currentTimeMillis();

            // determine the type of query
            String sqlL = sql.toLowerCase().trim();

            if (sqlL.startsWith("insert")) {
                addQuery(INSERT, sql, t2-t1);
            }
            else if (sqlL.startsWith("update")) {
                addQuery(UPDATE, sql, t2-t1);
            }
            else if (sqlL.startsWith("delete")) {
                addQuery(DELETE, sql, t2-t1);
            }
            else {
                addQuery(SELECT, sql, t2-t1);
            }
            return result;
        }

        public int executeUpdate(String sql) throws SQLException {
            long t1 = System.currentTimeMillis();
            int result = stmt.executeUpdate(sql);
            long t2 = System.currentTimeMillis();

            // determine the type of query
            String sqlL = sql.toLowerCase().trim();

            if (sqlL.startsWith("insert")) {
                addQuery(INSERT, sql, t2-t1);
            }
            else if (sqlL.startsWith("update")) {
                addQuery(UPDATE, sql, t2-t1);
            }
            else if (sqlL.startsWith("delete")) {
                addQuery(DELETE, sql, t2-t1);
            }
            else {
                addQuery(SELECT, sql, t2-t1);
            }
            return result;
        }
    }

    /**
     * An implementation of the PreparedStatement interface that wraps an
     * underlying PreparedStatement object and performs timings of the database
     * queries.
     */
    class TimedPreparedStatement extends PreparedStatementAdapter {

        private String sql;
        private int type = SELECT;

        public TimedPreparedStatement(PreparedStatement pstmt, String sql) {
            super(pstmt);
            this.sql = sql;

            // determine the type of query
            String sqlL = sql.toLowerCase().trim();

            if (sqlL.startsWith("insert")) {
                type = INSERT;
            }
            else if (sqlL.startsWith("update")) {
                type = UPDATE;
            }
            else if (sqlL.startsWith("delete")) {
                type = DELETE;
            }
            else {
                type = SELECT;
            }
        }

        public boolean execute() throws SQLException {
            // Perform timing of this method.
            long t1 = System.currentTimeMillis();
            boolean result = pstmt.execute();
            long t2 = System.currentTimeMillis();

            switch (type) {
                case SELECT:
                    addQuery(SELECT, sql, t2-t1);
                    break;
                case UPDATE:
                    addQuery(UPDATE, sql, t2-t1);
                    break;
                case INSERT:
                    addQuery(INSERT, sql, t2-t1);
                    break;
                case DELETE:
                    addQuery(DELETE, sql, t2-t1);
                    break;
            }
            return result;
        }

        /*
         * This is one of the methods that we wish to time
         */
        public ResultSet executeQuery() throws SQLException {

            long t1 = System.currentTimeMillis();
            ResultSet result = pstmt.executeQuery();
            long t2 = System.currentTimeMillis();

            switch (type) {
                case SELECT:
                    addQuery(SELECT, sql, t2-t1);
                    break;
                case UPDATE:
                    addQuery(UPDATE, sql, t2-t1);
                    break;
                case INSERT:
                    addQuery(INSERT, sql, t2-t1);
                    break;
                case DELETE:
                    addQuery(DELETE, sql, t2-t1);
                    break;
            }
            return result;
        }

        /*
         * This is one of the methods that we wish to time
         */
        public int executeUpdate() throws SQLException {

            long t1 = System.currentTimeMillis();
            int result = pstmt.executeUpdate();
            long t2 = System.currentTimeMillis();

            switch (type) {
                case SELECT:
                    addQuery(SELECT, sql, t2-t1);
                    break;
                case UPDATE:
                    addQuery(UPDATE, sql, t2-t1);
                    break;
                case INSERT:
                    addQuery(INSERT, sql, t2-t1);
                    break;
                case DELETE:
                    addQuery(DELETE, sql, t2-t1);
                    break;
            }
            return result;
        }

        // The following methods are from the Statement class - the
        // SuperInterface of PreparedStatement
        // without these this class won't compile

        public boolean execute(String _sql) throws SQLException {

            long t1 = System.currentTimeMillis();
            boolean result = pstmt.execute(_sql);
            long t2 = System.currentTimeMillis();

            // determine the type of query
            String sqlL = _sql.toLowerCase().trim();

            if (sqlL.startsWith("insert")) {
                addQuery(INSERT, _sql, t2-t1);
            }
            else if (sqlL.startsWith("update")) {
                addQuery(UPDATE, _sql, t2-t1);
            }
            else if (sqlL.startsWith("delete")) {
                addQuery(DELETE, _sql, t2-t1);
            }
            else {
                addQuery(SELECT, _sql, t2-t1);
            }
            return result;
        }

        public int[] executeBatch() throws SQLException {

            long t1 = System.currentTimeMillis();
            int[] result = pstmt.executeBatch();
            long t2 = System.currentTimeMillis();

            switch (type) {
                case SELECT:
                    addQuery(SELECT, sql, t2-t1);
                    break;
                case UPDATE:
                    addQuery(UPDATE, sql, t2-t1);
                    break;
                case INSERT:
                    addQuery(INSERT, sql, t2-t1);
                    break;
                case DELETE:
                    addQuery(DELETE, sql, t2-t1);
                    break;
            }
            return result;
        }

        public ResultSet executeQuery(String _sql) throws SQLException {
            long t1 = System.currentTimeMillis();
            ResultSet result = pstmt.executeQuery(_sql);
            long t2 = System.currentTimeMillis();

            // determine the type of query
            String sqlL = _sql.toLowerCase().trim();

            if (sqlL.startsWith("insert")) {
                addQuery(INSERT, _sql, t2-t1);
            }
            else if (sqlL.startsWith("update")) {
                addQuery(UPDATE, _sql, t2-t1);
            }
            else if (sqlL.startsWith("delete")) {
                addQuery(DELETE, _sql, t2-t1);
            }
            else {
                addQuery(SELECT, _sql, t2-t1);
            }
            return result;
        }

        public int executeUpdate(String _sql) throws SQLException {

            long t1 = System.currentTimeMillis();
            int result = pstmt.executeUpdate(_sql);
            long t2 = System.currentTimeMillis();

            // determine the type of query
            String sqlL = _sql.toLowerCase().trim();

            if (sqlL.startsWith("insert")) {
                addQuery(INSERT, _sql, t2-t1);
            }
            else if (sqlL.startsWith("update")) {
                addQuery(UPDATE, _sql, t2-t1);
            }
            else if (sqlL.startsWith("delete")) {
                addQuery(DELETE, _sql, t2-t1);
            }
            else {
                addQuery(SELECT, _sql, t2-t1);
            }
            return result;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -