新闻项目完整版及代码封装

新闻系统展示效果


登录页面



主页面


添加新闻

 


添加主题

 


编辑主题

 


修改新闻

 


删除新闻


dao类


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 {
	//添加新闻
		/**
		 * 添加新闻
		 * @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 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());
				ps.setString(7, news.getNimage());
				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=?,npath=? 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());
				ps.setString(6, news.getNimage());
				i = ps.executeUpdate();
			} catch (Exception e) {
				e.printStackTrace();
			}finally {
				DBHelper.closeDb(con, ps, null);
			}
			return i;
		}
		public News byid(int id) {
			News n=null;
			Connection con = null;
			PreparedStatement ps = null;
			ResultSet rs = null;
			try {
				con = DBHelper.getCon();
				String sql = "select * from News where nid="+id;
				ps = con.prepareStatement(sql);
				
				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);
					//实例化对象
				 n = new News(nid, tid, ntitle, nzz, nnr, nzy, ndate, nlook, nimage);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}finally {
				DBHelper.closeDb(con, ps, rs);
			}
			return n;
			
		}
		//分页查询:
		/**
		 * 分页查询
		 * @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 tid 新闻主题编号
		 * @return 返回查询到的数据集合
		 */
		public ArrayList<News> pageNewsbyid(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 news 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()) {
					int nid = rs.getInt(1);
					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;
		}
		
		//最大页码方法
		/**
		 * 求出最大也,啊
		 * @param pageSize 每页数据条数
		 * @return 返回最大页码
		 */
		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;
		}
		/**
		 * 求出最大页码
		 * @param pageSize 每页数据条数
		 * @param str 关键字
		 * @return 返回最大页码
		 */
		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 news 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;
		}
		/**
		 * 获取指定主题的新闻最大页码
		 * @param pageSize 每页数据条数
		 * @param tid 新闻的主题编号
		 * @return 返回最大页码
		 */
		public int getMaxPagebyid(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 news where tid="+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;
		}
		//模糊查询
		//分页模糊查询
		//。。。。
		/**
		 * 根据主题查询
		 * @param s 主题编号
		 * @return 返回查询到的数据集合
		 */
		public ArrayList<News>getAll(int s){
			ArrayList<News> slist = new ArrayList<>();
			Connection con = null;
			PreparedStatement ps = null;
			ResultSet rs = null;
			try {
				con = DBHelper.getCon();
				String sql = "select a.*,rownum mid from news a where tid="+s;
				ps = con.prepareStatement(sql);
				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);
					//把新闻对象放到集合中
					slist.add(news);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}finally {
				DBHelper.closeDb(con, ps, rs);
			}
			return slist;
		}
		/**
		 * 根据主题查询
		 * @param s主题编号
		 * @return 返回查询到的数据对象 slist
		 */
		public News getds(int s){
			News slist = null;
			Connection con = null;
			PreparedStatement ps = null;
			ResultSet rs = null;
			try {
				con = DBHelper.getCon();
				String sql = "select * from news a where nid="+s;
				ps = con.prepareStatement(sql);
				rs = ps.executeQuery();
				if(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);
					//实例化对象
					slist = new News(nid, tid, ntitle, nzz, nnr, nzy, ndate, nlook, nimage);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}finally {
				DBHelper.closeDb(con, ps, rs);
			}
			return slist;
		}
}

PtextDao

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

import entity.Ptext;
import util.DBHelper;

public class PtextDao {
	//添加评论
		/**
		 * 添加评论的方法
		 * @param ds要添加的评论
		 * @return 添加成功返回1,失败返回0
		 */
		public int addptext(Ptext ds) {
			int i=0;
			Connection con = null;
			PreparedStatement ps = null;
			try {
				con = DBHelper.getCon();
				ps = con.prepareStatement("insert into ptext values(?,?,?,?,sysdate,?)");
				ps.setInt(1, DBHelper.getNextId("ptext", "pid"));
				ps.setInt(2, ds.getUuid());
				ps.setInt(3, ds.getNid());
				ps.setString(4, ds.getPnr());
				ps.setString(5, ds.getPip());
				i=ps.executeUpdate();
			} catch (Exception e) {
				e.printStackTrace();
			}finally {
				DBHelper.closeDb(con, ps, null);
			}
			return i;
		}
		//删除评论:根据评论编号删除
		/**
		 * 评论的删除方法
		 * @param s 要删除评论的编号
		 * @return 成功返回1,失败返回0
		 */
		public int delte(int s) {
			int i=0;
			Connection con = null;
			PreparedStatement ps = null;
			try {
				con = DBHelper.getCon();
				ps = con.prepareStatement("delete ptext where pid="+s);
				i=ps.executeUpdate();
			} catch (Exception e) {
				e.printStackTrace();
			}finally {
				DBHelper.closeDb(con, ps, null);
			}
			return i;
		}
		//查询评论:根据新闻编号查询
		/**
		 * 根据新闻编号查询方法
		 * @param s 新闻编号
		 * @return 成功返回查询到的数据集合
		 */
		public ArrayList<Ptext>getptex(int s){
			ArrayList<Ptext> slist = new ArrayList<>();
			Connection con = null;
			PreparedStatement ps = null;
			ResultSet rs = null;
			try {
				con = DBHelper.getCon();
				String sql = "select * from ptext where nid="+s+" order by pid";
				ps = con.prepareStatement(sql);
				rs = ps.executeQuery();
				while(rs.next()) {
					int pid=rs.getInt(1);
					int uuid=rs.getInt(2);
					int nid=rs.getInt(3);
					String pnr=rs.getString(4);
					Date ptime=rs.getDate(5);
					String pip=rs.getString(6);
					//实例化对象
					Ptext sgf=new Ptext(pid,uuid, nid, pnr, ptime, pip);
					//把新闻对象放到集合中
					slist.add(sgf);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}finally {
				DBHelper.closeDb(con, ps, rs);
			}
			
			return slist;
		}
		public int addfl(int nid){
			int i=0;
			Connection con = null;
			PreparedStatement ps = null;
			ResultSet rs = null;
			try {
				con = DBHelper.getCon();
				ps = con.prepareStatement("update news set nlook=nlook+1 where nid="+nid);
				rs = ps.executeQuery();		
			} catch (Exception e) {
				e.printStackTrace();
			}finally {
				DBHelper.closeDb(con, ps, rs);
			}
			return i;
		}
}

SubjectDao

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

import entity.Subject;
import util.DBHelper;

public class SubjectDao {
	//添加主题
	/**
	 * 添加主题方法
	 * @param fd 添加对象
	 * @return 成功返回1,失败返回0
	 */
	public int addsubj(Subject fd) {
		int s=0;
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = DBHelper.getCon();
			ps = con.prepareStatement("insert into subject values(?,?)");
			ps.setInt(1, DBHelper.getNextId("subject", "tid"));
			ps.setString(2, fd.getTname());
			s = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBHelper.closeDb(con, ps, null);
		}
		return s;
	}
	//修改主题
	/**
	 * 修改主题
	 * @param sd修改后的主题
	 * @param fd要修改主题的编号
	 * @return 成功返回1,失败返回0
	 */
	public int update(Subject sd,int fd) {
		int s=0;
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = DBHelper.getCon();
			ps = con.prepareStatement("update subject set tname=? where tid="+fd);
			ps.setString(1, sd.getTname());
			s = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBHelper.closeDb(con, ps, null);
		}
		return s;
	}
	//删除主题
	/**
	 * 删除主题方法
	 * @param fd 主题编号
	 * @return 成功返回1,失败返回0
	 */
	public int dele(int fd) {
		int s=0;
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = DBHelper.getCon();
			ps = con.prepareStatement("delete subject where tid="+fd);
			s = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBHelper.closeDb(con, ps, null);
		}
		return s;
	}
	//查询主题
	/**
	 * 查询所以新闻主题
	 * @return 返回主题集合
	 */
	public ArrayList<Subject>getAll(){
		ArrayList<Subject> slist = new ArrayList<>();
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			con = DBHelper.getCon();
			String sql = "select * from subject";
			ps = con.prepareStatement(sql);
			rs = ps.executeQuery();
			while(rs.next()) {
				int tid=rs.getInt(1);
				String tname=rs.getString(2);
				Subject s=new Subject(tid,tname);
				slist.add(s);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBHelper.closeDb(con, ps, rs);
		}
		return slist;
	}
}

UserDao

package dao;

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

import entity.User;
import util.DBHelper;

public class UserDao {
	//用户登录-根据帐号 密码 进行查询
		/**
		 * 用户登录
		 * @param uname 用户名
		 * @param upwd 用户密码
		 * @return 登录成功返回用户对象,失败返回null
		 */
		public User login(String uname,String upwd) {
			User user = null;
			Connection con = null;
			PreparedStatement ps = null;
			ResultSet rs= null;
			try {
				con = DBHelper.getCon();
				ps = con.prepareStatement("select * from T277 where uname=? and upwd=?");
				ps.setString(1, uname);
				ps.setString(2, upwd);
				rs = ps.executeQuery();
				//操作数据
				if(rs.next()) {
					int uuid = rs.getInt(1);
					String uinfo = rs.getString(4);
					
					user = new User(uuid, uname, upwd, uinfo);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}finally {
				
				DBHelper.closeDb(con, ps, rs);
			}
			return user;
		}
		/**
		 * 用户注册
		 * @param user 注册的用户对象
		 * @return
		 */
		public int register(User user) {
			int i = 0;
			Connection con = null;
			PreparedStatement ps = null;
			try {
				con = DBHelper.getCon();
				ps = con.prepareStatement("insert into T277 values(?,?,?,?)");
				ps.setInt(1, user.getUuid());
				ps.setString(2, user.getUname());
				ps.setString(3, user.getUpwd());
				ps.setString(4, user.getUinfo());
				i = ps.executeUpdate();
			} catch (Exception e) {
				e.printStackTrace();
			}finally {
				DBHelper.closeDb(con, ps, null);
			}
			return i;
		}
		
	}


login登录页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户登录</title>
<link href="images/login.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
	var str = "qwertyupasdfghjkzxcvbnmQWERTYUPASDFGHJKLZXCVBNM23456789";
	function yz() {
		//随机出4个0-字符串长度之间的数字 做为下标
		yzmStr="";
		for(var i=1;i<=4;i++){
			yzmStr+=str.substr(Math.round(Math.random()*(str.length-1)),1);
		}
		//把验证码赋值到页面
		document.getElementById("syzm").innerHTML=yzmStr;
	}
	
	
	function $(id) {
		return document.getElementById(id);
	}
	
	function login_yz() {
		//验证 用户名 和 密码不能为空 
		//获取用户名
		var uname = $("uname").value;	
		if(uname.length==0){
			alert("用户名不能为空");
			return false;
		}
		//获取密码
		var upwd = $("upwd").value;
		if(upwd.length==0){
			alert("密码不能为空");
			return false;
		}
		
		//获取验证码:判断和电脑的验证码是否相等
	    //PS:自己实现-输入的验证码不区分大小写-大小写转换
	    var yzm = $("uyzm").value;
	    //yzm.lower();
	    var str1=yzm.toUpperCase();
	    //alert(str1);
	    var str2=yzmStr.toUpperCase();
	    if(str1!=str2){
	      alert("验证码输入错误,请重新输入");
	      //清空输入的验证码
	      $("uyzm").value="";
	      //重新生成验证码
	      yz();
	      return false;
	    }    
		
	}


</script>
</head>
<body onload="yz()">

	<div id="login">

		<div id="top">
			<div id="top_left">
				<img src="images/login_03.gif" />
			</div>
			<div id="top_center"></div>
		</div>

		<div id="center">
			<div id="center_left"></div>
			<div id="center_middle">
			<form action="dologin.jsp" onsubmit="return login_yz()">
				<div id="user">
					用 户 <input type="text" id="uname" name="textfield" />
				</div>
				<div id="password">
					密 码 <input type="password" id="upwd" name="textfield2" />
				</div>
				
				<div id="yzm">
					验证码 <input id="uyzm" style="width: 50px;" type="text" name="textfield3" />
					<span id="syzm" onclick="yz()"></span>
				</div>
				<div id="btn">
					<input type="submit" value="登录">
					<input type="reset" value="清空">
					<a href="register.jsp">注册 </a>
				</div>
			</form>
			</div>
			<div id="center_right"></div>
		</div>
		<div id="down">
			<div id="down_left">
				<div id="inf">
				
					<span class="inf_text">版本信息</span> <span class="copyright">管理信息系统
						2008 v2.0</span>
				</div>
			</div>
			<div id="down_center"></div>
		</div>

	</div>
</body>
</html>

dologin

<%@page import="entity.User"%>
<%@page import="dao.UserDao"%>
<%@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"%>
<%
	request.setCharacterEncoding("utf-8");
	String uname = request.getParameter("textfield");
	String jks=request.getParameter("jk");
	int jk=0;
	String upwd = request.getParameter("textfield2");
	UserDao t=new UserDao();
	User T=t.login(uname, upwd);
	if(T!=null){//成功就跳转到主页面
			session.setAttribute("a", T);
			request.getRequestDispatcher("admin.jsp").forward(request, response);
	}else{//错误就回到登录页面
			out.print("<script>alert('用户名或密码错误,请重新登录');location.href='login.jsp'</script>");
		}
%>

admin主页面

<%@page import="entity.News"%>
<%@page import="java.util.ArrayList"%>
<%@page import="dao.NewsDao"%>
<%@page import="entity.User"%>
<%@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("a");
	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">管理员: <%=((User)session.getAttribute("a")).getUname() %>  &#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="newspages/add_news.jsp">添加新闻</a></li>
      <li><a href="admin.jsp">查找新闻</a></li>
      <li><a href="newspages/add_sub.jsp">添加主题</a></li>
      <li><a href="newspages/upsub.jsp">修改主题</a></li>
      <li><a href="admin.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;//页码
    		//接收页码
    		String index = request.getParameter("pageIndex");
    		if(index!=null){//如果接收到页面,就给页面pageIndex赋值
    			pageIndex = Integer.valueOf(index);
    		}
    		int pageSize = 5;//每页的数据条数,每页5条数据
    		
    		//实例化 NewsDao
    		NewsDao nd = new NewsDao();
    		int maxPage = nd.getMaxPage(pageSize,str);//调用求 最大页面方法 获取最大页面
    		//调用分页查询方法
    		ArrayList<News> nlist = nd.pagelikeNews(pageIndex, pageSize, str);
			
    		//遍历集合
    		for(News n:nlist){
    	%>
      <li>
      	<a href="newspages/read_news.jsp?nid=<%=n.getNid()%>">
      		<%=n.getNtitle() %>
      	</a>
      <span> 作者:
        <%=n.getNzz() %> &#160;&#160;&#160;&#160; 
        <a href='newspages/up_news.jsp?nid=<%=n.getNid()%>'>修改</a> &#160;&#160;&#160;&#160; 
        <a href='newspages/dodelnews.jsp?nid=<%=n.getNid()%>' onclick='return clickdel()'>删除</a> </span> </li>
      <li class='space'></li>
      <%} %>
      <p align="right"> 
      		<a href="admin.jsp<%
      				if(str!=null){
      	      			out.print("?str="+str);
      	      		}
      		%>">首页</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%><%
      				if(str!=null){
      	      			out.print("&str="+str);
      	      		}
      		%>">末页</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>
</html>

index用户

<%@page import="entity.News"%>
<%@page import="dao.NewsDao"%>
<%@page import="entity.Subject"%>
<%@page import="java.util.ArrayList"%>
<%@page import="dao.SubjectDao"%>
<%@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"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>新闻中国</title>
<link href="CSS/main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
function $(id) {
	return document.getElementById(id);
}
function login() {
	//验证 用户名 和 密码不能为空 
	//获取用户名
	var uname = $("uname").value;	
	if(uname.length==0){
		alert("用户名不能为空");
		return false;
	}
	//获取密码
	var upwd = $("upwd").value;
	if(upwd.length==0){
		alert("密码不能为空");
		return false;
	}
}
</script>
</head>
<body>
<div id="header">
  <div id="top_login">
  <form action="login.jsp" onsubmit="return login()">
    <label> 登录名 </label>
    <input type="text" name="textfield" class="login_input" />
    <label> 密&#160;&#160;码 </label>
    <input type="password" name="textfield2" class="login_input" />
    <input type="submit" class="login_sub" value="登录"/>
    <input type="hidden" value="1" name="jk"> 
    <label id="error"> </label>
    <img src="images/friend_logo.gif" alt="Google" id="friend_logo" />  </form>
    </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>
    <!--mainnav end-->
  </div>
</div>
<div id="container">
  <div class="sidebar">
    <h1> <img src="images/title_1.gif" alt="国内新闻" /> </h1>
    <div class="side_list">
      <ul>
      <%
      //实例化一个subjectDao对象
      NewsDao dds=new NewsDao();
      ArrayList<News> slist=dds.getAll(1);
    //遍历集合
    for(News n:slist){
      %>
        <li> <a href='newspages/read_news.jsp?nid=<%=n.getTid()%>'><b><%=n.getNtitle() %></b></a> </li>
      <%} %>
      </ul>
    </div>
    <h1> <img src="images/title_2.gif" alt="国际新闻" /> </h1>
    <div class="side_list">
      <ul>
      <% 
      //实例化NewsDao 
      NewsDao dd=new NewsDao();
      ArrayList<News> hh=dd.getAll(2);
	for(News v:hh){
  	%>
        <li> <a href='newspages/read_news.jsp?nid=<%=v.getNid()%>'><b><%=v.getNtitle() %></b></a> </li>
      <%} %>
      </ul>
    </div>
    <h1> <img src="images/title_3.gif" alt="娱乐新闻" /> </h1>
    <div class="side_list">
      <ul>
          <% 
      ArrayList<News> sb=dd.getAll(5);
          for(News v:sb){
  	%>
        <li> <a href='newspages/read_news.jsp?nid=<%=v.getNid()%>'><b><%=v.getNtitle() %></b></a> </li>
        
      <%} %>
      </ul>
    </div>
  </div>
  <div class="main">
    <div class="class_type"> <img src="images/class_type.gif" alt="新闻中心" /> </div>
    <div class="content">
      <ul class="class_date">
        <li id='class_month'> 
        	<%
        	SubjectDao sf=new SubjectDao();
        	ArrayList<Subject> ssa=sf.getAll();
        	//查询主题
        	for(Subject vd:ssa){
        	%>
        	<a href='index.jsp?tid=<%=vd.getTid() %>'><b> <%=vd.getTname() %> </b></a>
        	<%} %> 
         </li>
      </ul>
      <ul class="classlist">
      	<%
      		//实例化NewsDao对象
      		NewsDao nd=new NewsDao();
      	//接收页码
      	String index=request.getParameter("pageIndex");
      		//接收新闻主题
      		String id = request.getParameter("tid");
      		int pageSize = 5;
      		int maxPage=0;
      		ArrayList<News> nlist=null;
      		int pageIndex=1;
      		if(index!=null){
      			pageIndex=Integer.valueOf(index);
      		}
      		//查询新闻
      		if(id!=null){//有就根据主题查询
      			int tid=Integer.valueOf(id);
      		//调用根据主题查询的最大页码
      		maxPage=nd.getMaxPagebyid(pageSize, tid);
      		//调用根据主题查询的数据 分页
      		nlist=nd.pageNewsbyid(pageIndex, pageSize, tid);
      		}else{//没有就查询所有
      		//查询所有的数据最大页码
      		maxPage=nd.getMaxPage(pageSize, "");
      		//调用查询所有新闻的方法 分页查询
      		nlist=nd.pageNews(pageIndex, pageSize);
      		}
      		//遍历集合
      		for(News n:nlist){
      	%>
        <li>
        	<a href='newspages/read_news.jsp?nid=<%=n.getNid()%>'><%=n.getNtitle() %> </a>
        	<span> <%=n.getNdate() %> </span>
        </li>
        <li class='space'></li>
        <%} %>
        <p align="right"> 
        	<a href="index.jsp<%
        		if(id!=null){
        			out.print("?tid="+id);
        		}
        	%>">首页</a> 
        	<a href="index.jsp?pageIndex=<%=pageIndex>1?pageIndex-1:1%><%
        		if(id!=null){
        			out.print("&tid="+id);
        		}
        	%>">上一页</a>
        		&nbsp;当前页数:[<%=pageIndex %>/<%=maxPage %>]&nbsp; 
        	<a href="index.jsp?pageIndex=<%=pageIndex<maxPage?pageIndex+1:maxPage%><%
        			if(id!=null){
        				out.print("&tid="+id);
        			}
        	%>">下一页</a> 
        	<a href="index.jsp?pageIndex=<%=maxPage%><%
        		if(id!=null){
        			out.print("&tid="+id);
        		}
        	%>">末页</a> 
        </p>
      </ul>
    </div>
    <div class="picnews">
      <ul>
        <li> <a href="#"><img src="images/Picture1.jpg" width="249" alt="" /> </a><a href="#">幻想中穿越时空</a> </li>
        <li> <a href="#"><img src="images/Picture2.jpg" width="249" alt="" /> </a><a href="#">国庆多变的发型</a> </li>
        <li> <a href="#"><img src="images/Picture3.jpg" width="249" alt="" /> </a><a href="#">新技术照亮都市</a> </li>
        <li> <a href="#"><img src="images/Picture4.jpg" width="249" alt="" /> </a><a href="#">群星闪耀红地毯</a> </li>
      </ul>
    </div>
  </div>
</div>
<div id="friend">
  <h1 class="friend_t"> <img src="../images/friend_ico.gif" alt="合作伙伴" /> </h1>
  <div class="friend_list">
    <ul>
      <li> <a href="#">中国政府网</a> </li>
      <li> <a href="#">中国政府网</a> </li>
      <li> <a href="#">中国政府网</a> </li>
      <li> <a href="#">中国政府网</a> </li>
      <li> <a href="#">中国政府网</a> </li>
      <li> <a href="#">中国政府网</a> </li>
      <li> <a href="#">中国政府网</a> </li>
    </ul>
  </div>
</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>
</html>

add_sub添加主题

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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("a");
	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">管理员: 登录  &#160;&#160;&#160;&#160;<a href="#">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="../admin.jsp">编辑新闻</a></li>
      <li><a href="../admin.jsp">查找新闻</a></li>
      <li><a href="#">添加主题</a></li>
      <li><a href="upsub.jsp">编辑主题</a></li>
    </ul>
  </div>
  <div id="opt_area">
    <h1 id="opt_type"> 添加主题: </h1>
    <form action="doadd_sub.jsp" method="post">
      <p>
        <label> 主题: </label>
        <input name="nauthor" type="text" class="opt_input" />
      </p>
      <input name="action" type="hidden" value="addnews">
      <input type="submit" value="提交" class="opt_sub" />
      <input type="reset" value="重置" class="opt_sub" />
    </form>
  </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>
</html>


doadd_news添加新闻

<%@page import="dao.NewsDao"%>
<%@page import="entity.News"%>
<%@page import="com.jspsmart.upload.Request"%>
<%@page import="com.jspsmart.upload.SmartUpload"%>
<%@page import="com.jspsmart.upload.File"%>
<%@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"%>
<%
	//创建SmartUpload对象
	SmartUpload su = new SmartUpload();
	//初始化
	su.initialize(pageContext);
	//声明一个File对象 用来接收上传的文件
	File file = null;
	//设置允许上传的文件类型
	su.setAllowedFilesList("jpg,png,gif,jpeg");
	//设置不允许上传的文件类型
	su.setDeniedFilesList("bat,exe,mp4");
	//设置单文件大小
	su.setMaxFileSize(80000);
	//设置总文件大小
	su.setTotalMaxFileSize(90000);
	//设置编码
	su.setCharset("utf-8");
	//开始上传
	su.upload();	
	//获取文件集合中的第一个文件
	file = su.getFiles().getFile(0);
	String filePath = "";
	if(!file.isMissing()){
		//拼接文件上传到服务器的 路径
		filePath ="onload/"+file.getFileName();
		//上传到服务器 保存到指定路径
		file.saveAs(filePath,SmartUpload.SAVE_VIRTUAL);
	}
	
	Request req = su.getRequest();
	request.setCharacterEncoding("utf-8");
	response.setCharacterEncoding("utf-8");

//接受添加界面的数据
int tid=Integer.valueOf(req.getParameter("ntid"));
String ntitle=req.getParameter("ntitle");
String nzz=req.getParameter("nauthor");
String nzy=req.getParameter("nsummary");
String nnr=req.getParameter("ncontent");
News x=new News(tid,ntitle,nzz,nzy,nnr,filePath);
NewsDao cs=new NewsDao();
int i=cs.addNews(x);
	if(i>0){
		out.print("<script>alert('添加成功');location.href='../admin.jsp'</script>");
	}else{
		out.print("<script>alert('添加失败');location.href='add_news.jsp'</script>");
	}
%>

修改新闻doup_news

<%@page import="entity.News"%>
<%@page import="dao.NewsDao"%>
<%@page import="com.jspsmart.upload.Request"%>
<%@page import="com.jspsmart.upload.File"%>
<%@page import="com.jspsmart.upload.SmartUpload"%>
<%@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"%>
<%
	//创建SmartUpload对象
	SmartUpload su = new SmartUpload();
	//初始化
	su.initialize(pageContext);
	//声明一个File对象 用来接收上传的文件
	File file = null;
	//设置允许上传的文件类型
	su.setAllowedFilesList("jpg,png,gif,jpeg");
	//设置不允许上传的文件类型
	su.setDeniedFilesList("bat,exe,mp4");
	//设置单文件大小
	su.setMaxFileSize(800000);
	//设置总文件大小
	su.setTotalMaxFileSize(9000000);
	//设置编码
	su.setCharset("utf-8");
	//开始上传
	su.upload();	
	//获取文件集合中的第一个文件
	file = su.getFiles().getFile(0);
	String filePath = "";
	if(!file.isMissing()){
		//拼接文件上传到服务器的 路径
		filePath ="onload/"+file.getFileName();
		//上传到服务器 保存到指定路径
		file.saveAs(filePath,SmartUpload.SAVE_VIRTUAL);
	}
	
	Request req = su.getRequest();
	request.setCharacterEncoding("utf-8");
	response.setCharacterEncoding("utf-8");

//接受添加界面的数据
int nid=Integer.valueOf(request.getParameter("nid"));
int tid=Integer.valueOf(req.getParameter("ntid"));
String ntitle=req.getParameter("ntitle");
String nzz=req.getParameter("nauthor");
String nzy=req.getParameter("nsummary");
String nnr=req.getParameter("ncontent");
//String nimage=req.getParameter("file");
NewsDao sd=new NewsDao();
News news=new News(tid,ntitle,nzz,nzy,nnr,filePath);
int i=sd.upNews(nid, news);
	if(i>0){
		out.print("<script>alert('修改成功');location.href='../admin.jsp'</script>");
	}else{
		out.print("<script>alert('修改失败');location.href='up_news.jsp?nid="+nid+"'</script>");
	}
%>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值