Jweb 简化代码(封装)

前面我们写了一整个新闻项目,里面有 用户登录,用户注册及新闻数据的维护(增,删,改查),我们所写的这个项目核心的文件的后缀名为 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界面

<%@page import="entity.users"%>
<%@page import="dao.NewsDao"%>
<%@page import="entity.News"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.PreparedStatement"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>管理员后台</title>
<link href="CSS/admin.css" rel="stylesheet" type="text/css" />

</head>
<body>
<%
  /*Object ob=session.getAttribute("u");

	if(ob==null){
		out.print("<script>alert('您还未登录,请您登录');location.href='login.jsp'</script>");
	}*/
%>
<div id="header">
  <div id="welcome">欢迎使用新闻管理系统!</div>
  <div id="nav">
    <div id="logo"><img src="images/logo.jpg" alt="新闻中国" /></div>
    <div id="a_b01"><img src="images/a_b01.gif" alt="" /></div>
  </div>
</div>
<div id="admin_bar">
  <div id="status">管理员:  &#160;&#160;&#160;&#160; <a href="login.jsp">login out</a></div>
  <div id="channel"> </div>
</div>
<div id="main">
  <div id="opt_list">
    <ul>
      <li><a href="add_news.jsp">添加新闻</a></li>
      <li><a href="upd_news.jsp">编辑新闻</a></li>
      <li><a href="add_sub.jsp">添加主题</a></li>
      <li><a href="upd_sub.jsp">编辑主题</a></li>
    </ul>
  </div>
  <div id="opt_area">
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <script language="javascript">
	function clickdel(){
		return confirm("删除请点击确认");
	}
	
</script>
    <form action="admin.jsp" align="center">
		<input type="text" name="str">
		<input type="submit" value="查询">
	</form>
  <ul class="classlist"> 
      <%
      String str=request.getParameter("str");
     
       if(str==null){
        str="";       
    }
      
     
		int pageIndex = 1;
		int pageSize = 5;

		String index = request.getParameter("pageIndex");
		if(index!=null){//如果接收到页面,就给页面pageIndex赋值
			pageIndex = Integer.valueOf(index);
		}
		
		
		int maxPage=new NewsDao().getMaxpageLike(pageSize, str);
	ArrayList<News> nlist=new NewsDao().pageNewsLike(pageIndex, pageSize,str);
	for(News s:nlist){
		
			
	%>
 <li>
 	<a href="read_news.jsp?nid=<%=s.getNid()%>">
 		<%=s.getNtitle() %>
 	</a>
 <span> 作者:
   <%=s.getNzz() %>                                             
   &#160;&#160;&#160;&#160; <a href='upd_news.jsp?nid=<%=s.getNid()%>'>修改</a> &#160;&#160;&#160;&#160; 
   <a href='dodel.jsp?nid=<%=s.getNid() %>' onclick='return clickdel()'>删除</a> </span> </li>
 <li class='space'></li>
 <%} %>
 <p align="right"> 
 				<a href="admin.jsp">首页</a> 
 		<a href="admin.jsp?pageIndex=<%=pageIndex>1?pageIndex-1:1%><%
 				if(str!=null){
 					out.print("&str="+str);
 				}
 				%>">上一页</a>
 		
 		
 		&nbsp;当前页数:[<%=pageIndex %>/<%=maxPage %>]&nbsp; 
 		<a href="admin.jsp?pageIndex=<%=pageIndex<maxPage?pageIndex+1:maxPage%><%
      				if(str!=null){
      					out.print("&str="+str);
      				}%>">下一页</a>
      	      		
      			
 		<a href="admin.jsp?pageIndex=<%=maxPage%>">末页</a> 

  </p>
</ul>
  </div>
</div>
<div id="site_link"> <a href="#">关于我们</a><span>|</span> <a href="#">Aboue Us</a><span>|</span> <a href="#">联系我们</a><span>|</span> <a href="#">广告服务</a><span>|</span> <a href="#">供稿服务</a><span>|</span> <a href="#">法律声明</a><span>|</span> <a href="#">招聘信息</a><span>|</span> <a href="#">网站地图</a><span>|</span> <a href="#">留言反馈</a> </div>
<div id="footer">
  <p class="">24小时客户服务热线:010-68988888  &#160;&#160;&#160;&#160; <a href="#">常见问题解答</a> &#160;&#160;&#160;&#160;  新闻热线:010-627488888<br />
    文明办网文明上网举报电话:010-627488888  &#160;&#160;&#160;&#160;  举报邮箱:<a href="#">jubao@jb-aptech.com.cn</a></p>
  <p class="copyright">Copyright &copy; 1999-2009 News China gov, All Right Reserver<br />
    新闻中国   版权所有</p>
</div>
</body>
</body>
</html>

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

可以直接用的,数据库的内容设置好就ok呢。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值