⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 newsdaoimpl.java

📁 企业宣传网站后台
💻 JAVA
字号:
package com.westaccp.jsp.company.dao.impl;

import java.util.ArrayList;
import java.util.List;

import com.westaccp.jsp.company.dao.NewsDao;
import com.westaccp.jsp.company.model.News;
import java.sql.*;

public class NewsDaoImpl extends BaseDao implements NewsDao {
	
	private Connection con = null;
	private PreparedStatement pstmt = null;
	private ResultSet rs = null;

	public int addNews(News news) {
		int rowCount = 0;
		String sql = "insert into tabNews values(?, ?, ?)";
		
		try {
			con = this.getCon();
			pstmt = con.prepareCall(sql);
			pstmt.setString(1, news.getTitle());
			pstmt.setString(2, news.getContent());
			pstmt.setString(3, news.getWriteDate());
			rowCount = pstmt.executeUpdate();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			this.closeDB(null, pstmt, con);
		}
		
		return rowCount;
	}

	public int deleteNews(int newsId) {
		int rowCount = 0;
		String sql = "delete from tabNews where newsId = ?";
		
		try {
			con = this.getCon();
			pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, newsId);
			rowCount = pstmt.executeUpdate();
		} catch (Exception ex) {
			ex.printStackTrace();
 		} finally {
 			this.closeDB(null, pstmt, con);
 		}
 		
 		return rowCount;
	}

	public List findListNews() {
		List newsList = null;
		String sql = "select * from tabNews";
		
		try {
			con = this.getCon();
			pstmt = con.prepareStatement(sql);
			rs = pstmt.executeQuery();
			newsList = new ArrayList();
			while(rs.next()) {
				News news = new News();
				news.setNewsId(rs.getInt("newsId"));
				news.setTitle(rs.getString("title"));
				news.setContent(rs.getString("content"));
				news.setWriteDate(rs.getString("writeDate"));
				newsList.add(news);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			this.closeDB(rs, pstmt, con);
		}
		
		return newsList;
	}

	public News findNews(int newsId) {
		// TODO Auto-generated method stub
		return null;
	}

	public int updateNews(News news) {
		// TODO Auto-generated method stub
		return 0;
	}

	public List findListNews(int top) {
		List newsList = null;
		String sql = "select top " + top + " * from tabNews order by newsId desc";
		try {
			con = this.getCon();
			pstmt = con.prepareStatement(sql);
			rs = pstmt.executeQuery();
			newsList = new ArrayList();
			while(rs.next()) {
				News news = new News();
				news.setNewsId(rs.getInt("newsId"));
				news.setTitle(rs.getString("title"));
				news.setContent(rs.getString("content"));
				news.setWriteDate(rs.getString("writeDate"));
				newsList.add(news);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			this.closeDB(rs, pstmt, con);
		}
		
		return newsList;
	}
}

⌨️ 快捷键说明

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