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

📄 jahiagroupdbutils.java

📁 java 写的一个新闻发布系统
💻 JAVA
字号:
////                                   ____.//                       __/\ ______|    |__/\.     _______//            __   .____|    |       \   |    +----+       \//    _______|  /--|    |    |    -   \  _    |    :    -   \_________//   \\______: :---|    :    :           |    :    |         \________>//           |__\---\_____________:______:    :____|____:_____\//                                      /_____|////                 . . . i n   j a h i a   w e   t r u s t . . .//package org.jahia.services.usermanager;import java.sql.Connection;import java.sql.Statement;import java.sql.ResultSet;import java.sql.SQLException;import org.jahia.exceptions.JahiaException;import org.jahia.exceptions.database.JahiaDatabaseConnectionException;import org.jahia.exceptions.database.JahiaDatabaseException;import org.jahia.services.usermanager.JahiaUserManagerService;import org.jahia.services.database.JahiaDBPoolService;import org.jahia.registries.ServicesRegistry;/** * * @author  Khue Nguyen * @version 1.1 */class JahiaGroupDBUtils{    static private JahiaGroupDBUtils mObject = null;    //--------------------------------------------------------------------------    private JahiaGroupDBUtils () {    }    //--------------------------------------------------------------------------    /** Get the user database utilities object reference.     *     * @return     *      The user database utilities reference.     */    static public JahiaGroupDBUtils getInstance ()    {        if (mObject == null) {            mObject = new JahiaGroupDBUtils ();        }        return mObject;    }    //--------------------------------------------------------------------------    /**     * Remove the specified property from the properties list.     *     * @param   key     Property's name.     *     * @return  Return true on success or false on any failure.     */    public synchronized boolean removeProperty (String propertyKey, int grpID)            throws  JahiaDatabaseException,                    JahiaDatabaseConnectionException,                    JahiaException    {        boolean result = false;        String query = "DELETE FROM jahia_grp_prop WHERE"+                " id_jahia_grp="+ grpID +                " AND name_jahia_grp_prop='"+ propertyKey +"'";        return makeQuery (query);    }    //--------------------------------------------------------------------------    /**     * Add (or update if not already in the property list) a property key-value     * pair in the grp's properties list.     *     * @param  key      Property's name.     * @param  value    Property's value.     *     * @return  Return true on success or false on any failure.     */    public synchronized boolean addProperty (String key, String value,                                             int grpID)        throws  JahiaDatabaseException,                JahiaDatabaseConnectionException,                JahiaException    {        boolean result = false;        String query = "INSERT INTO jahia_grp_prop (id_jahia_grp, "+                    "name_jahia_grp_prop, value_jahia_grp_prop)"+                    " VALUES ("+ grpID +",'"+ key +"','"+ value +"')";        return makeQuery (query);    }    //--------------------------------------------------------------------------    /**     * Add (or update if not already in the property list) a property key-value     * pair in the grp's properties list.     *     * @param  key      Property's name.     * @param  value    Property's value.     *     * @return  Return true on success or false on any failure.     */    public synchronized boolean updateProperty (String key, String value,                                                int grpID)        throws  JahiaDatabaseException,                JahiaDatabaseConnectionException,                JahiaException    {        boolean result = false;        String query = "UPDATE jahia_grp_prop SET "+                       "value_jahia_grp_prop='"+ value +"'"+                       " WHERE id_jahia_grp="+ grpID +                       "   AND name_jahia_grp_prop='"+ key +"'";        return makeQuery (query);    }    //--------------------------------------------------------------------------    // Executes and INSERT, UPDATE or DELETE SQL operation. This method should not    // be used with and SELECT operation. This method lock the object on database    // write access.    private boolean makeQuery (String query)        throws  JahiaDatabaseException,                JahiaDatabaseConnectionException,                JahiaException    {        // Get a database connection        Connection  dbConn = getDBConnection (2003);        if (dbConn == null) {            return false;        }        boolean     result = false;        Statement   statement = null;        try {            statement = dbConn.createStatement();            if (statement != null) {                synchronized (this) {                    ServicesRegistry.getInstance().getDBPoolService().executeUpdate (statement,query);                    result = true;                }            }        }        catch (SQLException sqlEx) {            throw new JahiaDatabaseException (                    "", query, sqlEx, JahiaDatabaseException.ERROR);        }        finally {            CloseStatement (statement);            CloseDBConnection (dbConn);        }        return result;    }    //-------------------------------------------------------------------------    private Connection getDBConnection (int debugInfo)        throws  JahiaDatabaseConnectionException,                JahiaException    {        JahiaDBPoolService dbPoolService = getPoolService ();        Connection dbConn = null;        try {            dbConn = dbPoolService.getConnection (debugInfo);        }        catch (SQLException ex) {            throw new JahiaDatabaseConnectionException (                   "JahiaGroupDBUtils could not get the DB Pool Connection");        }        return dbConn;    }    //-------------------------------------------------------------------------    private void CloseDBConnection (Connection dbConn)        throws  JahiaDatabaseException,                JahiaException    {        if (dbConn != null) {            JahiaDBPoolService dbPoolService = getPoolService ();            try {                dbPoolService.freeConnection (dbConn);            }            catch (SQLException sqlEx) {                throw new JahiaDatabaseException (                    "Could not close a database connection in JahiaGroupDBUtils",                    sqlEx, JahiaDatabaseException.ERROR);            }        }    }    //-------------------------------------------------------------------------    private void CloseStatement (Statement statement)        throws  JahiaDatabaseException    {        // Close the opened statement        try {            if (statement!=null) {                statement.close();            }        }        catch (SQLException sqlEx) {            throw new JahiaDatabaseException (                    "Could not close a statement in JahiaGroupDBUtils",                    sqlEx, JahiaDatabaseException.ERROR);        }    }    //-------------------------------------------------------------------------    private JahiaDBPoolService getPoolService ()        throws  JahiaException    {        JahiaDBPoolService dbPoolService = null;        // Try to get the DB Pool Service        ServicesRegistry registry = ServicesRegistry.getInstance();        if (registry != null)        {            dbPoolService = registry.getDBPoolService();            if (dbPoolService == null)            {                throw new JahiaException ("Jahia Internal Error",                        "JahiaGroupDBUtils Could not get the DBPoolService instance",                        JahiaException.SERVICE_ERROR, JahiaException.CRITICAL);            }        } else {            throw new JahiaException ("Jahia Internal Error",                    "JahiaGroupDBUtils Could not get the Service Registry instance",                    JahiaException.SERVICE_ERROR, JahiaException.CRITICAL);        }        return dbPoolService;    }}

⌨️ 快捷键说明

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