📄 friendsmanager.java
字号:
package com.bcxy.bbs.forum;
/**
* Title:
* Description:
* Copyright:
* Company: www.liyunet.com
*
* @author lishujiang
* @version 1.0
*/
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import com.bcxy.bbs.util.BBSConst;
import com.bcxy.bbs.util.GCookie;
import com.bcxy.bbs.util.ParamUtil;
import com.bcxy.db.JdbcWrapper;
public class FriendsManager {
String userName = "";
String sql = "";
private static Logger log = Logger.getLogger(FriendsManager.class);
public FriendsManager(HttpServletRequest request) {
userName = GCookie.getCookieValue(request, "UJBBUName", "");
}
public static Vector getFriendList(HttpServletRequest request)
throws Exception {
String userName = GCookie.getCookieValue(request, "UJBBUName", "");
String sql = "select F_friend from " + BBSConst.TABLE_FRIEND
+ " where F_username='" + userName
+ "' order by F_addtime desc";
JdbcWrapper jw = new JdbcWrapper();
Vector friendList = new Vector();
try {
jw.executeQuery(sql);
while (jw.next()) {
Friend friend = new Friend();
friend.setFriendFriend(jw.getString(1));
friendList.add(friend);
}
} catch (Exception e) {
log.error("取得好友列表出错", e);
throw new Exception("取得好友列表出错");
} finally {
jw.close();
}
return friendList;
}
public Vector getFriendInfo(HttpServletRequest request) throws Exception {
sql = "select F.*,U.useremail,U.homepage,U.oicq from "
+ BBSConst.TABLE_FRIEND + " F inner join "
+ BBSConst.TABLE_USER
+ " U on F.F_Friend=U.username where F.F_username='" + userName
+ "' order by F.f_addtime desc";
//
JdbcWrapper jw = new JdbcWrapper();
Vector friendInfo = new Vector();
try {
jw.executeQuery(sql);
while (jw.next()) {
Friend friend = new Friend();
friend.setFriendID(jw.getInt(1));
friend.setFriendUserName(jw.getString(2));
friend.setFriendFriend(jw.getString(3));
friend.setFriendAddTime(jw.getString(4));
friend.setFriendEmail(jw.getString(5));
friend.setFriendHomePage(jw.getString(6));
friend.setFriendOicq(jw.getString(7));
friendInfo.add(friend);
}
} catch (Exception e) {
log.error("取得好友信息出错", e);
throw new Exception("取得好友信息出错");
} finally {
jw.close();
}
return friendInfo;
}
public void delFriend(HttpServletRequest request) throws Exception {
int ID = 0;
try {
ID = ParamUtil.getInt(request, "id");
} catch (Exception e) {
throw new Exception("请指定相关参数。");
}
String userName = GCookie.getCookieValue(request, "UJBBUName", "");
JdbcWrapper jw = new JdbcWrapper();
sql = "delete from " + BBSConst.TABLE_FRIEND
+ " where F_username=? and F_id=" + ID;
try {
jw.prepareStatement(sql);
jw.setString(1, userName);
jw.executeUpdate();
} catch (Exception e) {
log.error("删除好友信息出错", e);
throw new Exception("删除好友信息出错");
} finally {
jw.close();
}
}
public void allDelFriend() throws Exception {
JdbcWrapper jw = new JdbcWrapper();
sql = "delete from " + BBSConst.TABLE_FRIEND + " where F_username='"+userName+"'";
try {
jw.executeUpdate(sql);
} catch (Exception e) {
log.error("删除好友信息出错", e);
throw new Exception("删除好友信息出错");
} finally {
jw.close();
}
}
public void saveFriend(HttpServletRequest request) throws Exception {
String touser = ParamUtil.getString(request, "touser");
if (touser == null || "".equals(touser.trim()))
throw new Exception("您忘记填写发送对象了吧。");
String userName = GCookie.getCookieValue(request, "UJBBUName", "");
touser = touser.trim();
String[] incept = touser.split(",");
JdbcWrapper jw = new JdbcWrapper();
try {
for (int i = 0; i < incept.length; i++) {
if (i > 4) {
throw new Exception("每次最多只能添加5位用户,您的名单5位以后的请重新填写");
}
sql = "select username from " + BBSConst.TABLE_USER
+ " where username='" + incept[i] + "'";
if (!jw.isExists(sql)) {
throw new Exception("论坛没有这个用户,操作未成功。");
}
sql = "select F_friend from " + BBSConst.TABLE_FRIEND
+ " where F_username='" + userName
+ "' and F_friend='" + incept[i] + "'";
if (!jw.isExists(sql)) {
sql = "insert into "
+ BBSConst.TABLE_FRIEND
+ " (F_username,F_friend,F_addtime) values (?,?,now())";
jw.prepareStatement(sql);
jw.setString(1, userName);
jw.setString(2, incept[i]);
jw.executeUpdate();
}
}
} catch (Exception e) {
log.error("添加好友信息出错", e);
throw e;
} finally {
jw.close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -