首页>代码>Android开发小巫新闻客户端>/服务端源代码/web/src/com/szy/web/dao/NewsDAO.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.News;


/**
 *@author coolszy
 *@date Feb 19, 2012
 *@blog http://blog.92coding.com
 */
public class NewsDAO
{
	SqlManager manager;
	String sql = "";
	ResultSet rs;

	public NewsDAO() throws IOException, ClassNotFoundException
	{
		manager = SqlManager.createInstance();
	}

	/**
	 * 获取新闻内容
	 * @param nid 新闻编号
	 * @return
	 * @throws SQLException
	 */
	public News getNews(int nid) throws SQLException
	{
		sql = "SELECT nid,cid,title,body,source,ptime,imgsrc FROM t_news WHERE nid=? and deleted=false";
		Object[] params = new Object[]{ nid };
		manager.connectDB();
		rs = manager.executeQuery(sql, params);
		News news = new News();
		if (rs.next())
		{
			news.setNid(rs.getInt("nid"));
			news.setCid(rs.getInt("cid"));
			news.setTitle(rs.getString("title"));
			news.setBody(rs.getString("body"));
			news.setSource(rs.getString("source"));
			news.setPtime(rs.getString("ptime"));
			news.setImgSrc(rs.getString("imgsrc"));
		}
		manager.closeDB();
		return news;
	}

	/**
	 * 获取指定类别的新闻列表
	 * @param tid 新闻类型
	 * @param startNid 起始编号
	 * @param count 返回数量
	 * @return
	 * @throws SQLException
	 */
	public ArrayList<News> getSpecifyCategoryNews(int tid, int startNid, int count)
			throws SQLException
	{
		ArrayList<News> list = new ArrayList<News>();
		sql = "SELECT nid,cid,title,digest,source,ptime,imgsrc FROM t_news WHERE cid=? AND deleted=false ORDER BY ptime desc LIMIT ?,?";
		Object[] params = new Object[]{ tid, startNid, count };
		manager.connectDB();
		rs = manager.executeQuery(sql, params);
		while (rs.next())
		{
			News news = new News();
			news.setNid(rs.getInt("nid"));
			news.setCid(rs.getInt("cid"));
			news.setTitle(rs.getString("title"));
			news.setDigest(rs.getString("digest"));
			news.setSource(rs.getString("source"));
			news.setPtime(rs.getString("ptime"));
			news.setImgSrc(rs.getString("imgsrc"));
			list.add(news);
		}
		manager.closeDB();
		return list;
	}
}
最近下载更多
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日
最近浏览更多
appppp  LV1 2024年12月21日
chenranr  LV10 2024年12月20日
652654393  LV5 2024年11月18日
wwwqwq 2024年6月21日
暂无贡献等级
AIWU1234  LV2 2024年6月9日
今晚都别睡啦  LV9 2024年5月29日
求学的熊猫  LV11 2024年5月6日
微信网友_6522007290040320  LV2 2024年4月15日
77777七  LV1 2024年2月26日
不止是六位数  LV7 2023年12月21日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友