javaBean封装

                                为了代码的整洁,所以用javaBean封装,以把java和jsp分离。

以下是我封装的类

封装类:

DBHelper类:链接数据库,因为Oracle我没有添加触发器,所以在类中写了getNextId函数,使它自增

        代码:

package com.zking.util;

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

public class DBHelper {
	
	private static String user="scott";
	private static String uwpd="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();
		}
	}
	
	public static Connection getCon() {
		Connection con=null;
		try {
			con=DriverManager.getConnection(url, user, uwpd);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return con;
	}
	
	public static void closeDB(Connection con,PreparedStatement ps,ResultSet rs) {
		try {
			if(con!=null) {
				con.close();
			}
			if(ps!=null) {
				ps.close();
			}
			if(rs!=null) {
				rs.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static int getNextId(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();
		}finally {
			DBHelper.closeDB(con, ps, rs);
		}
		return id;
		
	}
}

News类: 新闻实体类

         

package com.zking.entity;

import java.sql.Date;

public class News {

	private int  nid;
	private int tid;
	private String ntitle;
	private String nzz;
	private String ncontent;
	private Date ndate;
	private int nlook;
	private String nzy;
	private String ntp;
	
	
	
	public News(int tid, String ntitle, String nzz, String ncontent, String nzy) {
		super();
		this.tid = tid;
		this.ntitle = ntitle;
		this.nzz = nzz;
		this.ncontent = ncontent;
		this.nzy = nzy;
	}
	public News(int nid, int tid, String ntitle, String nzz, String ncontent, int nlook, String nzy) {
		super();
		this.nid = nid;
		this.tid = tid;
		this.ntitle = ntitle;
		this.nzz = nzz;
		this.ncontent = ncontent;
		this.nlook = nlook;
		this.nzy = nzy;
	}
	public News() {
		super();
	}
	public News(int tid, String ntitle, String nzz, String ncontent, Date ndate, int nlook, String nzy, String ntp) {
		super();
		this.tid = tid;
		this.ntitle = ntitle;
		this.nzz = nzz;
		this.ncontent = ncontent;
		this.ndate = ndate;
		this.nlook = nlook;
		this.nzy = nzy;
		this.ntp = ntp;
	}
	public News(int nid, int tid, String ntitle, String nzz, String ncontent, Date ndate, int nlook, String nzy,
			String ntp) {
		super();
		this.nid = nid;
		this.tid = tid;
		this.ntitle = ntitle;
		this.nzz = nzz;
		this.ncontent = ncontent;
		this.ndate = ndate;
		this.nlook = nlook;
		this.nzy = nzy;
		this.ntp = ntp;
	}
	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 getNcontent() {
		return ncontent;
	}
	public void setNcontent(String ncontent) {
		this.ncontent = ncontent;
	}
	public Date getNdate() {
		return ndate;
	}
	public void setNdate(Date ndate) {
		this.ndate = ndate;
	}
	public int getNlook() {
		return nlook;
	}
	public void setNlook(int nlook) {
		this.nlook = nlook;
	}
	public String getNzy() {
		return nzy;
	}
	public void setNzy(String nzy) {
		this.nzy = nzy;
	}
	public String getNtp() {
		return ntp;
	}
	public void setNtp(String ntp) {
		this.ntp = ntp;
	}
	
	
}

NewsDao类:新闻增删改查等的方法 

package com.zking.dao;

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

import com.zking.entity.News;
import com.zking.entity.Ptextt;
import com.zking.util.DBHelper;

public class NewsDao {
	public int addNews(News newst) {
		int i=0;
		Connection con=null;
		PreparedStatement ps=null;
		try {
			con=DBHelper.getCon();
			ps=con.prepareStatement("insert into newst(nid,tid,ntitle,nzz,ncontent,ndate,nlook,nzy)values(?,?,?,?,?,sysdate,0,?)");
			ps.setInt(1, DBHelper.getNextId("newst", "nid"));
			ps.setInt(2, newst.getTid());
			ps.setString(3, newst.getNtitle());
			ps.setString(4, newst.getNzz());
			ps.setString(5, newst.getNcontent());
			ps.setString(6, newst.getNzy());
			i=ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBHelper.closeDB(con, ps, null);
		}
		return i;
	}
	
	
	public int delNews(int n) {
		int i=0;
		Connection con=null;
		PreparedStatement ps=null;
		try {
			con=DBHelper.getCon();
			ps=con.prepareStatement("delete newst where nid="+n);
			i=ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBHelper.closeDB(con, ps, null);
		}
		return i;
	}
	
	
	public int updaNewst(int nid,News newst) {
		int i=0;
		Connection con=null;
		PreparedStatement ps=null;
		try {
			con=DBHelper.getCon();
			ps=con.prepareStatement("update newst set tid=?,ntitle=?,nzz=?,ncontent=?,nzy=? where nid="+nid);
			ps.setInt(1, newst.getTid());
			ps.setString(2, newst.getNtitle());
			ps.setString(3, newst.getNzz());
			ps.setString(4, newst.getNcontent());
			ps.setString(5, newst.getNzy());
			i=ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBHelper.closeDB(con, ps, null);
		}
		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();
			String sql = "select * from(select a.*,rownum mid from newst a)b where mid>=? and mid<=?";		
			ps=con.prepareStatement(sql);
			ps.setInt(1, start);
			ps.setInt(2, end);
			
			rs=ps.executeQuery();
			while(rs.next()) {
				News newst=new News(rs.getInt(1), rs.getInt(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getDate(6), rs.getInt(7), rs.getString(8), rs.getString(9));
				nlist.add(newst);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBHelper.closeDB(con, ps, rs);
		}
		return nlist;
	}
	
	public ArrayList<News> pageNewsByTid(int pageIndex,int pageSize,int tid){
		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 newst a where tid="+tid+") b where mid>=? and mid<=?";		
			ps=con.prepareStatement(sql);
			ps.setInt(1, start);
			ps.setInt(2, end);
			
			rs=ps.executeQuery();
			while(rs.next()) {
				News newst=new News(rs.getInt(1), rs.getString(3), rs.getString(4), rs.getString(5), rs.getDate(6), rs.getInt(7), rs.getString(8), rs.getString(9));
				nlist.add(newst);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBHelper.closeDB(con, ps, rs);
		}
		return nlist;
	}
	
	public int getMaxPage(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 newst where ntitle 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();
		}finally {
			DBHelper.closeDB(con, ps, rs);
		}
		return maxPage;
	}
	
	public int getMaxPageByTid(int pageSize,int tid) {
		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 newst where tid like '%"+tid+"%'");
			rs = ps.executeQuery();
			if(rs.next()) {
				count = rs.getInt(1);
				maxPage = count/pageSize;
				if(count%pageSize!=0) {
					maxPage++;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBHelper.closeDB(con, ps, rs);
		}
		return maxPage;
	}
	
	//分页的模糊查询
	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 newst 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()) {
				News newst=new News(rs.getInt(1), rs.getInt(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getDate(6), rs.getInt(7), rs.getString(8), rs.getString(9));
				nlist.add(newst);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBHelper.closeDB(con, ps, rs);
		}
		return nlist;
	}
	
	public News p(int n) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		News pp=null;
		try {
			con = DBHelper.getCon();
			ps = con.prepareStatement("select * from Newst where nid="+n);
			rs = ps.executeQuery();
			if(rs.next()) {
				pp=new News(rs.getInt(1), rs.getInt(2), rs.getString(3), rs.getString(4), rs.getString(5) , rs.getDate(6), rs.getInt(7), rs.getString(8), rs.getString(9));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBHelper.closeDB(con, ps, rs);
		}
		return pp;
	}
	public static void main(String[] args) {
		System.out.println(new NewsDao().delNews(50));
	}
	
}

 联合一起来使用

<%@page import="java.util.Date"%>
<%@page import="com.zking.entity.News"%>
<%@page import="com.zking.dao.NewsDao"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	request.setCharacterEncoding("utf-8");
	int tid=Integer.valueOf(request.getParameter("ntid"));
	String ntile=request.getParameter("ntitle");
	String nzz=request.getParameter("nauthor");
	String nzy=request.getParameter("nsummary");
	String ncontent=request.getParameter("ncontent");
	
	int i=new NewsDao().addNews(new News(1,tid,ntile,nzz,ncontent,0,nzy));
	
	if(i>0){
		out.print("<script>alert('成功成功!!');location.href='../admin.jsp'</script>");
	}else{
		out.print("<script>alert('添加失败!!');location.href='add_new.jsp'</script>");
	}
%>

其它类似,所以我只举一例

总结:

        javaBean封装后,一些冗长重复的代码可以直接拿来调用,省时省力,代码整洁清晰。

  • 15
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 18
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

I小菜鸡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值