首页>代码>Android开发小巫新闻客户端>/服务端源代码/web/src/com/szy/web/dao/CommentDAO.java
package com.szy.web.dao;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.szy.web.model.Comment;

/**
 *@author coolszy
 *@date Feb 23, 2012
 *@blog http://blog.92coding.com
 */
public class CommentDAO
{
	SqlManager manager;
	String sql = "";
	ResultSet rs;
	
	public CommentDAO() throws IOException, ClassNotFoundException
	{
		manager = SqlManager.createInstance();
	}
	
	/**
	 * 获取回复信息
	 * @param nid 新闻编号
	 * @param startRid 起始ID
	 * @param count 返回数量
	 * @return
	 * @throws SQLException
	 */
	public ArrayList<Comment> getComments(int nid,int startCid,int count) throws SQLException
	{
		ArrayList<Comment> list = new ArrayList<Comment>();
		//sql = "select cid,nid,ptime,region,content,supportcount,opposecount from t_comment where nid=? and deleted=false order by ptime desc limit ?,?";
		sql = "SELECT * FROM t_comment WHERE nid=? AND deleted=false order by ptime desc";
		Object[] params = new Object[]
		{ nid };
		manager.connectDB();
		rs = manager.executeQuery(sql, params);
		while (rs.next())
		{
			Comment comment = new Comment();
			comment.setCid(rs.getInt("cid"));
			comment.setNid(rs.getInt("nid"));
			comment.setPtime(rs.getString("ptime"));
			comment.setRegion(rs.getString("region"));
			comment.setContent(rs.getString("content"));
			comment.setSupportCount(rs.getInt("supportcount"));
			comment.setOpposeCount(rs.getInt("opposecount"));
			list.add(comment);
		}
		manager.closeDB();
		return list;
	}
	
	/**
	 * 保存新评论
	 * @param nid 新闻编号
	 * @param ptime 发表时间
	 * @param region 地区
	 * @param content 内容
	 * @throws SQLException
	 */
	public void addComment(int nid,String ptime,String region,String content) throws SQLException
	{
		sql = "INSERT INTO t_comment (nid,ptime,region,content) VALUES (?,?,?,?)";
		Object[] params = new Object[] { nid, ptime, region,content };
		manager.connectDB();
		manager.executeUpdate(sql, params);
		manager.closeDB();
	}
	
	/**
	 * 获取指定新闻评论数量
	 * @param nid
	 * @return
	 * @throws SQLException
	 */
	public long getSpecifyNewsCommentsCount(int nid) throws SQLException
	{
		long count = 0;
		sql = "select count(cid) as count from t_comment where nid=?";
		Object[] params = new Object[]{ nid };
		manager.connectDB();
		rs = manager.executeQuery(sql, params);
		if (rs.next())
		{
			count = rs.getLong("count");
		}
		return count;
	}
}
最近下载更多
yyyyyyzh  LV8 2023年6月11日
qjg12345  LV1 2022年10月25日
_nrxsh  LV12 2022年10月22日
萌了个乖乖  LV12 2022年5月21日
511076827  LV33 2022年4月10日
刘嘉晨11111  LV7 2022年3月31日
hedu53  LV1 2022年2月7日
RunGe946  LV3 2021年12月27日
凡先生  LV19 2021年12月19日
jkysll  LV7 2021年11月14日
最近浏览更多
求学的熊猫  LV6 前天
77777七  LV1 2月26日
不止是六位数  LV7 2023年12月21日
学习112  LV2 2023年12月21日
黄楚云  LV1 2023年12月20日
LITIANYU084414  LV11 2023年11月28日
hhhhhz  LV7 2023年11月14日
tianshi  LV7 2023年10月19日
陈小小222 2023年7月15日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友