新闻发布项目—代码封装

封装代码的目的是为了方便,以及减少代码量。更好的利于开发和调用。


流程:

1.在项目下找到Java Resources,在src中创建三个包,entity(实体类),dao(方法类),util(连接数据库类)。在entity中创建实体对象,dao中编写方法,util中创建DBhelper(连接数据库用)。

2.在dao类中的java文件下写方法

举例:以新闻数据操作为例3s

package dao;

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

import entity.News;
import util.DBHelper;

public class NewsDao {
	//添加新闻
	/**
	 * 添加新闻
	 * @param news 要添加的新闻对象
	 * @return 成功返回1,失败返回0
	 */
	public int addNews(News news) {
		int i = 0;
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = DBHelper.getCon();
			ps = con.prepareStatement("insert into news(nid,tid,ntitle,nzz,ncontent,nzy,ndate,nlook) values(?,?,?,?,?,?,sysdate,0)");
			ps.setInt(1, DBHelper.getNextId("news", "nid"));
			
			ps.setInt(2, news.getTid());
			ps.setString(3, news.getNtitle());
			ps.setString(4, news.getNzz());
			ps.setString(5, news.getNnr());
			ps.setString(6, news.getNzy());
			i = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBHelper.closeDb(con, ps, null);
		}
		return i;
	}
	//删除新闻
	/**
	 * 删除新闻
	 *  @param nid 要删除的新闻编号
	 * @return 成功返回1,失败返回0
	 */
	public int delete(int nid) {
		int i = 0;
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = DBHelper.getCon();
			ps = con.prepareStatement("delete news where nid="+nid);
			i = ps.executeUpdate();
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBHelper.closeDb(con, ps, null);
		}
		return i;
	}
	
	//修改新闻
	/**
	 * 修改新闻
	 * @param nid 要修改的 新闻编号
	 * @param news 修改后的新闻
	 * @return 成功返回1,失败返回0
	 */
	public int upNews(int nid,News news) {
		int i = 0;
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = DBHelper.getCon();
			ps = con.prepareStatement("update news set tid=?,ntitle=?,nzz=?,ncontent=?,nzy=? where nid="+nid);
			ps.setInt(1, news.getTid());
			ps.setString(2, news.getNtitle());
			ps.setString(3, news.getNzz());
			ps.setString(4, news.getNnr());
			ps.setString(5, news.getNzy());
			i = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBHelper.closeDb(con, ps, null);
		}
		return i;
	}
	
	
	/**
	 * 根据新闻编号查询新闻
	 * @param nid
	 * @return
	 */
	public News getByNid(int nid){
		Connection con=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		News ne=null;
		try {
			con=DBHelper.getCon();
			ps=con.prepareStatement("select * from news where nid="+nid);
			rs=ps.executeQuery();
			while(rs.next()) {
				ne=new News(rs.getInt(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(8));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBHelper.closeDb(con, ps, rs);
		}
		return ne;
	}
	
	
	//分页查询:
	/**
	 * 分页查询
	 * @param pageIndex 页码
	 * @param pageSize 每页数据条数
	 * @return 范湖查询到的集合
	 */
	public ArrayList<News> pageNews(int pageIndex,int pageSize){
		ArrayList<News> nlist = new ArrayList<>();
		int start = (pageIndex-1)*pageSize+1;
		int end = pageIndex*pageSize;
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			con = DBHelper.getCon();
			String sql = "select * from(select a.*,rownum mid from news a)b where mid>=? and mid<=?";
			ps = con.prepareStatement(sql);
			ps.setInt(1, start);
			ps.setInt(2, end);
			
			rs = ps.executeQuery();
			while(rs.next()) {
				int nid = rs.getInt(1);
				int tid = rs.getInt(2);
				String nzz = rs.getString(4);
				String ntitle = rs.getString(3);
				String nnr = rs.getString(5);
				Date ndate = rs.getDate(6);
				int nlook = rs.getInt(7);
				String nzy = rs.getString(8);
				String nimage = rs.getString(9);
				
				//实例化对象
				News news = new News(nid, tid, ntitle, nzz, nnr, nzy, ndate, nlook, nimage);
				//把新闻对象放到集合中
				nlist.add(news);
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBHelper.closeDb(con, ps, rs);
		}
		
		return nlist;
	}
	
	/**
	 * 分页模糊查询
	 * @param pageIndex
	 * @param pageSize
	 * @param str
	 * @return
	 */
	public ArrayList<News> pageLikeNews(int pageIndex,int pageSize,String str){
		ArrayList<News> nlist = new ArrayList<>();
		int start = (pageIndex-1)*pageSize+1;
		int end = pageIndex*pageSize;
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			con = DBHelper.getCon();
			String sql = "select * from(select a.*,rownum mid from news a where ntitle like '%"+str+"%')b where mid>=? and mid<=?";
			ps = con.prepareStatement(sql);
			ps.setInt(1, start);
			ps.setInt(2, end);
			
			rs = ps.executeQuery();
			while(rs.next()) {
				int nid = rs.getInt(1);
				int tid = rs.getInt(2);
				String nzz = rs.getString(4);
				String ntitle = rs.getString(3);
				String nnr = rs.getString(5);
				Date ndate = rs.getDate(6);
				int nlook = rs.getInt(7);
				String nzy = rs.getString(8);
				String nimage = rs.getString(9);
				
				//实例化对象
				News news = new News(nid, tid, ntitle, nzz, nnr, nzy, ndate, nlook, nimage);
				//把新闻对象放到集合中
				nlist.add(news);
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBHelper.closeDb(con, ps, rs);
		}
		
		return nlist;
	}
	
}

3.实例化方法类,然后进行方法的调用就可以,效果和前几次的效果是一样的 

一定要清楚自己需要的字段,进行字段设计,在实体类中封装方法 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值