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

📄 dbguestbook.java

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

import com.laoer.bbscs.db.*;
import java.util.List;
import com.laoer.bbscs.exception.ObjectException;
import com.laoer.bbscs.exception.ObjectNoExistException;
import com.laoer.bbscs.util.*;
import java.sql.*;
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 DBGuestBook
    extends GuestBook
    implements DBInf {

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

  public DBGuestBook() {
  }

  public int getGuestBook(TranContext aTranContext, String key, boolean isLoad) {
    TranContext myDBTrans = null;
    if (aTranContext == null) {
      myDBTrans = new TranContext();
    }
    else {
      myDBTrans = aTranContext;
    }
    try {
      this.loadDB(myDBTrans, key, isLoad);
      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 delGuestBook(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 List getGuestBookList(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 updateGuestBook(TranContext aTranContext, String key) {
    TranContext myDBTrans = null;
    if (aTranContext == null) {
      myDBTrans = new TranContext();
    }
    else {
      myDBTrans = aTranContext;
    }
    try {
      this.updateDB(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 int getGuestBookNum(TranContext aTranContext) {
    TranContext myDBTrans = null;
    if (aTranContext == null) {
      myDBTrans = new TranContext();
    }
    else {
      myDBTrans = aTranContext;
    }
    try {
      int total = 0;
      String strSQL = "select count(*) as total from " +
          this.myGuestBookInfo.getTablename() + " where UserID = ?";
      myDBTrans.prepareStatement(strSQL);
      myDBTrans.setLong(1, this.myGuestBookInfo.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 getGuestBookNewNum(TranContext aTranContext) {
    TranContext myDBTrans = null;
    if (aTranContext == null) {
      myDBTrans = new TranContext();
    }
    else {
      myDBTrans = aTranContext;
    }
    try {
      int total = 0;
      String strSQL = "select count(*) as total from " +
          this.myGuestBookInfo.getTablename() + " where UserID = ? and IsNew = 0";
      myDBTrans.prepareStatement(strSQL);
      myDBTrans.setLong(1, this.myGuestBookInfo.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 createGuestBook(TranContext aTranContext) {
    TranContext myDBTrans = null;
    if (aTranContext == null) {
      myDBTrans = new TranContext();
    }
    else {
      myDBTrans = aTranContext;
    }
    try {
      this.insertDB(myDBTrans);
      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("ID")) {
        SQL = "select * from " + this.myGuestBookInfo.getTablename() +
            " where ID= ? and UserID = ?";
        myDB.prepareStatement(SQL);
        myDB.setString(1, this.myGuestBookInfo.getID());
        myDB.setLong(2, this.myGuestBookInfo.getUserID());
      }
      rs = myDB.executeQuery();
      if (rs.next()) {
        if (isLoad) {
          this.myGuestBookInfo.setID(rs.getString("ID"));
          this.myGuestBookInfo.setUserID(rs.getLong("UserID"));
          this.myGuestBookInfo.setUname(rs.getString("Uname"));
          this.myGuestBookInfo.setFID(rs.getLong("FID"));
          this.myGuestBookInfo.setFname(rs.getString("Fname"));
          this.myGuestBookInfo.setMtype(rs.getInt("Mtype"));
          this.myGuestBookInfo.setNote(rs.getString("Note"));
          this.myGuestBookInfo.setStime(rs.getLong("Stime"));
          this.myGuestBookInfo.setIsNew(rs.getInt("IsNew"));
        }
      }
      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);
    GuestBookInfo aGuestBookInfo = null;
    try {
      if (key.equals("myGuestBookList")) {
        String SQLStr = "select * from " + this.myGuestBookInfo.getTablename() +
            " where UserID = " + this.myGuestBookInfo.getUserID() +
            " order by Stime desc";
        this.pages.setTotals(this.getGuestBookNum(myDB));
        this.pages.doPageBreak();
        this.sListPageBreak = this.pages.getListPageBreak();
        this.pages.setSQL(SQLStr);
        rs = this.pages.getRs(myDB, "ID");
      }
      while (rs.next()) {
        aGuestBookInfo = new GuestBookInfo();
        aGuestBookInfo.setID(rs.getString("ID"));
        aGuestBookInfo.setUserID(rs.getLong("UserID"));
        aGuestBookInfo.setUname(rs.getString("Uname"));
        aGuestBookInfo.setFID(rs.getLong("FID"));
        aGuestBookInfo.setFname(rs.getString("Fname"));
        aGuestBookInfo.setMtype(rs.getInt("Mtype"));
        aGuestBookInfo.setNote(rs.getString("Note"));
        aGuestBookInfo.setStime(rs.getLong("Stime"));
        aGuestBookInfo.setIsNew(rs.getInt("IsNew"));
        alist.add(aGuestBookInfo);
      }
      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 {
    String tb = "guestbook_" + Util.getTableID(this.myGuestBookInfo.getFname());
    try {
      SQL = "insert into " + tb +
          " (ID,UserID,Uname,FID,Fname,Mtype,Note,Stime,IsNew) values (?,?,?,?,?,?,?,?,?)";
      myDB.prepareStatement(SQL);
      myDB.setString(1,
                     String.valueOf(Util.getaLongTime()) +
                     String.valueOf(this.myGuestBookInfo.getFID()));
      myDB.setLong(2, this.myGuestBookInfo.getFID());
      myDB.setString(3, this.myGuestBookInfo.getFname());
      myDB.setLong(4, this.myGuestBookInfo.getUserID());
      myDB.setString(5, this.myGuestBookInfo.getUname());
      myDB.setInt(6, this.myGuestBookInfo.getMtype());
      myDB.setString(7, this.myGuestBookInfo.getNote());
      myDB.setLong(8, Util.getaLongTime());
      myDB.setInt(9, 0);
      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("readed")) {
        SQL = "update " + this.myGuestBookInfo.getTablename() +
            " set IsNew = 1 where ID = ? and UserID = ?";
        myDB.prepareStatement(SQL);
        myDB.setString(1, this.myGuestBookInfo.getID());
        myDB.setLong(2, this.myGuestBookInfo.getUserID());
        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("dela")) {
        SQL = "delete from " + this.myGuestBookInfo.getTablename() +
            " where ID = ? and UserID = ?";
        myDB.prepareStatement(SQL);
        myDB.setString(1, myGuestBookInfo.getID());
        myDB.setLong(2, this.myGuestBookInfo.getUserID());
        myDB.executeUpdate();
      }
      if (key.equals("dels")) {
        SQL = "delete from " + this.myGuestBookInfo.getTablename() +
            " where ID in (" + this.getDelGuestBookIDs() + ") and UserID = ?";
        myDB.prepareStatement(SQL);
        myDB.setLong(1, this.myGuestBookInfo.getUserID());
        myDB.executeUpdate();
      }
      if (key.equals("delAll")) {
        SQL = "delete from " + this.myGuestBookInfo.getTablename() +
            " where UserID = ?";
        myDB.prepareStatement(SQL);
        myDB.setLong(1, this.myGuestBookInfo.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 + -