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

📄 dbgroup.java

📁 这是学习Java必须读懂两套源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        //group could affect their permissions.
        DbCacheManager cacheManager = factory.getCacheManager();
        cacheManager.removeUserPerm(new Integer(user.getID()));
    }

    public boolean isAdministrator(User user) {
        boolean answer = false;
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(ADMIN_TEST);
            pstmt.setInt(1, id);
            pstmt.setInt(2, user.getID());
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                answer = true;
            }
        }
        catch( SQLException sqle ) {
            sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
        return answer;
    }

    public boolean isMember(User user) {
        boolean answer = false;
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(MEMBER_TEST);
            pstmt.setInt(1, id);
            pstmt.setInt(2, user.getID());
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                answer = true;
            }
        }
        catch( SQLException sqle ) {
            sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
        return answer;
    }

    public int getAdministratorCount() {
        int count = 0;
        boolean answer = false;
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(ADMIN_COUNT);
            pstmt.setInt(1, id);
            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 getMemberCount() {
        int count = 0;
        boolean answer = false;
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(MEMBER_COUNT);
            pstmt.setInt(1, id);
            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 members() {
        ArrayList admins = new ArrayList();
        //Load list of group admins from db.
        Connection con = null;
        PreparedStatement pstmt = null;

        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(LOAD_USERS);
            pstmt.setInt(1, id);
            ResultSet rs = pstmt.executeQuery();
            User user = null;
            while (rs.next()) {
                try {
                    user = profileManager.getUser(rs.getInt("userID"));
                }
                catch (UserNotFoundException unfe) {
                    unfe.printStackTrace(System.out);
                }
                admins.add(user);
            }
        }
        catch( SQLException sqle ) {
            System.err.println( "SQLException in DbGroup.java:" +
            "users():reading group data " + sqle );
            sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
        return admins.iterator();
    }

    public Iterator administrators() {
        ArrayList admins = new ArrayList();
        //Load list of group admins from db.
        Connection con = null;
        PreparedStatement pstmt = null;

        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(LOAD_ADMINS);
            pstmt.setInt(1, id);
            ResultSet rs = pstmt.executeQuery();
            User user = null;
            while (rs.next()) {
                try {
                    user = profileManager.getUser(rs.getInt("userID"));
                }
                catch (UserNotFoundException unfe) {
                    unfe.printStackTrace(System.out);
                }
                admins.add(user);
            }
        }
        catch( SQLException sqle ) {
            System.err.println( "SQLException in DbGroup.java:" +
            "administrators():reading group data " + sqle );
            sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
        return admins.iterator();
    }

    public ForumPermissions getPermissions(Authorization authorization) {
        int userID = authorization.getUserID();
        try {
            User user = profileManager.getUser(userID);
            if (isAdministrator(user)) {
                return new ForumPermissions(false, false, false, false,
                        true, false, false, false);
            }
        }
        catch (Exception e) { }

        return ForumPermissions.none();
    }

    public boolean hasPermission(int type) {
        return true;
    }

    //FROM THE CACHEABLE INTERFACE//

    public int getSize() {
        //Approximate the size of the object in bytes by calculating the size
        //of each field.
        int size = 0;
        size += CacheSizes.sizeOfObject();              //overhead of object
        size += CacheSizes.sizeOfInt();                 //id
        size += CacheSizes.sizeOfString(name);          //name
        size += CacheSizes.sizeOfString(description);   //description
        size += CacheSizes.sizeOfObject();              //profile manager ref.
        size += CacheSizes.sizeOfObject();              //forum factory ref.

        return size;
    }

    //OTHER METHODS

    /**
     * Returns a String representation of the Group object using the group name.
     *
     * @return a String representation of the Group object.
     */
    public String toString() {
        return name;
    }

    public int hashCode() {
        return id;
    }

    public boolean equals(Object object) {
        if (this == object) {
            return true;
        }
        if (object != null && object instanceof DbGroup) {
            return id == ((DbGroup)object).getID();
        }
        else {
            return false;
        }
    }

    /**
     * Load the group data from the database.
     */
    private synchronized void loadFromDb() throws GroupNotFoundException {
        String query;
        if (name == null) {
            query = LOAD_GROUP_BY_ID;
        }
        else {
            query = LOAD_GROUP_BY_NAME;
        }
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(query);
            if (name == null) {
                pstmt.setInt(1, id);
            }
            else {
                pstmt.setString(1, name);
            }
            ResultSet rs = pstmt.executeQuery();
            if (!rs.next()) {
                throw new GroupNotFoundException();
            }
            this.id = rs.getInt("groupID");
            this.name = rs.getString("name");
            this.description = rs.getString("description");
        }
        catch( SQLException sqle ) {
            System.err.println( "SQLException in DbGroup.java:" +
            "loadFromDb():reading group data " + sqle );
            throw new GroupNotFoundException();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
    }

    /**
     * Inserts a new record into the database.
     */
    private void insertIntoDb() {
        StringBuffer insert = new StringBuffer();
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(INSERT_GROUP);
            pstmt.setString(1, name);
            pstmt.setString(2, description);
            pstmt.setInt(3, id);
            pstmt.executeUpdate();
        }
        catch( SQLException sqle ) {
            System.err.println("Error in DbGroup:insertIntoDb()-" + sqle);
            sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
    }

    /**
     * Saves group data to the db.
     */
    private synchronized void saveToDb() {
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(SAVE_GROUP);
            pstmt.setString(1, name);
            pstmt.setString(2, description);
            pstmt.setInt(3, id);
            pstmt.executeUpdate();
        }
        catch( SQLException sqle ) {
            System.err.println( "SQLException in DbGroup.java:saveToDb(): " + sqle );
            sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
    }
}

⌨️ 快捷键说明

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