replydao.java

来自「java带进度条上传尽量不要让站长把时间都花费在为您修正说明上」· Java 代码 · 共 265 行

JAVA
265
字号
package com.jmwl.dao;

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

import com.jmwl.common.BlogException;
import com.jmwl.dto.UserReplyDTO;
import com.jmwl.vo.Re_UserVO;
import com.jmwl.vo.ReplyVO;

public class ReplyDAO extends BasicDAO
{
	/**
	 * 添加一条回复
	 * @param info
	 * @return
	 * @throws BlogException
	 */
	public boolean add_reply(UserReplyDTO info) throws BlogException
	{
		
		boolean b=false;
			String sql="insert into reply(content,pub_time,user_id,not_id) values(?,?,?,?)";
			
			try {
				PreparedStatement ps=this.getConn().prepareStatement(sql);
				ps.setString(1, info.getCountent());
				ps.setString(2,info.getPub_time());
				ps.setInt(3, info.getUser_id());
				ps.setInt(4, info.getNote_id());
				int i=ps.executeUpdate();
				if(i==1)
				{
					b=true;
				}
			} catch (SQLException e) {
				e.printStackTrace();
				throw new BlogException("对不起,添加回复时的SQL语句错误");
			}
			
		return b;
	}
	/**
	 * 作者对用户回复的回复
	 * @param id
	 * @param content
	 * @param time
	 * @return
	 * @throws BlogException
	 */
	public boolean add_hostreply(int id,String content,String time) throws BlogException
	{
		boolean b=false;
		String sql="update reply set host_answer=?,host_time=? where id=?";
		try {
			PreparedStatement ps=this.getConn().prepareStatement(sql);
			ps.setString(1, content);
			ps.setString(2, time);
			ps.setInt(3, id);
			int i=ps.executeUpdate();
			if(i==1)
			{
				b=true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new BlogException("对不起,添加回复时的SQL语句错误");
		}
		return b;	
	}
	/**
	 * 显示一篇日志的回复
	 * @param id
	 * @return
	 * @throws BlogException
	 */
	public List<ReplyVO> get_replyinfo(int id,int currpage) throws BlogException
	{
		List<ReplyVO> list=new ArrayList();
		String sql="select * from reply where not_id=? limit "+(currpage-1)*8+",8";
		try {
			PreparedStatement ps=this.getConn().prepareStatement(sql);
			ps.setInt(1, id);
			ResultSet rs=ps.executeQuery();
			while(rs.next())
			{
				ReplyVO info=new ReplyVO();
				info.setId(rs.getInt(1));
				info.setContent(rs.getString(2));
				info.setPub_time(rs.getString(3));
				info.setUser_id(rs.getInt(4));
				info.setNote_id(rs.getInt(5));
				info.setHost_answer(rs.getString(6));
				info.setHost_time(rs.getString(7));
				list.add(info);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new BlogException("对不起,显示回复时的SQL语句错误");
		}
		return list;
	}
	/**
	 * 日志回复的数量
	 * @param id
	 * @return
	 * @throws BlogException 
	 */
	public int get_reply_cs(int id) throws BlogException
	{
		int i=0;
		System.out.println("id:"+id);
		String sql="select count(*) from reply where not_id=?";
		try {
			PreparedStatement ps=this.getConn().prepareStatement(sql);
			ps.setInt(1, id);
			ResultSet rs=ps.executeQuery();
			while(rs.next())
			{
				i=rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new BlogException("对不起,显示回复数量的SQL语句错误");
		}
		return i;
	}
	/**
	 * 得到某一日志的回复页数
	 * @param id
	 * @return
	 * @throws BlogException
	 */
	public int gethfpage(int id) throws BlogException
	{
		int n=0;
		int pagesize=8;
		int countinfo=get_reply_cs(id);
		if(countinfo==0)
		{
			return 1;
		}
		if(countinfo%pagesize==0)
		{
			n=countinfo/pagesize;
		}
		else
		{
			n=countinfo/pagesize+1;
		}
		return n;
	}
	/**
	 * 得到一个回复用户的简单信息
	 * @param id
	 * @return
	 * @throws BlogException
	 */
	public Re_UserVO get_user(int id) throws BlogException
	{
		Re_UserVO info=new Re_UserVO();
		String sql="select userslogin.uId,userslogin.face,userslogin.nickName,usersinfo.sex,usersinfo.birthday,userslogin.counter from userslogin,usersinfo where usersinfo.uId=? and usersinfo.uId=userslogin.uId";
		try {
			PreparedStatement ps=this.getConn().prepareStatement(sql);
			ps.setInt(1, id);
			ResultSet rs=ps.executeQuery();
			while(rs.next())
			{
				info.setUId(rs.getInt(1));
				info.setFace(rs.getString(2));
				info.setNickName(rs.getString(3));
				info.setSex(rs.getInt(4));
				info.setBirthday(rs.getTimestamp(5));
				info.setCounter(rs.getInt(6));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new BlogException("对不起,得到简单用户信息的SQL语句错误");
		}
		
		return info;
		
	}
	/**
	 * 屏蔽一个用户的回复
	 * @param id
	 * @return
	 * @throws BlogException
	 */
	public boolean ReErrors(int id) throws BlogException
	{
		boolean b=false;
		String sql="update reply set content='该用户的回复已经被管理员屏蔽' where id=?";
		PreparedStatement ps;
		try {
			ps = this.getConn().prepareStatement(sql);
			ps.setInt(1, id);
			int i=ps.executeUpdate();
			if(i==1)
			{
				b=true;
			}
		} catch (SQLException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new BlogException("对不起,屏蔽回复内容的SQL语句错误");
		}
		
		return b;
	}
	/**
	 * 删除一篇日志的所有回复
	 * @param id
	 * @return
	 * @throws BlogException 
	 */
	public boolean delete_reply(int id) throws BlogException
	{
		boolean b=false;
		int n=0;
		String str="select count(*) from reply where not_id=?";
		try {
			PreparedStatement ps=this.getConn().prepareStatement(str);
			ps.setInt(1, id);
			ResultSet rs=ps.executeQuery();
			while(rs.next())
			{
				n=rs.getInt(1);
			}
			if(n==0)
			{
				return true;
			}
			
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String sql="delete from reply where not_id=?";
		try {
			PreparedStatement ps2=this.getConn().prepareStatement(sql);
			ps2.setInt(1, id);
			int i=ps2.executeUpdate();
			if(i!=0)
			{
				b=true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new BlogException("对不起,删除回复内容的SQL语句错误");
		}
		
		return b;
	}
	
}

⌨️ 快捷键说明

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