首页>代码>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);
		}
	}
	
	

}
最近下载更多
asddwh  LV12 2023年12月29日
19542347615  LV1 2023年11月23日
无名氏111  LV32 2023年11月18日
lwlwlwlw  LV3 2023年6月17日
傻瓜还没打死烦人  LV7 2022年12月6日
zhunishimian  LV6 2022年10月30日
SQ2930501923  LV14 2022年10月16日
CHENHAOJUDA  LV10 2022年6月15日
小丶无奈  LV10 2022年6月13日
dasdascccf  LV10 2022年6月10日
最近浏览更多
13543528515  LV8 4月28日
wjh007  LV4 2月29日
GerryGim  LV4 1月15日
Liang朝伟  LV1 1月5日
1345fwwhjb 1月4日
暂无贡献等级
13161895  LV1 2023年12月30日
hxy19991216  LV4 2023年12月29日
asddwh  LV12 2023年12月26日
yuanshun  LV6 2023年12月19日
洪徽宏 2023年12月15日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友