📄 clubmyfriends.java
字号:
/*
* ClubMyFriends.java
* Made In GamVan
* Created on 2005年4月7日, 下午3:39
*/
package com.gamvan.club.user;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.StringTokenizer;
import com.gamvan.club.ClubUsers;
import com.gamvan.club.message.ClubMessage;
import com.gamvan.conn.ConnClub;
import com.gamvan.tools.Gb;
public class ClubMyFriends {
public String userFriend = "";
public String userMe = "";
public String message = "";
public String postMsg = "";
public String userMessage = "";
public String userIp = "";
public int userMeID
, userFriendID = 0
, isOnline=0
, friendOnlineCount=0;
//格式化当前时间
java.text.SimpleDateFormat isNow = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private String now = isNow.format(new java.util.Date());
ClubUsers cu = new ClubUsers();
ConnClub bridge = new ConnClub();
ClubMessage cm = new ClubMessage();
Gb clubgb = new Gb();
//更新用户是否在线配合CLubOnlie类。
public void friendOnline() throws Exception{
String sqlCommand = new String();
Connection con = bridge.getConnection();
String userfriend="";
int userfriendID=0;
try{
Statement sta = con.createStatement();
sqlCommand = "Select * From GVclubFriends where userMeID="+ userMeID +" order by isOnline desc, cfID desc";
ResultSet rs = sta.executeQuery(sqlCommand);
friendOnlineCount=0; //初始化在线好友人数
while(rs.next()){
userfriend = rs.getString(3);
userfriendID = rs.getInt(8);
//查询如果在线名单有好友名字则更新好友状态为在线
if(friendOnlineSelect(userfriend)){
friendOnlineUpdate(userfriendID, 1);
}else{
friendOnlineUpdate(userfriendID, 0); //更新为离线
}
}
rs.close();
sta.close();
}catch(Exception e){
}finally{
con.close();
}
}
//判断好友是否在线。
public boolean friendOnlineSelect(String userFriend) throws Exception{
boolean bea = false;
String sqlCommand = new String();
Connection con = bridge.getConnection();
try{
sqlCommand = "select * From GVclubOnline where userName=?";
PreparedStatement pps = con.prepareStatement(sqlCommand);
pps.setString(1, userFriend);
ResultSet rs = pps.executeQuery();
if(rs.next()){
bea = true;
}
rs.close();
pps.close();
}catch(Exception e){
bea = false;
}finally{
con.close();
}
return bea;
}
public void friendOnlineUpdate(int userfriendID, int isOnline) throws Exception{
String sqlCommand = new String();
Connection con = bridge.getConnection();
try{
sqlCommand = "Update GVclubFriends set isOnline=? where userMeID=? and userFriendID=?"; //更新自己为在线
PreparedStatement pps = con.prepareStatement(sqlCommand);
pps.setInt(1,isOnline);
pps.setInt(2,userMeID);
pps.setInt(3,userfriendID);
pps.executeUpdate();
pps.close();
friendOnlineCount = friendOnlineCount + isOnline;
}catch(Exception e){
}finally{
con.close();
}
}
public boolean friendSelect(int userMeID, int userFriendID) throws Exception{
boolean bea = false;
StringBuffer sqlCommand = new StringBuffer("");
Connection con = bridge.getConnection();
try{
sqlCommand.append("Select * From GVclubFriends where userMeID=? and userFriendID=?");
PreparedStatement pps = con.prepareStatement(sqlCommand.toString());
pps.setInt(1, userMeID);
pps.setInt(2, userFriendID);
ResultSet rs = pps.executeQuery();
if(rs.next()){
bea = true;
}
rs.close();
pps.close();
}catch(Exception e){
bea = false;
}finally{
con.close();
}
return bea;
}
public boolean friendSelect(String userMe, String userFriend) throws Exception{
boolean bea = false;
StringBuffer sqlCommand = new StringBuffer("");
Connection con = bridge.getConnection();
try{
sqlCommand.append("Select * From GVclubFriends where userMe=? and userFriend=?");
PreparedStatement pps = con.prepareStatement(sqlCommand.toString());
pps.setString(1, userMe);
pps.setString(2, userFriend);
ResultSet rs = pps.executeQuery();
if(rs.next()){
bea = true;
}
rs.close();
pps.close();
}catch(Exception e){
bea = false;
con.close();
}finally{
con.close();
}
return bea;
}
public boolean friendsInsert(String userFriend) throws Exception {
userFriend = clubgb.gb(userFriend);
boolean bea = false;
if(userFriend==null || userFriend.equals("")){
message = "朋友ID不能为空!";
return false;
}
StringTokenizer st = new StringTokenizer(userFriend,",");
String[] userFriends = new String[st.countTokens()];
try{
for(int i=0; st.hasMoreTokens(); i++){
userFriends[i] = st.nextToken().trim();
friendInsert(userFriends[i], userMe);
}
}catch(Exception e){
message = e.toString();
}
return bea;
}
public boolean friendInsert(String friendName, String userMe) throws Exception{
boolean bea = false;
if(friendSelect(userMe,friendName)){
message += friendName + " 已存在在您的好友列表,添加操作被终止!<br/>";
return false;
}
StringBuffer sqlCommand = new StringBuffer("");
Connection con = bridge.getConnection();
try{
if(cu.userSelect(friendName)){ //判断ID是否为注册用户
userFriendID = cu.getUserID(); //取得用户数据库ID号
sqlCommand.append("Insert into GVclubFriends(userMe, userFriend, addTime, userMessage, userIp, userMeID, userFriendID, isOnline)");
sqlCommand.append(" Values(?,?,?,?,?,?,?,?)");
PreparedStatement pps = con.prepareStatement(sqlCommand.toString());
pps.setString(1,userMe);
pps.setString(2,friendName);
pps.setString(3, now);
pps.setString(4, clubgb.gb(userMessage));
pps.setString(5, userIp);
pps.setInt(6, userMeID);
pps.setInt(7, userFriendID);
pps.setInt(8,0); //默认加入用户状态为离线
pps.executeUpdate();
pps.close();
bea = true;
message += friendName + " 被您加为好友<br/>";
if(postMsg.equals("1")){
cm.setContent(clubgb.gb(userMessage));
cm.setTopic("社区提醒: 来自 " + userMe + " 的短消息");
cm.setUserIp(userIp);
cm.setReID(0);
cm.setSendUser(userMe);
cm.setIsSend(1);
cm.setOrder(0);
cm.setIsPost("0");
cm.setTakeUser(friendName);
cm.setSendID(userMeID);
cm.setTakeID(userFriendID);
cm.cmAddTake(friendName);
}
}else{
message += friendName + " 此ID不是注册用户,添加失败!<br/>";
}
}catch(Exception e){
message = e.toString();
bea = false;
}finally{
con.close();
}
userFriend = "";
return bea;
}
public boolean friendDel(String[] cfIDs) throws Exception{
boolean bea = false;
StringBuffer sqlCommand = new StringBuffer("");
Connection con = bridge.getConnection();
StringBuffer cfID = new StringBuffer("0");
int delCount=0;
for(int i=0; i<cfIDs.length; i++){
delCount++;
cfID.append(",");
cfID.append(cfIDs[i]);
}
try{
sqlCommand.append("Delete From GVclubFriends where cfID in ("+ cfID.toString() +")");
PreparedStatement pps = con.prepareStatement(sqlCommand.toString());
pps.executeUpdate();
pps.close();
bea = true;
message = String.valueOf(delCount)+" 个好友被删除。";
}catch(Exception e){
message = e.toString();
con.close();
}finally{
con.close();
}
return bea;
}
public boolean friendDel(int cfID) throws Exception{
boolean bea = false;
StringBuffer sqlCommand = new StringBuffer("");
Connection con = bridge.getConnection();
try{
sqlCommand.append("Delete From GVclubFriends where cfID=?");
PreparedStatement pps = con.prepareStatement(sqlCommand.toString());
pps.setInt(1, cfID);
pps.executeUpdate();
pps.close();
bea = true;
}catch(Exception e){
message = e.toString();
con.close();
}finally{
con.close();
}
return bea;
}
public void setUserMeID(int userMeID){
this.userMeID = userMeID;
}
public void setUserMe(String userMe){
if(userMe!=null && !userMe.equals("")){
this.userMe = userMe;
}else{
this.userMe = "";
}
}
public String getMessage(){
return this.message;
}
public void setPostMsg(String postMsg){
if(postMsg==null){
this.postMsg = "0";
}else{
this.postMsg = postMsg;
}
}
public void setUserMessage(String userMessage){
if(userMessage==null){
this.userMessage = "";
}else{
this.userMessage = userMessage;
}
}
public void setUserIp(String userIp){
this.userIp = userIp;
}
public String getUserIp(){
return this.userIp;
}
public int getFriendOnlineCount(){
return this.friendOnlineCount;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -