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

📄 replydaoimpl.java

📁 一个bbs论坛系统
💻 JAVA
字号:
package com.lovo.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.lovo.factory.DAOFactory;
import com.lovo.po.PublishPO;
import com.lovo.po.ReplyPO;
import com.lovo.po.UserPO;
import com.lovo.util.DBUtil;
import com.lovo.util.Page;

public class ReplyDAOImpl implements ReplyDAO {
	
	private Connection con = null;

	private PreparedStatement st = null;

	private ResultSet rs = null;

	/**
	 * 添加回复贴
	 * @param replyPO
	 * @throws SQLException
	 */
	public void insert(ReplyPO replyPO) throws SQLException {
		String sql = "insert into reply(publish_id,user_id,content,replyDate)" +
				" values(?, ?, ?, date_format(now(),'%Y-%m-%d %H:%i:%s'))";
		try {
			con = DBUtil.getDBUtil().getConnection();
			st = con.prepareStatement(sql);
			st.setInt(1, replyPO.getPublish().getId());
			st.setInt(2, replyPO.getReplyMan().getId());
			st.setString(3, replyPO.getContent());
			
			st.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		} finally{
			DBUtil.getDBUtil().close(st);
			DBUtil.getDBUtil().close(con);
		}
		
	}
	
	/**
	 * 删除回复贴
	 * @param id
	 * @throws SQLException
	 */
	public void delete(int id) throws SQLException {
		String sql = "delete from reply where id = ?"; 
		try {
			con = DBUtil.getDBUtil().getConnection();
			//得到语句对象
			st = con.prepareStatement(sql);
			st.setInt(1, id);
			st.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		} finally{
			DBUtil.getDBUtil().close(st);
			DBUtil.getDBUtil().close(con);
		}
	}
	
	/**
	 * 查询某个用户的所有帖子
	 * @param id
	 * @return
	 * @throws SQLException
	 */
	public List<ReplyPO> queryByUserId(int id) throws SQLException {
		List<ReplyPO> list = new ArrayList<ReplyPO> ();
		String sql = "select * from reply where user_id = ?";
		try{
			con = DBUtil.getDBUtil().getConnection();
			st = con.prepareStatement(sql);
			st.setInt(1, id);
			rs = st.executeQuery();
			while(rs.next()){
				ReplyPO replyPO = new ReplyPO();
				this.initReply(rs, replyPO);
				list.add(replyPO);
			}
		}catch(SQLException e){
			e.printStackTrace();
			throw e;
		}
		return list;
	}
	
	/**
	 * 查询某个主题的所有恢复帖子
	 * @param id
	 * @return
	 * @throws SQLException
	 */
	public List<ReplyPO> queryByPublish(int id, Page page) throws SQLException {  
		List<ReplyPO> list = new ArrayList<ReplyPO> ();
		String sql = "select * from reply where publish_id = ? limit ?, ?";
		try{
			con = DBUtil.getDBUtil().getConnection();
			st = con.prepareStatement(sql);
			st.setInt(1, id);
			st.setInt(2, (page.getCurrPage() - 1) * page.getPageRows());
			st.setInt(3, page.getPageRows());
			rs = st.executeQuery();
			while(rs.next()){
				ReplyPO replyPO = new ReplyPO();
				
				this.initReply(rs, replyPO);
				
				list.add(replyPO);
			}
		}catch(SQLException e){
			e.printStackTrace();
			throw e;
		}
		return list;
	}
	
	public List<ReplyPO> queryByContent(String content, Page page) throws SQLException {  
		List<ReplyPO> list = new ArrayList<ReplyPO> ();
		String sql = "select * from reply where content like ? limit ?, ?";
		try{
			con = DBUtil.getDBUtil().getConnection();
			st = con.prepareStatement(sql);
			st.setString(1, content);
			st.setInt(2, (page.getCurrPage() - 1) * page.getPageRows());
			st.setInt(3, page.getPageRows());
			rs = st.executeQuery();
			while(rs.next()){
				ReplyPO replyPO = new ReplyPO();
				
				this.initReply(rs, replyPO);
				
				list.add(replyPO);
			}
		}catch(SQLException e){
			e.printStackTrace();
			throw e;
		}
		return list;
	}
	/**
	 * 根据ID具体查询某个回复贴
	 * @param id
	 * @return
	 * @throws SQLException
	 */
	public ReplyPO queryById(int id) throws SQLException {
		String sql = "select * from reply where id = ?";
		ReplyPO replyPO = null;
		try{
			con = DBUtil.getDBUtil().getConnection();
			st = con.prepareStatement(sql);
			st.setInt(1, id);
			rs = st.executeQuery();
			while(rs.next()){
				replyPO = new ReplyPO();
				this.initReply(rs, replyPO);
			}
		}catch(SQLException e){
			e.printStackTrace();
			throw e;
		}
		return replyPO;
	}
	
	public int queryReplyNumByPublish(int id) throws SQLException {
		String sql = "select count(*) maxrows from reply where publish_id = ?";
		int maxRows = 0;
		try{
			con = DBUtil.getDBUtil().getConnection();
			st = con.prepareStatement(sql);
			st.setInt(1, id);
			rs = st.executeQuery();
			while(rs.next()){
				maxRows = rs.getInt("maxrows");
			}
		}catch(SQLException e){
			e.printStackTrace();
			throw e;
		}
		return maxRows;
	}
	
	public void initReply(ResultSet rs, ReplyPO replyPO) throws SQLException {
		PublishPO publish = new PublishPO();
		UserPO replyMan = new UserPO();
		replyMan.setId(rs.getInt("user_id"));
		publish.setId(rs.getInt("publish_id"));
		replyPO.setId(rs.getInt("id"));
		replyPO.setContent(rs.getString("content"));
		replyPO.setPublish(publish);
		replyPO.setReplyDate(rs.getString("replydate"));
		replyPO.setReplyMan(replyMan);
	}

	
}

⌨️ 快捷键说明

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