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

📄 dbuseronline.java

📁 天乙代码src_531.rar 天乙代码src_531.rar 天乙代码src_531.rar 天乙代码src_531.rar
💻 JAVA
字号:
package com.laoer.bbscs.bbs.business;

import java.util.List;
import com.laoer.bbscs.db.TranContext;
import com.laoer.bbscs.exception.ObjectException;
import com.laoer.bbscs.exception.ObjectNoExistException;
import com.laoer.bbscs.db.DBInf;
import java.sql.*;
import com.laoer.bbscs.sysinfo.*;
import com.laoer.bbscs.util.*;
import org.apache.log4j.*;

/**
 * <p>Title: 天乙社区V5.0</p>
 * <p>Description: BBS-CS天乙社区V5.0</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: laoer.com</p>
 * @author 龚天乙
 * @version 5.0
 */

public class DBUserOnline
    extends UserOnline
    implements DBInf {

  static Logger logger = Logger.getLogger(DBUserOnline.class.getName());

  ResultSet rs = null;
  static final String LOAD_USER = "select * from useronline where UserName = ?";
  static final String INSERT_USER = "insert into useronline (ID,UserName,NickName,OnlineTime,IsGuest) values (?,?,?,?,?)";
  static final String UPDATE_USER =
      "update useronline set NickName=?,OnlineTime = ?,IsGuest = ? where UserName = ?";
  static final String LOAD_ONLINEUSER =
      "select count(*) as total from useronline where OnlineTime > ? and IsGuest = 0";
  static final String LOAD_ONLINEGUEST =
      "select count(*) as total from useronline where OnlineTime > ? and IsGuest = 1";
  static final String LOAD_ONLINELIST =
      "select * from useronline where OnlineTime > ? and IsGuest = 0";
  static final String DEL_OUTTIMEGUEST =
      "delete from useronline where OnlineTime < ? and IsGuest = 1";

  public DBUserOnline() {
  }

  public int getFriendOnlineNum() {
    int fonlinenum = 0;
    long atime = Util.getaLongTime() - 180000;
    String SQL = "";
    TranContext DBSQL = new TranContext();
    try {
      //DBSQL = new TranContext();
      if (DBSQL == null) {
        return 0;
      }
      if (this.userFriend == null || this.userFriend.length() == 0) {
        return 0;
      }
      if (this.userFriend.endsWith(",")) {
        this.userFriend = this.userFriend.substring(0,
            this.userFriend.length() - 1);
      }
      SQL = "select count(*) as total from useronline where ID in (" +
          this.userFriend + ") and OnlineTime > ? and IsGuest = 0";
      DBSQL.prepareStatement(SQL);
      DBSQL.setLong(1, atime);
      rs = DBSQL.executeQuery();
      if (rs.next()) {
        fonlinenum = rs.getInt("total");
      }
      DBSQL.close();
      return fonlinenum;
    }
    catch (Exception e) {
      logger.error(e);
      return 0;
    }
    finally {
      try {
        if (rs != null) {
          rs.close();
        }
      }
      catch (SQLException e) {
      }
      DBSQL.freeCon();
    }

  }

  public List getFriendOnlineList(TranContext aTranContext) {
    TranContext myDBTrans = null;
    if (aTranContext == null) {
      myDBTrans = new TranContext();
    }
    else {
      myDBTrans = aTranContext;
    }
    try {
      List alist = null;
      alist = this.loadDBs(myDBTrans, "Friend");
      return alist;
    }
    catch (ObjectException e) {
      logger.error(e);
      return (List) Sys.RESULT_NULL;
    }
    finally {
      if (aTranContext == null && myDBTrans != null) {
        myDBTrans.freeCon();
      }
    }
  }

  public int setOnlineUser() {
    boolean isHaveUser = false;
    TranContext myDB = new TranContext();
    try {
      this.loadDB(myDB, "UserName", false);
      isHaveUser = true;
    }
    catch (ObjectException e) {
      return Sys.RESULT_OBJECTEXCEPTION;
    }
    catch (ObjectNoExistException e) {
      isHaveUser = false;
    }
    try {
      if (isHaveUser) {
        this.updateDB(myDB, "UserName");
      }
      else {
        this.insertDB(myDB);
      }
      return Sys.RESULT_RIGHT;
    }
    catch (ObjectException e) {
      logger.error(e);
      return Sys.RESULT_OBJECTEXCEPTION;
    }
    finally {
      myDB.freeCon();
    }

  }

  public int[] getOnlineNum() {
    int[] onlineNum = {
        0, 0};
    TranContext myDB = new TranContext();
    long atime = Util.getaLongTime() - 180000;
    try {
      myDB.prepareStatement(LOAD_ONLINEUSER);
      myDB.setLong(1, atime);
      rs = myDB.executeQuery();
      if (rs.next()) {
        onlineNum[0] = rs.getInt("total");
      }
      rs.close();
      myDB.close();
      myDB.prepareStatement(LOAD_ONLINEGUEST);
      myDB.setLong(1, atime);
      rs = myDB.executeQuery();
      if (rs.next()) {
        onlineNum[1] = rs.getInt("total");
      }
      rs.close();
      myDB.close();
      return onlineNum;
    }
    catch (SQLException e) {
      logger.error(e);
      return onlineNum;
    }
    finally {
      try {
        if (rs != null) {
          rs.close();
          rs = null;
        }
      }
      catch (SQLException e) {
      }
      try {
        if (myDB != null) {
          myDB.close();
        }
      }
      catch (SQLException e) {
      }
      myDB.freeCon();
    }

  }

  public List getOnlineList(TranContext aTranContext) {
    TranContext myDBTrans = null;
    if (aTranContext == null) {
      myDBTrans = new TranContext();
    }
    else {
      myDBTrans = aTranContext;
    }
    try {
      List alist = null;
      alist = this.loadDBs(myDBTrans, "Online");
      return alist;
    }
    catch (ObjectException e) {
      logger.error(e);
      return (List) Sys.RESULT_NULL;
    }
    finally {
      if (aTranContext == null && myDBTrans != null) {
        myDBTrans.freeCon();
      }
    }
  }

  public int delOnlineUser(TranContext aTranContext, String key) {
    TranContext myDBTrans = null;
    if (aTranContext == null) {
      myDBTrans = new TranContext();
    }
    else {
      myDBTrans = aTranContext;
    }
    try {
      this.delDB(myDBTrans, key);
      return Sys.RESULT_RIGHT;
    }
    catch (ObjectException e) {
      logger.error(e);
      return Sys.RESULT_OBJECTEXCEPTION;
    }
    finally {
      if (aTranContext == null && myDBTrans != null) {
        myDBTrans.freeCon();
      }
    }
  }

  public void loadDB(TranContext myDB, String key, boolean isLoad) throws
      ObjectException, ObjectNoExistException {
    try {
      if (key.equals("UserName")) {
        myDB.prepareStatement(LOAD_USER);
        myDB.setString(1, this.myUserOnlineInfo.getUserName());
        rs = myDB.executeQuery();
      }
      if (rs.next()) {
        if (isLoad) {
          this.myUserOnlineInfo.setID(rs.getLong("ID"));
          this.myUserOnlineInfo.setUserName(rs.getString("UserName"));
          this.myUserOnlineInfo.setNickName(rs.getString("NickName"));
          this.myUserOnlineInfo.setOnlineTime(rs.getLong("OnlineTime"));
          this.myUserOnlineInfo.setIsGuest(rs.getInt("IsGuest"));
        }
      }
      else {
        throw new ObjectNoExistException();
      }
    }
    catch (SQLException e) {
      throw new ObjectException(e.toString());
    }
    finally {
      try {
        if (rs != null) {
          rs.close();
          rs = null;
        }
      }
      catch (SQLException e) {
      }
      try {
        if (myDB != null) {
          myDB.close();
        }
      }
      catch (SQLException e) {
      }
    }
  }

  public List loadDBs(TranContext myDB, String key) throws ObjectException {
    long atime = Util.getaLongTime() - 180000;
    List alist = ListFactory.getInstance(2);
    try {
      if (key.equals("Online")) {
        myDB.prepareStatement(LOAD_ONLINELIST);
        myDB.setLong(1, atime);
        rs = myDB.executeQuery();
      }
      if (key.equals("Friend")) {
        if (this.userFriend == null || this.userFriend.length() == 0) {
          return alist;
        }
        if (this.userFriend.endsWith(",")) {
          this.userFriend = this.userFriend.substring(0,
              this.userFriend.length() - 1);
        }
        String SQL = "select * from useronline where ID in (" + this.userFriend +
            ") and OnlineTime > ? and IsGuest = 0";
        myDB.prepareStatement(SQL);
        myDB.setLong(1, atime);
        rs = myDB.executeQuery();

      }
      while (rs.next()) {
        UserOnlineInfo aUserOnlineInfo = new UserOnlineInfo();
        aUserOnlineInfo.setID(rs.getLong("ID"));
        aUserOnlineInfo.setUserName(rs.getString("UserName"));
        aUserOnlineInfo.setNickName(rs.getString("NickName"));
        aUserOnlineInfo.setOnlineTime(rs.getLong("OnlineTime"));
        aUserOnlineInfo.setIsGuest(rs.getInt("IsGuest"));
        alist.add(aUserOnlineInfo);
        aUserOnlineInfo = null;
      }
      return alist;
    }
    catch (SQLException e) {
      throw new ObjectException(e.toString());
    }
    finally {
      try {
        if (rs != null) {
          rs.close();
          rs = null;
        }
      }
      catch (SQLException e) {
      }
      try {
        if (myDB != null) {
          myDB.close();
        }
      }
      catch (SQLException e) {
      }
    }
  }

  public void insertDB(TranContext myDB) throws ObjectException {
    try {
      myDB.prepareStatement(INSERT_USER);
      myDB.setLong(1, this.myUserOnlineInfo.getID());
      myDB.setString(2, this.myUserOnlineInfo.getUserName());
      myDB.setString(3, this.myUserOnlineInfo.getNickName());
      myDB.setLong(4, Util.getaLongTime());
      myDB.setInt(5, this.myUserOnlineInfo.getIsGuest());
      myDB.executeUpdate();
    }
    catch (SQLException e) {
      throw new ObjectException(e.toString());
    }
    finally {
      try {
        if (myDB != null) {
          myDB.close();
        }
      }
      catch (SQLException e) {
      }
    }
  }

  public void updateDB(TranContext myDB, String key) throws ObjectException {
    try {
      if (key.equals("UserName")) {
        myDB.prepareStatement(UPDATE_USER);
        myDB.setString(1, this.myUserOnlineInfo.getNickName());
        myDB.setLong(2, Util.getaLongTime());
        myDB.setInt(3, this.myUserOnlineInfo.getIsGuest());
        myDB.setString(4, this.myUserOnlineInfo.getUserName());
        myDB.executeUpdate();
      }
    }
    catch (SQLException e) {
      throw new ObjectException(e.toString());
    }
    finally {
      try {
        if (myDB != null) {
          myDB.close();
        }
      }
      catch (SQLException e) {
      }
    }
  }

  public void delDB(TranContext myDB, String key) throws ObjectException {
    try {
      if (key.equals("delOuttime")) {
        long atime = Util.getaLongTime() - 180000;
        myDB.prepareStatement(DEL_OUTTIMEGUEST);
        myDB.setLong(1, atime);
        myDB.executeUpdate();
      }
    }
    catch (SQLException e) {
      throw new ObjectException(e.toString());
    }
    finally {
      try {
        if (myDB != null) {
          myDB.close();
        }
      }
      catch (SQLException e) {
      }
    }
  }

}

⌨️ 快捷键说明

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