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

📄 dbfriend.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.*;
import com.laoer.bbscs.exception.ObjectException;
import com.laoer.bbscs.exception.ObjectNoExistException;
import java.sql.*;
import com.laoer.bbscs.util.*;
import com.laoer.bbscs.sysinfo.*;
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 DBFriend
    extends Friend
    implements DBInf {

  ResultSet rs = null;
  String SQL = "";
  static Logger logger = Logger.getLogger(DBFriend.class.getName());

  public DBFriend() {
  }

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

  public int getFriend(TranContext aTranContext, String key, boolean isLoad) {
    TranContext myDBTrans = null;
    if (aTranContext == null) {
      myDBTrans = new TranContext();
    }
    else {
      myDBTrans = aTranContext;
    }
    try {
      this.loadDB(myDBTrans, key, false);
      return Sys.RESULT_RIGHT;
    }
    catch (ObjectException e) {
      logger.error(e);
      return Sys.RESULT_OBJECTEXCEPTION;
    }
    catch (ObjectNoExistException e) {
      return Sys.RESULT_OBJECTNOEXISTEXCEPTION;
    }
    finally {
      if (aTranContext == null && myDBTrans != null) {
        myDBTrans.freeCon();
      }
    }

  }

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

  public int createFriend(TranContext aTranContext) {
    if ( (this.myUser.getUserInfo().getFriend() + this.myFriendInfo.getFID() +
          ",").length() > 2000) {
      return Sys.RESULT_OTHER;
    }
    TranContext myDBTrans = null;
    if (aTranContext == null) {
      myDBTrans = new TranContext();
    }
    else {
      myDBTrans = aTranContext;
    }
    try {
      this.insertDB(myDBTrans);
      this.updateUserFriendStr(myDBTrans);
      return Sys.RESULT_RIGHT;
    }
    catch (ObjectException e) {
      logger.error(e);
      return Sys.RESULT_OBJECTEXCEPTION;
    }
    finally {
      if (aTranContext == null && myDBTrans != null) {
        myDBTrans.freeCon();
      }
    }
  }

  public int getFriendNum(TranContext aTranContext) {
    TranContext myDBTrans = null;
    if (aTranContext == null) {
      myDBTrans = new TranContext();
    }
    else {
      myDBTrans = aTranContext;
    }
    try {
      int total = 0;
      SQL = "select count(*) as total from " + this.myFriendInfo.getTablename() +
          " where UserID = ?";
      myDBTrans.prepareStatement(SQL);
      myDBTrans.setLong(1, this.myFriendInfo.getUserID());
      rs = myDBTrans.executeQuery();
      if (rs.next()) {
        total = rs.getInt("total");
      }
      return total;
    }
    catch (SQLException e) {
      logger.error(e);
      return Sys.RESULT_ZERO;
    }
    finally {
      try {
        if (rs != null) {
          rs.close();
        }
      }
      catch (SQLException e) {
      }
      try {
        if (myDBTrans != null) {
          myDBTrans.close();
        }
      }
      catch (SQLException e) {
      }
      if (aTranContext == null && myDBTrans != null) {
        myDBTrans.freeCon();
      }
    }
  }

  public int updateUserFriendStr(TranContext aTranContext) {
    List alist = null;
    StringBuffer friendstr = new StringBuffer();
    FriendInfo aFriendInfo = null;
    TranContext myDBTrans = null;
    if (aTranContext == null) {
      myDBTrans = new TranContext();
    }
    else {
      myDBTrans = aTranContext;
    }
    try {
      alist = loadDBs(myDBTrans, "All");
      int alistlen = alist.size();
      for (int i = 0; i < alistlen; i++) {
        aFriendInfo = (FriendInfo) alist.get(i);
        friendstr.append(aFriendInfo.getFID() + ",");
      }
      alist = null;
      UserInfo aUserInfo = this.myUser.getUserInfo();
      aUserInfo.setFriend(friendstr.toString());
      this.myUser.setUserInfo(aUserInfo);
      this.myUser.updateUser(myDBTrans, "friend");
      return Sys.RESULT_OTHER;
    }
    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("reFname")) {
        SQL = "select * from " + this.myFriendInfo.getTablename() +
            " where Fname = ? and UserID = ?";
        myDB.prepareStatement(SQL);
        myDB.setString(1, this.myFriendInfo.getFname());
        myDB.setLong(2, this.myFriendInfo.getUserID());
        rs = myDB.executeQuery();
      }
      if (rs.next()) {

      }
      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 {
    List alist = ListFactory.getInstance(2);
    FriendInfo aFriendInfo = null;
    try {
      if (key.equals("All")) {
        SQL = "select * from " + this.myFriendInfo.getTablename() +
            " where UserID = ?";
        myDB.prepareStatement(SQL);
        myDB.setLong(1, this.myFriendInfo.getUserID());
        rs = myDB.executeQuery();
      }
      if (key.equals("myFriendList")) {
        String SQLStr = "select * from " + this.myFriendInfo.getTablename() +
            " where UserID = " + this.myFriendInfo.getUserID();
        this.pages.setTotals(this.getFriendNum(myDB));
        this.pages.doPageBreak();
        this.sListPageBreak = this.pages.getListPageBreak();
        this.pages.setSQL(SQLStr);
        rs = this.pages.getRs(myDB, "ID");
      }
      while (rs.next()) {
        aFriendInfo = new FriendInfo();
        aFriendInfo.setID(rs.getString("ID"));
        aFriendInfo.setUserID(rs.getLong("UserID"));
        aFriendInfo.setUname(rs.getString("Uname"));
        aFriendInfo.setFID(rs.getLong("FID"));
        aFriendInfo.setFname(rs.getString("Fname"));
        aFriendInfo.setComment(rs.getString("Fcomment"));
        alist.add(aFriendInfo);
        aFriendInfo = 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 {
      SQL = "insert into " + this.myFriendInfo.getTablename() +
          " (ID,UserID,Uname,FID,Fname,Fcomment) values (?,?,?,?,?,?)";
      myDB.prepareStatement(SQL);
      myDB.setString(1,
                     String.valueOf(Util.getaLongTime()) +
                     String.valueOf(this.myFriendInfo.getUserID()));
      myDB.setLong(2, this.myFriendInfo.getUserID());
      myDB.setString(3, this.myFriendInfo.getUname());
      myDB.setLong(4, this.myFriendInfo.getFID());
      myDB.setString(5, this.myFriendInfo.getFname());
      myDB.setString(6, this.myFriendInfo.getComment());
      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 {
  }

  public void delDB(TranContext myDB, String key) throws ObjectException {
    try {
      if (key.equals("del")) {
        SQL = "delete from " + this.myFriendInfo.getTablename() +
            " where ID = ? and UserID = ?";
        myDB.prepareStatement(SQL);
        myDB.setString(1, this.myFriendInfo.getID());
        myDB.setLong(2, this.myFriendInfo.getUserID());
        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 + -