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

📄 postdao.java

📁 用java实现的一个论坛,可以供大家参考
💻 JAVA
字号:
/*******************************************************************\
*                                                                   *
*			   LightningBoard			    *
*                                                                   *
*          http://sourceforge.net/projects/lightningboard/          *
*			                                            *
*	            Copyright (c) 2002 Xiaobo Liu	            *
*                                                                   *
*********************************************************************
*                        LICENSE INFORMATION                        *
*                                                                   *
*  LightningBoard is free software; you can redistribute it and/or  *
*  modify it under the terms of the GNU General Public License as   *
*  published by the Free Software Foundation; either version 2 of   *
*  the License, or any later version.                               *
*                                                                   *
*  We don't charge anything for the use of LightningBoard, we only  *
*  require you to keep the copyright present on your site and in    *
*  the source files.                                                *
*                                                                   *
*  LightningBoard is distributed in the hope that it will be useful,*
*  but without any warranty; without even the implied warranty of   *
*  merchantability or fitness for a particular purpose. See the GNU *
*  General Public License for more details.                         *
*                                                                   *
\*******************************************************************/

package liuxiaobo.lb.dao;

import java.sql.*;
import java.util.*;
import liuxiaobo.db.*;
import liuxiaobo.util.*;
import liuxiaobo.lb.*;
import liuxiaobo.lb.bean.*;

public class PostDAO  {
  private DBConnectionManager dbcm = DBConnectionManager.getInstance();

  public int post(int f_id,int u_id, String subject,String content,String u_lastip)throws ActionException{
    Connection connection=null;
    try {
      String dataTime=DateTimeUtil.getDateTime();
      connection=dbcm.getConnection();
      DBAccess dba=new DBAccess(connection);
      connection.setAutoCommit(false);
      //1
      CommonDAO idGen=new CommonDAO();
      int  t_id = idGen.getTopicID(dba);
      String sql = "INSERT INTO topic(t_id,t_f_id,t_name,t_u_id,t_replies,t_views,t_lasttime,t_u_id2)"
                 +"VALUES ("+t_id+","+f_id+",'"+subject+"',"+u_id+",0,0,'"+dataTime+"',"+u_id+")";
      dba.runSql(sql);
      //2
      int m_id=idGen.getMessageID(dba);
      sql="INSERT INTO message"
         +"(m_id,m_t_id,m_content,m_u_id,m_time)"
         +" VALUES ("+m_id+","+t_id+",'"+content+"',"+u_id+",'"+dataTime+"')";
      dba.runSql(sql);
      //3
      dba.runSql("UPDATE user SET u_post=u_post+1,u_lasttime='"+dataTime+"',u_lastip='"+u_lastip+"' WHERE u_id="+u_id);
      //4
      dba.runSql("UPDATE forum SET f_topics=f_topics+1,f_messages=f_messages+1,f_u_id2="+u_id+",f_lasttime='"+dataTime+"' WHERE f_id="+f_id);

      connection.commit();
      return t_id;
    } catch (Exception ex) {
      try {
        connection.rollback();
      }
      catch (SQLException sqle) {
      }
      throw new ActionException(ex);
    }finally{
      try {
        connection.setAutoCommit(true);
        dbcm.freeConnection(connection);
      } catch (SQLException ex) {
        //
      }
    }
  }

  public void reply(int t_id,int u_id, String content,String u_lastip)throws ActionException{
    Connection connection=null;
    try {
      String dataTime=DateTimeUtil.getDateTime();
      connection=dbcm.getConnection();
      connection.setAutoCommit(false);
      DBAccess dba=new DBAccess(connection);
      //1
      String sql ="UPDATE topic SET t_replies=t_replies+1,"
                 +" t_lasttime='"+dataTime+"',t_u_id2="+u_id
                 +" WHERE t_id="+t_id;
      dba.runSql(sql);
      //2
      CommonDAO idGen=new CommonDAO();
      int m_id=idGen.getMessageID(dba);
      sql="INSERT INTO message (m_id,m_t_id,m_content,m_u_id,m_time)"
         +" VALUES ("+m_id+","+t_id+",'"+content+"',"+u_id+",'"+dataTime+"')";
      dba.runSql(sql);
      //3
      dba.runSql("UPDATE user SET u_post=u_post+1,u_lasttime='"+dataTime+"',u_lastip='"+u_lastip+"' WHERE u_id="+u_id);
     //4
      int f_id=0;
      ResultSet rs=dba.openSelect("SELECT t_f_id FROM topic WHERE t_id="+t_id);
      while (rs.next())
        f_id=rs.getInt("t_f_id");
      rs.close();
      dba.closeSelect();
      dba.runSql("UPDATE forum SET f_messages=f_messages+1,f_u_id2="+u_id+",f_lasttime='"+dataTime+"' WHERE f_id="+f_id);
      connection.commit();
    } catch (Exception ex) {
      try {
        connection.rollback();
      }
      catch (SQLException sqle) {
      }

      throw new ActionException(ex);
    }finally{
      try {
        connection.setAutoCommit(true);
        dbcm.freeConnection(connection);
      } catch (SQLException ex) {
        //
      }
    }
  }
}

⌨️ 快捷键说明

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