0
点赞
收藏
分享

微信扫一扫

Jweb 简化代码(封装)

静悠 2022-04-13 阅读 44

前面我们写了一整个新闻项目,里面有 用户登录,用户注册及新闻数据的维护(增,删,改查),我们所写的这个项目核心的文件的后缀名为 jsp,里面包含了很多代码,看着呢蛮复杂。

用形象的话来比喻说:我们有同一个衣柜,里面放了衣服,裤子,鞋子,袜子,围巾,帽子等等,很多,也比较杂乱,找起来也特别不方便,而现在呢,我们决定分类简化(封装),把衣服,裤子放在一个大的抽屉里,为衣裤间,围巾和帽子放在另一个中间的抽屉,为戴饰间,等 ,合理分配。

在这个项目里,我们也来了一波简化(封装),在JavaResources的src 下有三个包,分别为:entity:实体类:对象

util:帮助类:连接数据库的一套操作

dao:方法体:写方法 !!!

admin.jsp :运行的界面。

我们就以新闻admin界面模糊查询举例:

admin.jsp模糊查询

1.DBHelper( util包 帮助类)

package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import oracle.jdbc.driver.OracleDriver;

public class DBHelper {
	
	private static String user="scott";
	private static String upwd="tiger";
	private static String cname="oracle.jdbc.driver.OracleDriver";
	private static String url="jdbc:oracle:thin:@localhost:1521:orcl";
	static {
		try {
			Class.forName(cname);
			
		}catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
	}
	public static Connection getCon() {
		Connection con=null;
		try {
			con=DriverManager.getConnection(url,user,upwd);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return con;
	}
	public static void closeDb(Connection con,PreparedStatement ps,ResultSet rs) {
		try {
			if(con!=null) {
				con.close();
			}
			if(ps!=null) {
				con.close();
			}
			if(rs!=null) {
				con.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
	}
	public static int getNext(String tablename,String col) {
		int id=1;
		Connection con=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			con=DBHelper.getCon();
			ps=con.prepareStatement("select max("+col+") from +tablename");
			
			rs=ps.executeQuery();
			if(rs.next()) {
				id=rs.getInt(1)+1;
		
			}
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		return id;
	}
	
}

2.News(entity包)新闻实体类

package entity;

import java.sql.Date;

public class News {
	private int nid;
	private int tid;
	private String ntitle;
	private String nzz;
	private String ncr;
	
	private String look;
	private String nzy;
	private String npic;
	private Date ndate;
	
	
	public News(int nid, int tid, String ntitle, String nzz, String ncr, String look, String nzy, String npic) {
		super();
		this.nid = nid;
		this.tid = tid;
		this.ntitle = ntitle;
		this.nzz = nzz;
		this.ncr = ncr;
		this.look = look;
		this.nzy = nzy;
		this.npic = npic;
	}
	public int getNid() {
		return nid;
	}
	public void setNid(int nid) {
		this.nid = nid;
	}
	public int getTid() {
		return tid;
	}
	public void setTid(int tid) {
		this.tid = tid;
	}
	public String getNtitle() {
		return ntitle;
	}
	public void setNtitle(String ntitle) {
		this.ntitle = ntitle;
	}
	public String getNzz() {
		return nzz;
	}
	public void setNzz(String nzz) {
		this.nzz = nzz;
	}
	public String getNcr() {
		return ncr;
	}
	public void setNcr(String ncr) {
		this.ncr = ncr;
	}
	public Date getNdate() {
		return ndate;
	}
	public void setNdate(Date ndate) {
		this.ndate = ndate;
	}
	public String getLook() {
		return look;
	}
	public void setLook(String look) {
		this.look = look;
	}
	public String getNzy() {
		return nzy;
	}
	public void setNzy(String nzy) {
		this.nzy = nzy;
	}
	public String getNpic() {
		return npic;
	}
	public void setNpic(String npic) {
		this.npic = npic;
	}
	public News(int nid, int tid, String ntitle, String nzz, String ncr, Date ndate, String look, String nzy,
			String npic) {
		super();
		this.nid = nid;
		this.tid = tid;
		this.ntitle = ntitle;
		this.nzz = nzz;
		this.ncr = ncr;
		this.ndate = ndate;
		this.look = look;
		this.nzy = nzy;
		this.npic = npic;
	}
	public News() {
		super();
	}
	public News(int tid, String ntitle, String nzz, String ncr, Date ndate, String look, String nzy, String npic) {
		super();
		this.tid = tid;
		this.ntitle = ntitle;
		this.nzz = nzz;
		this.ncr = ncr;
		this.ndate = ndate;
		this.look = look;
		this.nzy = nzy;
		this.npic = npic;
	}
	@Override
	public String toString() {
		return "News [nid=" + nid + ", tid=" + tid + ", ntitle=" + ntitle + ", nzz=" + nzz + ", ncr=" + ncr + ", ndate="
				+ ndate + ", look=" + look + ", nzy=" + nzy + ", npic=" + npic + "]";
	}
	
	
	


}

里面的内容就不用过多解释了哈

3.NewsDao(dao包)功能:

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 entity.Subject;
import util.DBHelper;

public class NewsDao {
//添加方法
	public  static int add(News news ) {
		News n=null;
		int i=0;
		Connection con=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			con=DBHelper.getCon();
			ps=con.prepareStatement("insert into newb(newsid,newstid,newstitle,newszz,newcontent,newsdate,newslook) values(?,?,?,?,?,sysdate,?)");
			ps.setInt(1, DBHelper.getNext("newsid","newsb"));
			ps.setInt(2,news.getTid());
			ps.setString(3, news.getNtitle());
			ps.setString(4, news.getNzz());
			ps.setString(5, news.getNcr());
			ps.setDate(6,news.getNdate());
			ps.setString(7, news.getLook());
			i=ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
				
		
		
		return i;
	}
//删除
	public  static int del(int id ) {
		
		int i=0;
		Connection con=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			con=DBHelper.getCon();
			ps=con.prepareStatement("delete from newsb where newsid="+id);
			i=ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
				
		
		
		return i;
	}
//修改	
	
	public  static int upd(int id,News news ) {
		News n=null;
		int i=0;
		Connection con=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			con=DBHelper.getCon();
			ps=con.prepareStatement("update newsb set newstid=?, newstitle=?,newszz=?,newszy=? newscontent=? where newsid="+id);
			ps.setInt(1, news.getTid());
			ps.setString(2, news.getNtitle());
			ps.setString(3, news.getNzz());
			ps.setString(4, news.getNzy());
			ps.setString(5,news.getNcr());
			
			i=ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
				
		
		
		return i;
	}
	//分页查询
	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();
			ps=con.prepareStatement("select * from(select a.*,rownum mid from newsb a)b where mid>=? and mid<=?");
			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 tit=rs.getString(3);
				String tit1=rs.getString(4);
				String tit2=rs.getString(5);
				String tit3=rs.getString(6);
				String tit4=rs.getString(7);
				String tit6=rs.getString(8);

				News ne=new News(nid, tid, tit, tit1, tit2, tit3, tit4, tit6);
				nlist.add(ne);
				
			}
			
		
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		
		
		
		return nlist;
	}
//获得最大页码	
	public static int getMaxpage(int pageSize) {
	Connection con=null;
	PreparedStatement ps=null;
	ResultSet rs=null;
	int maxPage=0;
	int count=0;
	try {
		
		con=DBHelper.getCon();
		ps=con.prepareStatement("select count(*) from newsb");
		rs=ps.executeQuery();
		if(rs.next()) {
			count=rs.getInt(1);
			maxPage=count/pageSize;
			if(count%pageSize!=0) {
				maxPage++;
			}
		}
	} catch (Exception e) {
		e.printStackTrace();
		// TODO: handle exception
	}
	
		
		
		return maxPage;
	}
//分页模糊查询	
	public ArrayList<News> pageNewsLike(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();
			ps=con.prepareStatement("select * from(select a.*,rownum mid from newsb a where newstitle like '%"+str+"%')b where mid>=? and mid<=?");
			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 tit=rs.getString(3);
				String tit1=rs.getString(4);
				String tit2=rs.getString(5);
				String tit3=rs.getString(6);
				String tit4=rs.getString(7);
				String tit6=rs.getString(8);

				News ne=new News(nid, tid, tit, tit1, tit2, tit3, tit4, tit6);
				nlist.add(ne);
				
			}
			
		
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		
		
		
		return nlist;
	}
//获得分页模糊查询的最大气页码	
	public static int getMaxpageLike(int pageSize,String str) {
		Connection con=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		int maxPage=0;
		int count=0;
		try {
			
			con=DBHelper.getCon();
			ps=con.prepareStatement("select count(*) from newsb where newstitle like '%"+str+"%' ");
			rs=ps.executeQuery();
			if(rs.next()) {
				count=rs.getInt(1);
				maxPage=count/pageSize;
				if(count%pageSize!=0) {
					maxPage++;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		
			
			
			return maxPage;
		}
	
	
}

4.admin.jsp界面

是不是封装起来代码整齐且简单呢??老有味道了!!!

举报

相关推荐

0 条评论