connectionmanager.java

来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 493 行 · 第 1/2 页

JAVA
493 行
字号
        JiveGlobals.setJiveProperty("connectionProvider.className",                provider.getClass().getName());    }    /**     * Retrives a large text column from a result set, automatically performing     * streaming if the JDBC driver requires it. This is necessary because     * different JDBC drivers have different capabilities and methods for     * retrieving large text values.     *     * @param rs the ResultSet to retrieve the text field from.     * @param columnIndex the column in the ResultSet of the text field.     * @return the String value of the text field.     */    public static String getLargeTextField(ResultSet rs, int columnIndex)            throws SQLException    {        if (streamLargeText) {            Reader bodyReader = null;            String value = null;            try {                bodyReader = rs.getCharacterStream(columnIndex);                if (bodyReader == null) {                    return null;                }                char [] buf = new char[256];                int len;                StringWriter out = new StringWriter(256);                while ((len = bodyReader.read(buf)) >= 0) {                    out.write(buf, 0, len);                }                value = out.toString();                out.close();            }            catch (Exception e) {                e.printStackTrace();                throw new SQLException("Failed to load text field");            }            finally {                try { bodyReader.close(); }                catch(Exception e) { }            }            return value;        }        else {            return rs.getString(columnIndex);        }    }    /**     * Sets a large text column in a result set, automatically performing     * streaming if the JDBC driver requires it. This is necessary because     * different JDBC drivers have different capabilities and methods for     * setting large text values.     *     * @param pstmt the PreparedStatement to set the text field in.     * @param parameterIndex the index corresponding to the text field.     * @param value the String to set.     */    public static void setLargeTextField(PreparedStatement pstmt,            int parameterIndex, String value) throws SQLException    {        if (streamLargeText) {            Reader bodyReader = null;            try {                bodyReader = new StringReader(value);                pstmt.setCharacterStream(parameterIndex, bodyReader, value.length());            }            catch (Exception e) {                e.printStackTrace();                throw new SQLException("Failed to set text field.");            }            // Leave bodyReader open so that the db can read from it. It *should*            // be garbage collected after it's done without needing to call close.        }        else {            pstmt.setString(parameterIndex, value);        }    }    /**     * Sets the max number of rows that should be returned from executing a     * statement. The operation is automatically bypassed if Jive knows that the     * the JDBC driver or database doesn't support it.     *     * @param stmt the Statement to set the max number of rows for.     * @param maxRows the max number of rows to return.     */    public static void setMaxRows(Statement stmt, int maxRows) {        if (supportsMaxRows) {            try {                stmt.setMaxRows(maxRows);            }            catch (Throwable t) {                // Ignore. Exception may happen if the driver doesn't support                // this operation and we didn't set meta-data correctly.                // However, it is a good idea to update the meta-data so that                // we don't have to incur the cost of catching an exception                // each time.                supportsMaxRows = false;            }        }    }    /**     * Sets the number of rows that the JDBC driver should buffer at a time.     * The operation is automatically bypassed if Jive knows that the     * the JDBC driver or database doesn't support it.     *     * @param rs the ResultSet to set the fetch size for.     * @param fetchSize the fetchSize.     */    public static void setFetchSize(ResultSet rs, int fetchSize) {        if (supportsFetchSize) {            try {                rs.setFetchSize(fetchSize);            }            catch (Throwable t) {                // Ignore. Exception may happen if the driver doesn't support                // this operation and we didn't set meta-data correctly.                // However, it is a good idea to update the meta-data so that                // we don't have to incur the cost of catching an exception                // each time.                supportsFetchSize = false;            }        }    }    /**     * Uses a connection from the database to set meta data information about     * what different JDBC drivers and databases support.     */    private static void setMetaData(Connection con) throws SQLException {         DatabaseMetaData metaData = con.getMetaData();        // Supports transactions?        supportsTransactions = metaData.supportsTransactions();        // Supports subqueries?        supportsSubqueries = metaData.supportsCorrelatedSubqueries();        // Supports scroll insensitive result sets?        supportsScrollResults = metaData.supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE);        // Set defaults for other meta properties        streamLargeText = false;        supportsMaxRows = true;        supportsFetchSize = true;        // Get the database name so that we can perform meta data settings.        String dbName = metaData.getDatabaseProductName().toLowerCase();        String driverName = metaData.getDriverName().toLowerCase();        // Oracle properties.        if (dbName.indexOf("oracle") != -1) {            databaseType = DatabaseType.ORACLE;            streamLargeText = true;            // The i-net AUGURO JDBC driver            if (driverName.indexOf("auguro") != -1) {                streamLargeText = false;                supportsFetchSize = true;                supportsMaxRows = false;            }        }        // Postgres properties        else if (dbName.indexOf("postgres") != -1) {            databaseType = DatabaseType.POSTGRES;            // Postgres blows, so disable scrolling result sets.            supportsScrollResults = false;            supportsFetchSize = false;        }        // Interbase properties        else if (dbName.indexOf("interbase") != -1) {            supportsFetchSize = false;            supportsMaxRows = false;        }        // SQLServer, JDBC driver i-net UNA properties        else if (dbName.indexOf("sql server") != -1 &&            driverName.indexOf("una") != -1)        {            supportsFetchSize = true;            supportsMaxRows = false;        }        // MySQL properties        else if (dbName.indexOf("mysql") != -1) {            databaseType = DatabaseType.MYSQL;        }    }    /**     * Returns the database type. The possible types are constants of the     * DatabaseType class. Any database that doesn't have its own constant     * falls into the "Other" category.     *     * @return the database type.     */    public static DatabaseType getDatabaseType() {        return databaseType;    }    /**     * Returns true if connection profiling is turned on. You can collect     * profiling statistics by using the static methods of the ProfiledConnection     * class.     *     * @return true if connection profiling is enabled.     */    public static boolean isProfilingEnabled() {        return profilingEnabled;    }    /**     * Turns connection profiling on or off. You can collect profiling     * statistics by using the static methods of the ProfiledConnection     * class.     *     * @param enable true to enable profiling; false to disable.     */    public static void setProfilingEnabled(boolean enable) {        // If enabling profiling, call the start method on ProfiledConnection        if (!profilingEnabled && enable) {            ProfiledConnection.start();        }        // Otherwise, if turning off, call stop method.        else if(profilingEnabled && !enable) {            ProfiledConnection.stop();        }        profilingEnabled = enable;     }    /**     * A class that identifies the type of the database that Jive is connected     * to. In most cases, we don't want to make any database specific calls     * and have no need to know the type of database we're using. However,     * there are certain cases where it's critical to know the database for     * performance reasons.     */    public static class DatabaseType {        public static final DatabaseType ORACLE = new DatabaseType();        public static final DatabaseType POSTGRES = new DatabaseType();        public static final DatabaseType MYSQL = new DatabaseType();        public static final DatabaseType OTHER = new DatabaseType();        private DatabaseType() {            /* do nothing */        }    }}

⌨️ 快捷键说明

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