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

📄 frienddao.java

📁 EasyJForum 是一个基于 Java 技术的免费社区论坛软件系统
💻 JAVA
字号:
package com.hongshee.ejforum.data;

/**
 * <p>Title: FriendDAO.java</p>
 * <p>Description: Friend data access object</p>
 * <p>Copyright: hongshee.com (c) 2008</p>
 * @author jackie
 * @version 1.0
 */

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import com.hongshee.ejforum.data.UserDAO.UserInfo;
import com.hongshee.ejforum.util.PageUtils;
import com.hongshee.ejforum.util.AppUtils;

public class FriendDAO extends EntityDAO
{
    private static FriendDAO _dao = null;

    protected FriendDAO()
    {}

    public static FriendDAO getInstance()
    {
        if (_dao == null)
        {
            _dao = new FriendDAO();
        }
        return _dao;
    } 
        
    /**
     * 添加一个论坛用户好友
     * @param 
     *      request - HttpServletRequest
     * @return 无
     * @throws SQLException
     * @since 1.0
     */
    public String addFriend(HttpServletRequest request, UserInfo userinfo) throws SQLException
    {
        String friendID = PageUtils.getParam(request,"friendID");
        String remark = PageUtils.getParam(request,"remark");
        remark = remark.replace("<", "&lt;");
        remark = remark.replace(">", "&gt;");

        Connection conn = null;
        PreparedStatement pstmtInsert = null;
        conn = dbManager.getConnection();
        try
        {
            boolean isExistedID = UserDAO.getInstance().isExistedID(friendID, conn);
            if (!isExistedID) 
            {
                return "添加好友失败! 用户名无效: " + friendID;
            }
            
            pstmtInsert = conn.prepareStatement(adapter.Friend_Insert);
            pstmtInsert.setString(1, userinfo.userID);
            pstmtInsert.setString(2, friendID);
            pstmtInsert.setString(3, remark);
            pstmtInsert.executeUpdate();
            
            return "OK";
        }
        catch(SQLException sqle)
        {
            ArrayList<Object> paramValues = new ArrayList<Object>();
            paramValues.add(userinfo.userID);
            paramValues.add(friendID);
            int count = 
                this.execSelectCountSql(adapter.Friend_IsExistedFriend, paramValues, conn);
            if (count >= 1)
                return "添加好友失败! 此用户已经在好友列表中: " + friendID;
            else
                throw sqle;
        }
        finally
        {
            dbManager.closePStatement(pstmtInsert);
            dbManager.closeConnection(conn);
        }
    }

    /**
     * 查询论坛用户好友列表
     * @param 
     *      userID - 论坛用户ID
     * @return 好友列表
     * @throws SQLException
     * @since 1.0
     */
    public Object[] getFriends(String userID,
                               int pageNo, int pageRows) throws SQLException
    {
        Object[] result = new Object[2];
        String querySql = adapter.Friend_GetList;
        String countSql = adapter.Friend_GetCount;
        
        int totalCount = 0;
        Connection conn = null;
        PreparedStatement pstmtQuery = null;
        ResultSet rs = null;
        try
        {
            conn = dbManager.getConnection();
            pstmtQuery = conn.prepareStatement(countSql);
            pstmtQuery.setString(1, userID);
            rs = pstmtQuery.executeQuery();
            if(rs.next())
            {
                totalCount = rs.getInt(1);
            }
            
            if (totalCount > 0)
            {
                dbManager.closeResultSet(rs);
                dbManager.closePStatement(pstmtQuery);
             
                querySql = adapter.getPageQuerySql(
                            new StringBuilder(querySql),pageNo, pageRows, totalCount);
            
                pstmtQuery = conn.prepareStatement(querySql);
                pstmtQuery.setString(1, userID);
                rs = pstmtQuery.executeQuery();
            
                ArrayList<FriendVO> friendList = new ArrayList<FriendVO>();
                FriendVO aFriend = null;
            
                while(rs.next())
                {
                    aFriend = new FriendVO();
                    aFriend.userID = userID;
                    aFriend.friendID = rs.getString("friendID");
                    aFriend.nickname = rs.getString("nickname");
                    aFriend.remark = rs.getString("remark");
                    aFriend.createTime = AppUtils.formatSQLTimeStr(rs.getTimestamp("createTime"));

                    friendList.add(aFriend);
                }
                result[1] = friendList;
            }
        }
        finally
        {
            dbManager.closeResultSet(rs);
            dbManager.closePStatement(pstmtQuery);
            dbManager.closeConnection(conn);
        }
        // Get result page code
        if (totalCount > 0)
        {
            int pageCount = (totalCount - 1) / pageRows + 1;
            if (pageCount <= 1) return result;
            result[0] = PageUtils.getPageHTMLStr(totalCount, pageNo, pageRows, 0);
        }        
        return result;
    }

    /**
     * 删除论坛用户好友
     * @param 
     *      friendID - 好友ID
     * @return 无
     * @throws SQLException
     * @since 1.0
     */
    public void deleteFriends(HttpServletRequest request, UserInfo userinfo) throws SQLException
    {
        Connection conn = null;
        PreparedStatement pstmtUpdate = null;
        try
        {
            String[] friendIDs = request.getParameterValues("friendID");
            
            conn = dbManager.getConnection();
            pstmtUpdate = conn.prepareStatement(adapter.Friend_Delete);
                
            for (int i=0; i<friendIDs.length; i++)
            {
                pstmtUpdate.setString(1, userinfo.userID);
                pstmtUpdate.setString(2, friendIDs[i]);
                pstmtUpdate.addBatch();
            }
            pstmtUpdate.executeBatch();
        }
        finally
        {
            dbManager.closePStatement(pstmtUpdate);
            dbManager.closeConnection(conn);
        }
    }

    public static class FriendVO
    {
        public String userID = null;
        public String friendID = null;
        public String nickname = null;
        public String remark = null;
        public String createTime = null;
    }    
}

⌨️ 快捷键说明

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