这里的简化代码即封装。
用之前写过的新闻项目来举例,之前的整个项目都是在WebConnect中完成的,但现在封装代码我们需要再java Resources中的src下建包谢东西。也是我们以前在eclipse中常用的三个包:
实体类:entity;帮助类:DBhelper;方法类:dao
用之前写过的新闻模糊查询为例:
DBhelper
package 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 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 (ClassNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static Connection getCon() {
Connection con=null;
try {
con=DriverManager.getConnection(url, user, upwd);
} catch (Exception e) {
// TODO: handle exception
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) {
// TODO: handle exception
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) {
// TODO: handle exception
e.printStackTrace();
}finally {
DBHelper.closeDB(con, ps, rs);
}
return id;
}
}
entity(news)
package entity;
import java.sql.Date;
public class News {
private int nid;
private int tid;
private String ntitle;
private String nzz;
private String nnr;
private String nzy;
private Date ndate;
private int nlook;
private String nimage;
public News() {
super();
}
public News(int tid, String ntitle, String nzz, String nnr, String nzy, Date ndate, int nlook, String nimage) {
super();
this.tid = tid;
this.ntitle = ntitle;
this.nzz = nzz;
this.nnr = nnr;
this.nzy = nzy;
this.ndate = ndate;
this.nlook = nlook;
this.nimage = nimage;
}
public News(int nid, int tid, String ntitle, String nzz, String nnr, String nzy, Date ndate, int nlook,
String nimage) {
super();
this.nid = nid;
this.tid = tid;
this.ntitle = ntitle;
this.nzz = nzz;
this.nnr = nnr;
this.nzy = nzy;
this.ndate = ndate;
this.nlook = nlook;
this.nimage = nimage;
}
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 getNnr() {
return nnr;
}
public void setNnr(String nnr) {
this.nnr = nnr;
}
public String getNzy() {
return nzy;
}
public void setNzy(String nzy) {
this.nzy = nzy;
}
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 getNimage() {
return nimage;
}
public void setNimage(String nimage) {
this.nimage = nimage;
}
@Override
public String toString() {
return "News [nid=" + nid + ", tid=" + tid + ", ntitle=" + ntitle + ", nzz=" + nzz + ", nnr=" + nnr + ", nzy="
+ nzy + ", ndate=" + ndate + ", nlook=" + nlook + ", nimage=" + nimage + "]";
}
}
newsdao
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 {
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;
}
//删除新闻
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;
}
//修改新闻
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;
}
//分页查询:
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;
}
//最大页码方法
public 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 news");
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;
}
}
admin(新闻界面)
<%@page import="entity.News"%>
<%@page import="dao.NewsDao"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加主题--管理后台</title>
<link href="CSS/admin.css" rel="stylesheet" type="text/css" />
</head>
<body>
<%
Object obj = session.getAttribute("kkk");
if(obj==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">
管理员:<%=session.getAttribute("kkk") %> 登录      <a href="index.jsp">login out</a>
</div>
<div id="channel"></div>
</div>
<div id="main">
<div id="opt_list">
<ul>
<li><a href="newspages/add_news.jsp">添加新闻</a></li>
<li><a href="#">编辑新闻</a></li>
<li><a href="#">查找新闻</a></li>
<li><a href="newspages/add_sub.jsp">添加主题</a></li>
<li><a href="newspages/up_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'>
<div align = 'center'>
<input type = 'text' name = 'str'>
<input type = 'submit' value = '查询'>
</div>
</form>
<ul class="classlist">
<%
String str = request.getParameter("str");
if(str==null){
str = "";
}
int pageIndex = 1;
String index = request.getParameter("pageIndex");
if(index!=null){
pageIndex = Integer.valueOf(index);
}
int pageSize = 5;
NewsDao nd=new NewsDao();
int maxPage=nd.getMaxPage(pageSize);
for(News n:nlist){
%>
<li>
<a href="newspages/read_news.jsp?nid=<%=n.getNid()%>">
<%=n.getNtitle() %>
</a><span> 作者:
<%=n.getNzz() %>
     <a href='newspages/update_news.jsp?nid=<%=n.getNid()%>'>修改</a>
     <a href='newspages/dodel.jsp?nid=<%=n.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%><%
%>">上一页</a>
当前页数:[<%=pageIndex %>/<%=maxPage %>]
<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      <a href="#">常见问题解答</a>
     新闻热线:010-627488888<br />
文明办网文明上网举报电话:010-627488888      举报邮箱:<a href="#">jubao@jb-aptech.com.cn</a>
</p>
<p class="copyright">
Copyright © 1999-2009 News China gov, All Right Reserver<br />
新闻中国 版权所有
</p>
</div>
</body>
</html>