首页>代码>jsp开发新闻发布系统>/News/src/cn/edu/qust/news/dao/NewsDao.java
package cn.edu.qust.news.dao;

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

import cn.edu.qust.news.dao.NewsDao;
import cn.edu.qust.news.domain.News;
import cn.edu.qust.news.utils.JdbcUtil;

public class NewsDao {

	public void addNews(News news) {
		if (news == null)
			throw new IllegalArgumentException();
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			conn = JdbcUtil.getConnection();
			stmt = conn
					.prepareStatement("insert into t_news(f_title,f_content,f_date,f_type) values(?,?,?,?)");
			stmt.setString(1, news.getTitle());
			stmt.setString(2, news.getContent());
			stmt.setDate(3, new java.sql.Date(news.getDate().getTime()));
			stmt.setString(4, news.getType());
			stmt.executeUpdate();
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			JdbcUtil.release(rs, stmt, conn);
		}
	}

	public void updateNews(News news) {

		if (news == null)
			throw new IllegalArgumentException();
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			conn = JdbcUtil.getConnection();
			stmt = conn
					.prepareStatement("update t_news set f_title=?,f_content=?,f_date=?,f_type=? where f_id=?");
			stmt.setString(1, news.getTitle());
			stmt.setString(2, news.getContent());
			stmt.setDate(3, new java.sql.Date(news.getDate().getTime()));
			stmt.setString(4, news.getType());
			stmt.setInt(5, news.getId());
			stmt.executeUpdate();
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			JdbcUtil.release(rs, stmt, conn);
		}
	}

	
	public void deleteNewsById(Integer newsId) {

		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			conn = JdbcUtil.getConnection();
			stmt = conn.prepareStatement("delete from t_news where f_id=?");
			stmt.setInt(1, newsId);
			stmt.executeUpdate();
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			JdbcUtil.release(rs, stmt, conn);
		}
	}
	
	
	public List<News> findAll() {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		List<News> lNews = new ArrayList<News>();
		try {
			conn = JdbcUtil.getConnection();
			stmt = conn
					.prepareStatement("select f_id,f_title,f_content,f_date,f_type from t_news order by f_id DESC");
			rs = stmt.executeQuery();
			while (rs.next()) {
				News news = new News();
				news.setId(rs.getInt("f_id"));
				news.setTitle(rs.getString("f_title"));
				news.setContent(rs.getString("f_content"));
				news.setDate(rs.getDate("f_date"));
				news.setType(rs.getString("f_type"));
				lNews.add(news);
			} 
			return lNews;
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			JdbcUtil.release(rs, stmt, conn);
		}
		
	}

	
	public News findNewsById(Integer newsId) {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			conn = JdbcUtil.getConnection();
			stmt = conn
					.prepareStatement("select f_id,f_title,f_content,f_date,f_type from t_news where f_id=?");
			stmt.setInt(1, newsId);
			rs = stmt.executeQuery();
			if (rs.next()) {
				News news = new News();
				news.setId(rs.getInt("f_id"));
				news.setTitle(rs.getString("f_title"));
				news.setContent(rs.getString("f_content"));
				news.setDate(rs.getDate("f_date"));
				news.setType(rs.getString("f_type"));
				return news;
			} else
				return null;
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			JdbcUtil.release(rs, stmt, conn);
		}
	}

	
	public List<News> findNewsByType(String type) {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		List<News> lNews = new ArrayList<News>();
		try {
			conn = JdbcUtil.getConnection();
			stmt = conn
					.prepareStatement("select f_id,f_title,f_content,f_date,f_type from t_news where f_type=? order by f_id DESC");
			stmt.setString(1, type);
			rs = stmt.executeQuery();
			while (rs.next()) {
				News news = new News();
				news.setId(rs.getInt("f_id"));
				news.setTitle(rs.getString("f_title"));
				news.setContent(rs.getString("f_content"));
				news.setDate(rs.getDate("f_date"));
				news.setType(rs.getString("f_type"));
				lNews.add(news);
			}
			return lNews;
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			JdbcUtil.release(rs, stmt, conn);
		}
	}

	
	public int getTotalRecords() {
		 Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try{
			conn = JdbcUtil.getConnection();
			stmt = conn.prepareStatement("select count(*) from t_news");
			rs = stmt.executeQuery();
			if(rs.next()){
				return rs.getInt(1);
			}else
				return 0;
		}catch(Exception e){
			throw new RuntimeException(e);
		}finally{
			JdbcUtil.release(rs, stmt, conn);
		}
	}

	
	public List<News> findPageRecords(int startIndex, int pagesize) {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		List<News> lNews = new ArrayList<News>();
		try {
			conn = JdbcUtil.getConnection();
			stmt = conn
					.prepareStatement("select f_id,f_title,f_content,f_date,f_type from t_news order by f_id DESC limit ?,?");
			stmt.setInt(1, startIndex);
			stmt.setInt(2, pagesize);
			rs = stmt.executeQuery();
			while (rs.next()) {
				News news = new News();
				news.setId(rs.getInt("f_id"));
				news.setTitle(rs.getString("f_title"));
				news.setContent(rs.getString("f_content"));
				news.setDate(rs.getDate("f_date"));
				news.setType(rs.getString("f_type"));
				lNews.add(news);
			} 
			return lNews;
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			JdbcUtil.release(rs, stmt, conn);
		}
	}
	
	

}
最近下载更多
张泽帅  LV6 2024年10月28日
hao98382  LV2 2024年10月22日
bankroll  LV5 2024年6月13日
asddwh  LV13 2023年12月29日
19542347615  LV1 2023年11月23日
无名氏111  LV33 2023年11月18日
lwlwlwlw  LV3 2023年6月17日
傻瓜还没打死烦人  LV7 2022年12月6日
zhunishimian  LV6 2022年10月30日
SQ2930501923  LV14 2022年10月16日
最近浏览更多
潜心小白来到  LV3 6月10日
krispeng  LV15 5月29日
f22m1a2b2  LV17 1月21日
233123 2024年12月20日
暂无贡献等级
ma406805131  LV19 2024年12月18日
qwqwx585160  LV2 2024年12月15日
traume 2024年12月11日
暂无贡献等级
沉着冷静 2024年11月25日
暂无贡献等级
张泽帅  LV6 2024年10月28日
微信网友_7222576034680832  LV2 2024年10月23日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友