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

📄 dbprofilemanager.java

📁 这是学习Java必须读懂两套源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            pstmt.setInt(1,userID);
            pstmt.execute();
            pstmt.close();
            //remove user from all groups
            pstmt = con.prepareStatement(DELETE_USER_GROUPS);
            pstmt.setInt(1,userID);
            pstmt.execute();
            pstmt.close();
            //delete all of the users's extended properties
            pstmt = con.prepareStatement(DELETE_USER_PROPS);
            pstmt.setInt(1,userID);
            pstmt.execute();
            pstmt.close();
            //delete the actual user entry
            pstmt = con.prepareStatement(DELETE_USER);
            pstmt.setInt(1,userID);
            pstmt.execute();
        }
        catch( SQLException sqle ) {
            sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }

        //Finally, expire all relevant caches
        //all of users's messages
        DbCacheManager cacheManager = factory.getCacheManager();
        for (int i=0; i<messages.length; i++) {
            cacheManager.remove(
                    DbCacheManager.MESSAGE_CACHE,
                    new Integer(messages[i])
            );
        }
        //user cache
        cacheManager.remove(DbCacheManager.USER_ID_CACHE, user.getUsername());
        cacheManager.remove(DbCacheManager.USER_CACHE, new Integer(userID));
    }

    public Group createGroup(String name) throws UnauthorizedException,
            GroupAlreadyExistsException
    {
        Group newGroup = null;
        try {
            Group existingGroup = getGroup(name);

            //The group already exists since now exception, so:
            throw new GroupAlreadyExistsException();
        }
        catch (GroupNotFoundException unfe) {
            //The group doesn't already exist so we can create a new group
            newGroup = new DbGroup(name, factory);
        }
        return newGroup;
    }

    public Group getGroup(int groupID) throws GroupNotFoundException {
        DbCacheManager cacheManager = factory.getCacheManager();
        //If cache is not enabled, do a new lookup of object
        if (!cacheManager.isCacheEnabled()) {
            return new DbGroup(groupID, factory);
        }
        //Cache is enabled.
        Integer groupIDInteger = new Integer(groupID);
        DbGroup group = (DbGroup)cacheManager.get(
                DbCacheManager.GROUP_CACHE,
                groupIDInteger
        );
        if(group == null) {
            group = new DbGroup(groupID, factory);
            cacheManager.add(DbCacheManager.GROUP_CACHE, groupIDInteger, group);
        }
        return group;
    }

    public Group getGroup(String name) throws GroupNotFoundException {
        DbCacheManager cacheManager = factory.getCacheManager();
        //If cache is not enabled, do a new lookup of object
        if (!cacheManager.isCacheEnabled()) {
            Group group = new DbGroup(name, null, factory);
            return getGroup(group.getID());
        }
        //Cache is enabled.
        CacheableInteger groupIDInteger = (CacheableInteger)cacheManager.get(
                DbCacheManager.GROUP_ID_CACHE,
                name
        );
        //if id wan't found in cache, load it up and put it there.
        if (groupIDInteger == null) {
            Group group = new DbGroup(name, null, factory);
            groupIDInteger = new CacheableInteger(new Integer(group.getID()));
            cacheManager.add(DbCacheManager.GROUP_ID_CACHE, name, groupIDInteger);
        }
        return getGroup(groupIDInteger.getInteger().intValue());
    }

    public void deleteGroup(Group group) throws UnauthorizedException {
        int groupID = group.getID();
        int [] members = new int[group.getMemberCount()];
        Iterator iter = group.members();
        for (int i=0; i<members.length; i++) {
            User user = (User)iter.next();
            members[i] = user.getID();
        }

        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            //mark all message by user as anonymous
            pstmt = con.prepareStatement(DELETE_GROUP_USERS);
            pstmt.setInt(1,groupID);
            pstmt.execute();
            pstmt.close();
            //remove all permissions given to user
            pstmt = con.prepareStatement(DELETE_GROUP);
            pstmt.setInt(1,groupID);
            pstmt.execute();
            pstmt.close();
        }
        catch( SQLException sqle ) {
            sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }

        //Finally, expire all relevant caches
        DbCacheManager cacheManager = factory.getCacheManager();
        cacheManager.remove(DbCacheManager.GROUP_ID_CACHE, group.getName());
        cacheManager.remove(DbCacheManager.GROUP_CACHE, new Integer(groupID));
        //Removing a group can change the permissions of all the users in that
        //group. Therefore, remove each user from the user perms cache.
        for (int i=0; i<members.length; i++) {
            cacheManager.removeUserPerm(new Integer(members[i]));
        }
    }

    public int getUserCount() {
        int count = 0;
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(USER_COUNT);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                count = rs.getInt(1);
            }
        }
        catch( SQLException sqle ) {
            sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
        return count;
    }

    public int getGroupCount() {
        int count = 0;
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(GROUP_COUNT);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                count = rs.getInt(1);
            }
        }
        catch( SQLException sqle ) {
            sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
        return count;
    }

    public Iterator users() {
        return new DbUserIterator(this);
    }

    public Iterator users(int startIndex, int numResults) {
        return new DbUserIterator(this, startIndex, numResults);
    }

    public Iterator groups() {
        return new DbGroupIterator(this);
    }

    public Iterator groups(int startIndex, int numResults) {
        return new DbGroupIterator(this, startIndex, numResults);
    }

    public int userMessageCount(User user, Forum forum) {
        int count = 0;
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(USER_MESSAGE_COUNT);
            pstmt.setInt(1, user.getID());
            pstmt.setInt(2, forum.getID());
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                count = rs.getInt(1);
            }
        }
        catch( SQLException sqle ) {
            sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
        return count;
    }

    public Iterator userMessages(User user, Forum forum) {
        return new DbUserMessagesIterator(factory, user, forum);
    }

    /**
     * Returns an array of all the groups that the user belongs to.
     */
    protected int[] getUserGroups(int userID) {
        Connection con = null;
        PreparedStatement pstmt = null;
        int [] groups = new int[0];
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(USER_GROUPS);
            pstmt.setInt(1,userID);
            ResultSet rs = pstmt.executeQuery();
            ArrayList groupList = new ArrayList();
            while (rs.next()) {
                groupList.add(new Integer(rs.getInt("groupID")));
            }
            groups = new int[groupList.size()];
            for (int i=0; i<groups.length; i++) {
                groups[i] = ((Integer)groupList.get(i)).intValue();
            }
        }
        catch( SQLException sqle ) {
            sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
        return groups;
    }
}

⌨️ 快捷键说明

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