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

📄 jahiauserdbutils.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;/** * A JahiaUser represents a physical person who is defined by a username and * a password for authentification purpose. Every other property of a JahiaUser * is stored in it's properties list, which hold key-value string pairs. * For example email, firstname, lastname, ... information should be stored in * this properties list. * * @author  Fulco Houkes * @version 1.1 */class JahiaUserDBUtils{    static private JahiaUserDBUtils mObject = null;    //--------------------------------------------------------------------------    private JahiaUserDBUtils () {    }    //--------------------------------------------------------------------------    /** Get the user database utilities object reference.     *     * @return     *      The user database utilities reference.     */    static public JahiaUserDBUtils getInstance ()    {        if (mObject == null) {            mObject = new JahiaUserDBUtils ();        }        return mObject;    }    //--------------------------------------------------------------------------    // FH       29 Mar. 2001    //          Initial implementation    //    /**     * 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 userID)            throws  JahiaDatabaseException,                    JahiaDatabaseConnectionException,                    JahiaException    {        boolean result = false;        String query = "DELETE FROM jahia_user_prop WHERE"+                " id_jahia_users="+ userID +                " AND name_jahia_user_prop='"+ propertyKey +"'";        return makeQuery (query);    }    //---------------------------------------------------------------------------    // FH       29 Mar. 2001    //          Initial implementation    //    /**     * Change the user's password.     *     * @param   newPassword     *      New user's password     *     * @return     *      Return true id the old password is the same as the current one and     *      the new password is valid. Return false on any failure.     */    public synchronized boolean setPassword (String password, int userID)        throws  JahiaDatabaseException,                JahiaDatabaseConnectionException,                JahiaException    {        boolean result = false;        // try to avoid a NullPointerException        if (password != null)        {            // password should not be empty.            if (password.length() > 0)            {                // Encrypt the new password                String tmp = JahiaUserManagerService.                                    encryptPassword (password);                String query = "UPDATE jahia_users SET password_jahia_users='"+                        tmp +"' WHERE id_jahia_users="+ userID;                result = makeQuery (query);            }        }        return result;    }    //--------------------------------------------------------------------------    // FH       29 Mar. 2001    //          Initial implementation    //    /**     * Add (or update if not already in the property list) a property key-value     * pair in the user'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 userID)        throws  JahiaDatabaseException,                JahiaDatabaseConnectionException,                JahiaException    {        boolean result = false;        String query = "INSERT INTO jahia_user_prop (id_jahia_users, "+                    "name_jahia_user_prop, value_jahia_user_prop)"+                    " VALUES ("+ userID +",'"+ key +"','"+ value +"')";        return makeQuery (query);    }    //--------------------------------------------------------------------------    // FH       29 Mar. 2001    //          Initial implementation    //    /**     * Add (or update if not already in the property list) a property key-value     * pair in the user'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 userID)        throws  JahiaDatabaseException,                JahiaDatabaseConnectionException,                JahiaException    {        boolean result = false;        String query = "UPDATE jahia_user_prop SET "+                       "value_jahia_user_prop='"+ value +"'"+                       " WHERE id_jahia_users="+ userID +                       "   AND name_jahia_user_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 (                   "JahiaDBUser 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 JahiaDBUser",                    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 JahiaDBUser",                    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",                        "JahiaDBUser Could not get the DBPoolService instance",                        JahiaException.SERVICE_ERROR, JahiaException.CRITICAL);            }        } else {            throw new JahiaException ("Jahia Internal Error",                    "JahiaDBUser Could not get the Service Registry instance",                    JahiaException.SERVICE_ERROR, JahiaException.CRITICAL);        }        return dbPoolService;    }}

⌨️ 快捷键说明

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