biz
package cn.jbit.biz; import java.util.List; import cn.jbit.dao.CommentDao; import cn.jbit.dao.impl.CommentDaoImpl; import cn.jbit.entity.Comment; /** * * @author chaoyi * @2013-12-15 */ public class CommentBiz { CommentDao commentDao = new CommentDaoImpl(); /** * 查询评论信息 * * @param id * @return List<Comment> */ public List<Comment> getCommentList(int id) { String sql = "select cid, cnid, ccontent, cdate, cip, cauthor" + " from comments" + " where cnid = ? order by cdate asc"; Object[] pars = { id }; return commentDao.executeQuery(sql, pars); } /** * 添加评论信息 * * @param comment * @return int */ public int addComment(Comment comment) { String sql = "insert into comments" + " (cid, cnid, ccontent, cdate, cip, cauthor)" + " values" + " (seq_comment.nextval, ?, ?, sysdate, ?, ?)"; Object[] pars = { comment.getCnid(), comment.getCcontent(), comment.getCip(), comment.getCauthor() }; return commentDao.executeUpdate(sql, pars); } } package cn.jbit.biz; import java.util.List; import cn.jbit.dao.NewsDao; import cn.jbit.dao.impl.NewsDaoImpl; import cn.jbit.entity.News; public class NewsBiz { NewsDao newsDao = new NewsDaoImpl(); /** * 查询新闻列表信息 * * @return List<News> */ public List<News> getNewsList() { String sql = "select nid," + " ntid," + " ntitle," + " nauthor," + " ncreatedate," + " npicpath," + " ncontent," + " nmodifydate," + " nsummary" + " from news" + " where rownum < 11" + " order by ncreatedate desc"; return newsDao.executeQuery(sql, null); } /** * 根据id查询新闻信息 * * @param id * @return News */ public News getNewsById(int id) { News item = null; String sql = "select nid," + " ntid," + " ntitle," + " nauthor," + " ncreatedate," + " npicpath," + " ncontent," + " nmodifydate," + " nsummary" + " from news" + " where nid = ?"; Object[] pars = { id }; List<News> list = newsDao.executeQuery(sql, pars); if (list.size() > 0) { item = list.get(0); } return item; } /** * 分页查询新闻列表信息 * * @param pageIndex * 第几页 * @param pageSize * 每页的数目 * @return List<News> */ public List<News> getNewsListByPage(int pageIndex, int pageSize) { int start = (pageIndex - 1) * pageSize; // 上限 int end = pageIndex * pageSize; // 下限 String sql = "select *" + " from (select temp.*,rownum num" + " from (select news.* from news order by news.ncreatedate desc) temp" + " where rownum <= ?) temp2" + " where num > ?"; Object[] pars = { end, start }; return newsDao.executeQuery(sql, pars); } /** * 获取总页数 * * @param pageSize * @return */ public int getTotalPage(int pageSize) { String sql = "select ceil(count(*)/?) from news"; Object[] pars = { pageSize }; return newsDao.getTotalPage(sql, pars); } } package cn.jbit.biz; import java.util.List; import cn.jbit.dao.TopicDao; import cn.jbit.dao.impl.TopicDaoImpl; import cn.jbit.entity.Topic; public class TopicBiz { TopicDao topicDao = new TopicDaoImpl(); /** * 查询类型列表 * * @return List<Topic> */ public List<Topic> getTopicList() { String sql = "select tid, tname from topic"; return topicDao.executeQuery(sql, null); } }
dao
package cn.jbit.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; /** * 数据连接与关闭工具类 * * @author chaoyi * @2013-11-7 */ public class BaseDao { // 数据库驱动字符串 private static String driver = "oracle.jdbc.driver.OracleDriver"; // 连接URL字符串 private static String url = "jdbc:oracle:thin:@localhost:1521:oracle10"; // 数据库用户名 private static String user = "news"; // 用户密码 private static String password = "accp"; /** * 获取数据库连接对象 * * @return 数据连接对象 */ public Connection getConnection() { Connection conn = null; // 数据连接对象 // 获取连接并捕获异常 try { // 加载驱动 Class.forName(driver); // 获取数据库连接 conn = DriverManager.getConnection(url, user, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; // 返回数据连接对象 } /** * 关闭数据库连接 * * @param conn * 数据库连接 * @param pstmt * PreparedStatement对象 * @param rs * 结果集 */ public void closeAll(Connection conn, PreparedStatement pstmt, ResultSet rs) { // 若结果集对象不为空,则关闭 if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } // 若PreparedStatement对象不为空,则关闭 if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } // 若数据库连接对象不为空,则关闭 if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } /** * 执行更新、删除、插入SQL语句命令 * * @param sql * 预编译的 SQL 语句 * @param pars * 预编译的 SQL 语句中的‘?’参数的字符串数组 * @return 影响的条数 */ public int executeUpdate(String sql, Object[] pars) { int rowCount = 0; Connection conn = null; PreparedStatement pstmt = null; try { // 连接数据库 conn = this.getConnection(); // 创建数据库命令对象 pstmt = conn.prepareStatement(sql); if (pars != null) { for (int i = 0; i < pars.length; i++) { pstmt.setObject(i + 1, pars[i]); } } // 执行数据库命令 rowCount = pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } return rowCount; } } package cn.jbit.dao; import java.util.List; import cn.jbit.entity.Comment; public interface CommentDao { public List<Comment> executeQuery(String sql,Object[] pars); public int executeUpdate(String sql,Object[] pars); } package cn.jbit.dao; import java.util.List; import cn.jbit.entity.News; public interface NewsDao { /** * 对新闻进行查询操作 * * @param sql * @param pars * @return list<Pet> */ List<News> executeQuery(String sql, Object[] pars); /** * 对新闻进行增、删、改操作 * * @param sql * @param pars * @return int */ int executeUpdate(String sql, Object[] pars); /** * 查询总页数 * @param sql * @param pars * @return */ int getTotalPage(String sql, Object[] pars); } package cn.jbit.dao; import java.util.List; import cn.jbit.entity.Topic; public interface TopicDao { public List<Topic> executeQuery(String sql,Object[] pars); public int executeUpdate(String sql,Object[] pars); } package cn.jbit.dao; import java.util.List; import cn.jbit.entity.Topic; import cn.jbit.entity.User; public interface UserDao { public List<User> executeQuery(String sql,Object[] pars); public int executeUpdate(String sql,Object[] pars); }
dao.impl
package cn.jbit.dao.impl; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import cn.jbit.dao.BaseDao; import cn.jbit.dao.CommentDao; import cn.jbit.entity.Comment; public class CommentDaoImpl extends BaseDao implements CommentDao { public List<Comment> executeQuery(String sql, Object[] pars) { List<Comment> list = new ArrayList<Comment>(); Connection con = null; PreparedStatement pstmt = null; ResultSet rs = null; try { con = super.getConnection(); pstmt = con.prepareStatement(sql); if(pars!=null){ for (int i = 0; i < pars.length; i++) { pstmt.setObject(i+1, pars[i]); } } rs = pstmt.executeQuery(); while(rs.next()){ Comment item = new Comment(); item.setCid(rs.getString(1)); item.setCnid(rs.getString(2)); item.setCcontent(rs.getString(3)); item.setCdate(rs.getString(4)); item.setCip(rs.getString(5)); item.setCauthor(rs.getString(6)); list.add(item); } } catch (SQLException e) { e.printStackTrace(); }finally{ super.closeAll(con, pstmt, rs); } return list; } } package cn.jbit.dao.impl; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import cn.jbit.dao.BaseDao; import cn.jbit.dao.NewsDao; import cn.jbit.entity.News; public class NewsDaoImpl extends BaseDao implements NewsDao { Connection conn = null; // 数据连接对象 PreparedStatement pstmt = null; // PreparedStatement对象 ResultSet rs = null; // 结果集对象 /** * 对新闻查询操作 */ public List<News> executeQuery(String sql, Object[] pars) { // 创建ArrayList集合对象,并把新闻查询信息放入其中 List<News> list = new ArrayList<News>(); try { // 获取数据连接对象 conn = super.getConnection(); // 创建数据库命令对象 pstmt = conn.prepareStatement(sql); if (pars != null) { for (int i = 0; i < pars.length; i++) { pstmt.setObject(i + 1, pars[i]); } } // 执行数据库命令 rs = pstmt.executeQuery(); // 处理结果 while (rs.next()) { // 创建一个新闻对象 News news = new News(); news.setNid(rs.getInt(1)); news.setNtid(rs.getInt(2)); news.setNtitle(rs.getString(3)); news.setNauthor(rs.getString(4)); news.setNcreatedate(rs.getString(5)); news.setNpicpath(rs.getString(6)); news.setNcontent(rs.getString(7)); news.setNmodifydate(rs.getString(8)); news.setNsummary(rs.getString(9)); // 把新闻对象添加到集合中 list.add(news); } } catch (SQLException e) { e.printStackTrace(); } finally { // 调用关闭数据库连接 super.closeAll(conn, pstmt, rs); } return list; } /** * 查询新闻总页数 */ public int getTotalPage(String sql, Object[] pars) { int totalPage = 0; try { // 获取数据连接对象 conn = super.getConnection(); // 创建数据库命令对象 pstmt = conn.prepareStatement(sql); if (pars != null) { for (int i = 0; i < pars.length; i++) { pstmt.setObject(i + 1, pars[i]); } } // 执行数据库命令 rs = pstmt.executeQuery(); // 处理结果 if (rs.next()) { totalPage = rs.getInt(1); } } catch (SQLException e) { e.printStackTrace(); } finally { // 调用关闭数据库连接 super.closeAll(conn, pstmt, rs); } return totalPage; } } package cn.jbit.dao.impl; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import cn.jbit.dao.BaseDao; import cn.jbit.dao.TopicDao; import cn.jbit.entity.Topic; public class TopicDaoImpl extends BaseDao implements TopicDao { public List<Topic> executeQuery(String sql, Object[] pars) { List<Topic> list = new ArrayList<Topic>(); Connection con = null; PreparedStatement pstmt = null; ResultSet rs = null; try { con = super.getConnection(); pstmt = con.prepareStatement(sql); if(pars!=null){ for (int i = 0; i < pars.length; i++) { pstmt.setObject(i+1, pars[i]); } } rs = pstmt.executeQuery(); while(rs.next()){ Topic item = new Topic(); item.setTid(rs.getString(1)); item.setTname(rs.getString(2)); list.add(item); } } catch (SQLException e) { e.printStackTrace(); }finally{ super.closeAll(con, pstmt, rs); } return list; } } package cn.jbit.dao.impl; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import cn.jbit.dao.BaseDao; import cn.jbit.dao.UserDao; import cn.jbit.entity.User; public class UserDaoImpl extends BaseDao implements UserDao { public List<User> executeQuery(String sql, Object[] pars) { List<User> list = new ArrayList<User>(); Connection con = null; PreparedStatement pstmt = null; ResultSet rs = null; try { con = super.getConnection(); pstmt = con.prepareStatement(sql); if(pars!=null){ for (int i = 0; i < pars.length; i++) { pstmt.setObject(i+1, pars[i]); } } rs = pstmt.executeQuery(); while(rs.next()){ User item = new User(); item.setUid(rs.getString(1)); item.setUname(rs.getString(2)); item.setUpwd(rs.getString(3)); list.add(item); } } catch (SQLException e) { e.printStackTrace(); }finally{ super.closeAll(con, pstmt, rs); } return list; } }
entity
package cn.jbit.entity; import java.io.Serializable; /** * 评论 实体类 * * @author **/ public class Comment implements Serializable { private String cid; private String cnid; private String ccontent; private String cdate; private String cip; private String cauthor; public Comment() { } public Comment(String cid, String cnid, String ccontent, String cdate, String cip, String cauthor) { this.cid = cid; this.cnid = cnid; this.ccontent = ccontent; this.cdate = cdate; this.cip = cip; this.cauthor = cauthor; } public String getCid() { return cid; } public void setCid(String cid) { this.cid = cid; } public String getCnid() { return cnid; } public void setCnid(String cnid) { this.cnid = cnid; } public String getCcontent() { return ccontent; } public void setCcontent(String ccontent) { this.ccontent = ccontent; } public String getCdate() { return cdate; } public void setCdate(String cdate) { this.cdate = cdate; } public String getCip() { return cip; } public void setCip(String cip) { this.cip = cip; } public String getCauthor() { return cauthor; } public void setCauthor(String cauthor) { this.cauthor = cauthor; } } package cn.jbit.entity; import java.io.Serializable; /** * 新闻实体类 * * @author kyaccp * */ public class News implements Serializable { private int nid; // 新闻id private int ntid; // 新闻类型id private String ntitle; // 新闻标题 private String nauthor; // 新闻作者 private String ncreatedate; // 新闻创建日期 private String npicpath; // 新闻图片路径 private String ncontent; // 新闻评论内容 private String nmodifydate; // 新闻修改时间 private String nsummary; // 新闻摘要 /** * 无参构造方法 */ public News() { } /** * 带两个参数构造方法 * * @param ntitle * @param nauthor */ public News(String ntitle, String nauthor) { this.setNtitle(ntitle); this.setNauthor(nauthor); } /** * 带参数构造方法 * * @param nid * @param ntid * @param ntitle * @param nauthor * @param ncreatedate * @param npicpath * @param ncontent * @param nmodifydate * @param nsummary */ public News(int nid, int ntid, String ntitle, String nauthor, String ncreatedate, String npicpath, String ncontent, String nmodifydate, String nsummary) { this.nid = nid; this.ntid = ntid; this.ntitle = ntitle; this.nauthor = nauthor; this.ncreatedate = ncreatedate; this.npicpath = npicpath; this.ncontent = ncontent; this.nmodifydate = nmodifydate; this.nsummary = nsummary; } public int getNid() { return nid; } public void setNid(int nid) { this.nid = nid; } public int getNtid() { return ntid; } public void setNtid(int ntid) { this.ntid = ntid; } public String getNtitle() { return ntitle; } public void setNtitle(String ntitle) { this.ntitle = ntitle; } public String getNauthor() { return nauthor; } public void setNauthor(String nauthor) { this.nauthor = nauthor; } public String getNcreatedate() { return ncreatedate; } public void setNcreatedate(String ncreatedate) { this.ncreatedate = ncreatedate; } public String getNpicpath() { return npicpath; } public void setNpicpath(String npicpath) { this.npicpath = npicpath; } public String getNcontent() { return ncontent; } public void setNcontent(String ncontent) { this.ncontent = ncontent; } public String getNmodifydate() { return nmodifydate; } public void setNmodifydate(String nmodifydate) { this.nmodifydate = nmodifydate; } public String getNsummary() { return nsummary; } public void setNsummary(String nsummary) { this.nsummary = nsummary; } } package cn.jbit.entity; import java.io.Serializable; /** * 主题 实体类 * * @author */ public class Topic implements Serializable { private String tid; private String tname; public Topic() { } public Topic(String tid, String tname) { this.tid = tid; this.tname = tname; } public String getTid() { return tid; } public void setTid(String tid) { this.tid = tid; } public String getTname() { return tname; } public void setTname(String tname) { this.tname = tname; } } package cn.jbit.entity; import java.io.Serializable; /** * 用户 实体类 * * @author */ public class User implements Serializable { private String uid; private String uname; private String upwd; public User() { } public User(String uid, String uname, String upwd) { this.uid = uid; this.uname = uname; this.upwd = upwd; } public void setUid(String uid) { this.uid = uid; } public void setUname(String uname) { this.uname = uname; } public void setUpwd(String upwd) { this.upwd = upwd; } public String getUid() { return uid; } public String getUname() { return uname; } public String getUpwd() { return upwd; } }
---------------------------------------------------
index.jsp
<%@page import="java.util.regex.Pattern"%> <%@page import="cn.jbit.biz.NewsBiz"%> <%@page import="javax.naming.NamingException"%> <%@page import="javax.sql.DataSource"%> <%@page import="javax.naming.InitialContext"%> <%@page import="javax.naming.Context"%> <%@page import="cn.jbit.entity.News"%> <%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK" /> <title>新闻中国</title> <link href="CSS/main.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="header"> <div id="top_login"> <form name="form1" method="post" action="util/doLogin.jsp" onsubmit="return check()"> <label> 登录名 </label> <input type="text" id="uname" value="" name="uname" class="login_input" /> <label> 密  码 </label> <input type="password" id="upwd" value="" name="pwd" class="login_input" /> <input type="submit" class="login_sub" value="登录" onclick="login()"/> <label id="error"> </label> <img src="Images/friend_logo.gif" alt="Google" id="friend_logo" /> <script type="text/javascript"> function check(){ var uname = document.getElementById("uname"); var upwd = document.getElementById("upwd"); if("" == uname.value){ alert("用户名不能为空!"); return false; }else if("" == upwd.value){ alert("密码不能为空"); return false; } return true; } </script> <%! /** *统计访问次数 */ public synchronized void browCount(ServletContext application){ if(application.getAttribute("brow")==null){ application.setAttribute("brow",1); }else{ int count =Integer.parseInt(application.getAttribute("brow").toString()); application.setAttribute("brow",count+1); } } %> <% browCount(application); //Integer count = (Integer)application.getAttribute("count"); //if(count!=null){ //count++; //}else{ //count=1; //} //application.setAttribute("count",count); %> 在线访问次数:<%=application.getAttribute("brow") %> </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"> <%! public List<News> queryNewsInfo(String sql, Object []param){ List<News> list = new ArrayList<News>(); // 数据库驱动字符串 /*String driver = "oracle.jdbc.driver.OracleDriver"; // 连接URL字符串 String url = "jdbc:oracle:thin:@localhost:1521:oracle10"; // 数据库用户名 String user = "news"; // 用户密码 String password = "accp";*/ Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; // 获取连接并捕获异常 try { Context ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/news"); conn = ds.getConnection(); // 加载驱动 //Class.forName(driver); // 获取数据库连接 //conn = DriverManager.getConnection(url, user, password); pstmt = conn.prepareStatement(sql); if(param != null){ for(int i=0; i<param.length; i++){ pstmt.setObject(i+1,param[i]); } } rs = pstmt.executeQuery(); // 执行数据库命令 while(rs.next()) { News item = new News(); item.setNtitle(rs.getString(1)); item.setNauthor(rs.getString(2)); list.add(item); } } catch (NamingException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { // 若结果集对象不为空,则关闭 if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } // 若PreparedStatement对象不为空,则关闭 if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } // 若数据库连接对象不为空,则关闭 if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } return list; } %> <% String sql = "select ntitle, nauthor" + " from news" + " where ntid = 1" + " and rownum < 11" + " order by ncreatedate desc"; List<News> chinaInList = queryNewsInfo(sql,null); String sql1 = "select ntitle, nauthor" + " from news" + " where ntid = 2" + " and rownum < 11" + " order by ncreatedate desc"; List<News> chinaOutList = queryNewsInfo(sql1,null); %> <div class="sidebar"> <h1> <img src="Images/title_1.gif" alt="国内新闻" /> </h1> <div class="side_list"> <ul> <% for(News item : chinaInList){ %> <li> <a href='#'><b><%=item.getNtitle().length()>11?item.getNtitle().substring(0,11)+"...":item.getNtitle() %> </b></a> </li> <% } %> </ul> </div> <h1> <img src="Images/title_2.gif" alt="国际新闻" /> </h1> <div class="side_list"> <ul> <% for(News item : chinaOutList){ %> <li> <a href='#'><b><%=item.getNtitle().length()>11?item.getNtitle().substring(0,11)+"...":item.getNtitle() %></b></a> </li> <% } %> </ul> </div> <h1> <img src="Images/title_3.gif" alt="娱乐新闻" /> </h1> <div class="side_list"> <ul> <li> <a href='#'><b> 施瓦辛格启动影视业回迁计划 推进加州经济复苏 </b></a> </li> <li> <a href='#'><b> 《沧海》导演回应观众质疑 自信能超越《亮剑》 </b></a> </li> <li> <a href='#'><b> 《海角七号》导演新片开机 吴宇森等出席 </b></a> </li> <li> <a href='#'><b> 《四大名捕》敦煌热拍 八主演飙戏火花四溅 </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'> <a href='#'><b> 国内 </b></a> <a href='#'><b> 国际 </b></a> <a href='#'><b> 军事 </b></a> <a href='#'><b> 体育 </b></a> <a href='#'><b> 娱乐 </b></a> <a href='#'><b> 社会 </b></a> <a href='#'><b> 财经 </b></a> <a href='#'><b> 科技 </b></a> <a href='#'><b> 健康 </b></a> <a href='#'><b> 汽车 </b></a> <a href='#'><b> 教育 </b></a> </li> <li id='class_month'> <a href='#'><b> 房产 </b></a> <a href='#'><b> 家居 </b></a> <a href='#'><b> 旅游 </b></a> <a href='#'><b> 文化 </b></a> <a href='#'><b> 其他 </b></a> </li> </ul> <ul class="classlist"> <% NewsBiz newsBiz = new NewsBiz(); int pageSize = 10; int pageIndex = 1; int totalPage = newsBiz.getTotalPage(pageSize); String pageStr = request.getParameter("pageIndex"); if(pageStr != null){ if(!Pattern.matches("^\\d+$",pageStr)){ out.print("<script>alert('请求数据无效!');</script>"); }else{ int index = Integer.parseInt(pageStr); if(index < 1){ out.print("<script>alert('已经是第一页');</script>"); pageIndex = 1; }else if(index > totalPage){ out.print("<script>alert('已经是最后一页');</script>"); pageIndex = totalPage; }else{ pageIndex = index; } } } List<News> newsList = newsBiz.getNewsListByPage(pageIndex,pageSize); for(int i=0;i<newsList.size();i++){ News item = newsList.get(i); %> <li><a href='newspages/news_add.jsp?id=<%=item.getNid() %>'><%=item.getNtitle() %></a><span> <%=item.getNcreatedate() %> </span></li> <% if((i+1)%5==0){ %> <li class='space'></li> <% } } %> <p align="right"> 当前页数:[<%=pageIndex %>/<%=totalPage %>] <a href="index.jsp?pageIndex=1">首页</a> <a href="index.jsp?pageIndex=<%=pageIndex-1 %>">上一页</a> <a href="index.jsp?pageIndex=<%=pageIndex+1 %>">下一页</a> <a href="index.jsp?pageIndex=<%=totalPage%>">末页</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      <a href="#">常见问题解答</a>      新闻热线:010-627488888 <br /> 文明办网文明上网举报电话:010-627488888      举报邮箱: <a href="#">[email protected]</a> </p> <p class="copyright"> Copyright © 1999-2009 News China gov, All Right Reserver <br /> 新闻中国 版权所有 </p> </div> </body> </html>
admin.jsp
<%@page import="cn.jbit.biz.NewsBiz"%> <%@page import="cn.jbit.entity.News"%> <%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="utf-8"%> <%@ include file="element/control.jsp" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK" /> <title>添加主题--管理后台</title> <link href="CSS/admin.css" rel="stylesheet" type="text/css" /> </head> <body> <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">管理员: <% String str=""; //for(Cookie cookie : request.getCookies()){ //if(cookie.getName().equals("uName")){ //str=cookie.getValue(); //} //} str = session.getAttribute("uName").toString(); %> <%=str %> 登录      <a href="#">login out</a></div> <div id="channel"> </div> </div> <div id="main"> <div id="opt_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> </ul> </div> <div id="opt_area"> <script language="javascript"> function clickdel(){ return confirm("删除请点击确认"); } </script> <% List<News> list = new NewsBiz().getNewsList(); %> <ul class="classlist"> <% for(int i=0;i<list.size();i++){ News news = list.get(i); %> <li> <%=news.getNtitle() %> <span> 作者:<%=news.getNauthor() %>     <a href='news_edit.jsp?nid=<%=news.getNid() %>'>修改</a>      <a href='#' onclick='return clickdel()'>删除</a> </span> </li> <% if((i+1)%5==0){ %> <li class='space'></li> <% } %> <% } %> </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="#">[email protected]</a></p> <p class="copyright">Copyright © 1999-2009 News China gov, All Right Reserver<br /> 新闻中国 版权所有</p> </div> </body> </html>
control.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% if(session.getAttribute("uName")==null){ response.sendRedirect("index.jsp"); return; } %>
doComment.jsp
<%@page import="cn.jbit.biz.CommentBiz"%> <%@page import="cn.jbit.entity.Comment"%> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% request.setCharacterEncoding("UTF-8"); //获取数据 Comment comment = new Comment(); comment.setCauthor(request.getParameter("cauthor")); comment.setCip(request.getRemoteAddr()); comment.setCcontent(request.getParameter("ccontent")); comment.setCnid(request.getParameter("nid")); CommentBiz commentBiz = new CommentBiz(); if(commentBiz.addComment(comment) > 0){ out.print("<script>alert('添加评论成功!');location.href='../newspages/news_add.jsp?id="+comment.getCnid()+"';</script>"); return; }else{ out.print("<script>alert('添加失败!');history.back();</script>"); } %>
doLogin.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ page import="java.sql.*"%> <% request.setCharacterEncoding("utf-8"); String uName = request.getParameter("uname"); String pwd = request.getParameter("pwd"); String ms = ""; if (uName.equals("")) { ms = "用户名不能为空"; } else if (pwd.equals("")) { ms = "登录密码不能为空"; } response.setCharacterEncoding("utf-8"); if (!ms.equals("")) { out.print("<script>alert('" + ms + "');history.back();</script>"); return; } // 数据库驱动字符串 String driver = "oracle.jdbc.driver.OracleDriver"; // 连接URL字符串 String url = "jdbc:oracle:thin:@localhost:1521:oracle10"; // 数据库用户名 String user = "news"; // 用户密码 String password = "accp"; Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; // 获取连接并捕获异常 try { // 加载驱动 Class.forName(driver); // 获取数据库连接 conn = DriverManager.getConnection(url, user, password); String sql = "select uname, upwd from news_users where uname =? and upwd=?"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, uName); pstmt.setString(2, pwd); rs = pstmt.executeQuery(); // 执行数据库命令 if (rs.next()) { if (uName.trim().equals("admin")) { //管理员登录 //Cookie cookie = new Cookie("uName",uName); //cookie.setMaxAge(5*60); //response.addCookie(cookie); session.setAttribute("uName",uName); //request.getRequestDispatcher("../admin.jsp").forward(request,response); response.sendRedirect("../admin.jsp?uname=" + uName); }else{ //普通用户登 } } else { out.print("<script>alert('用户名或密码错误!');history.back();</script>"); return; } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { // 若结果集对象不为空,则关闭 if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } // 若PreparedStatement对象不为空,则关闭 if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } // 若数据库连接对象不为空,则关闭 if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } %>
news_edit.jsp
<%@page import="cn.jbit.entity.Topic"%> <%@page import="cn.jbit.biz.TopicBiz"%> <%@page import="cn.jbit.entity.News"%> <%@page import="cn.jbit.biz.NewsBiz"%> <%@page import="java.util.regex.Pattern"%> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK" /> <title>管理后台</title> <link href="CSS/admin.css" rel="stylesheet" type="text/css" /> </head> <body> <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">管理员: 登录      <a href="#">login out</a></div> <div id="channel"> </div> </div> <div id="main"> <div id="opt_list"> <ul> <li><a href="news_add.jsp">添加新闻</a></li> <li><a href="#">编辑新闻</a></li> <li><a href="#">查找新闻</a></li> <li><a href="topic_add.jsp">添加主题</a></li> <li><a href="topicList.jsp">编辑主题</a></li> </ul> </div> <div id="opt_area"> <h1 id="opt_type"> 修改新闻: </h1> <% request.setCharacterEncoding("utf-8"); String id = request.getParameter("nid"); if(id == null){ out.print("<script>alert('请求数据不存在!');history.back();</script>"); return; }else if(!Pattern.matches("^\\d+$",id)){ out.print("<script>alert('请求数据无效!');history.back();</script>"); return; } NewsBiz newsBiz = new NewsBiz(); News news = newsBiz.getNewsById(Integer.parseInt(id)); if(news == null){ out.print("<script>alert('请求数据不存在!');history.back();</script>"); return; } %> <form action="news_manage.jsp" method="post"> <p> <label> 编号 </label> <input name="nid" type="text" value=<%=news.getNid() %> readonly="readonly" class="opt_input"/> </p> <p> <label> 主题 </label> <select name="ntid"> <% TopicBiz topicBiz = new TopicBiz(); List<Topic> topicList = topicBiz.getTopicList(); for(int i = 0; i < topicList.size(); i++){ Topic topic = topicList.get(i); %> <option value=<%=topic.getTid() %> <% if(topic.getTid().equals(news.getNtid()+"")){ %> selected="selected" <% } %> > <%=topic.getTname() %> </option> <% } %> </select> </p> <p> <label> 标题 </label> <input name="ntitle" type="text" value=<%=news.getNtitle() %> class="opt_input"/> </p> <p> <label> 作者 </label> <input name="nauthor" type="text" value=<%=news.getNauthor() %> class="opt_input" /> </p> <p> <label> 摘要 </label> <textarea name="nsummary" cols="40" rows="3" > <%=news.getNsummary() %> </textarea> </p> <p> <label> 内容 </label> <textarea name="ncontent" cols="70" rows="10" > <%=news.getNcontent() %> </textarea> </p> <p> <label> 上传图片 </label> <input name="file" type="file" 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      <a href="#">常见问题解答</a>      新闻热线:010-627488888<br /> 文明办网文明上网举报电话:010-627488888      举报邮箱:<a href="#">[email protected]</a></p> <p class="copyright">Copyright © 1999-2009 News China gov, All Right Reserver<br /> 新闻中国 版权所有</p> </div> </body> </html>
news_add.jsp
<%@page import="cn.jbit.entity.Comment"%> <%@page import="cn.jbit.biz.CommentBiz"%> <%@page import="java.util.regex.Pattern"%> <%@page import="cn.jbit.entity.News"%> <%@page import="cn.jbit.biz.NewsBiz"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK" /> <title>新闻中国</title> <link href="../CSS/read.css" rel="stylesheet" type="text/css" /> <script type="text/javascript"> function check(){ var cauthor = document.getElementById("cauthor"); var content = document.getElementById("ccontent"); if(cauthor.value == ""){ alert("用户名不能为空!!"); return false; }else if(content.value == ""){ alert("评论内容不能为空!!"); return false; } return true; } </script> </head> <body> <div id="header"> <div id="top_login"> <label> 登录名 </label> <input type="text" id="uname" value="" class="login_input" /> <label> 密  码 </label> <input type="password" id="upwd" value="" class="login_input" /> <input type="button" class="login_sub" value="登录" onclick="login()"/> <label id="error"> </label> <a href="../index.jsp" class="login_link">返回首页</a> <img src="../Images/friend_logo.gif" alt="Google" id="friend_logo" /> </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> <li> <a href='#'><b> 重庆涉黑富豪黎强夫妇庭审答辩言辞相互矛盾 </b></a> </li> <li> <a href='#'><b> 发改委:4万亿投资计划不会挤占民间投资空间 </b></a> </li> <li> <a href='#'><b> 河南2个乡镇政绩报告内容完全一致引关注 </b></a> </li> </ul> </div> <h1> <img src="../Images/title_2.gif" alt="国际新闻" /> </h1> <div class="side_list"> <ul> <li> <a href='#'><b> 日本首相鸠山首次全面阐述新政府外交政策 </b></a> </li> <li> <a href='#'><b> 黎巴嫩以色列再次交火互射炮弹 </b></a> </li> <li> <a href='#'><b> 伊朗将于30日前就核燃料供应方案作出答复 </b></a> </li> <li> <a href='#'><b> 与基地有关组织宣称对巴格达连环爆炸负责 </b></a> </li> </ul> </div> <h1> <img src="../Images/title_3.gif" alt="娱乐新闻" /> </h1> <div class="side_list"> <ul> <li> <a href='#'><b> 施瓦辛格启动影视业回迁计划 推进加州经济复苏 </b></a> </li> <li> <a href='#'><b> 《沧海》导演回应观众质疑 自信能超越《亮剑》 </b></a> </li> <li> <a href='#'><b> 《海角七号》导演新片开机 吴宇森等出席 </b></a> </li> <li> <a href='#'><b> 《四大名捕》敦煌热拍 八主演飙戏火花四溅 </b></a> </li> </ul> </div> </div> <div class="main"> <div class="class_type"> <img src="../Images/class_type.gif" alt="新闻中心" /> </div> <div class="content"> <% request.setCharacterEncoding("utf-8"); String id = request.getParameter("id"); if(!Pattern.matches("^\\d+$",id)){ out.print("<script>alert('您请求的信息无效!');history.back();</script>"); return; } int nid = Integer.parseInt(id); NewsBiz newsBiz = new NewsBiz(); News news = newsBiz.getNewsById(nid); if(news==null){ out.print("<script>alert('您请求的信息不存在!');history.back();</script>"); return; } %> <ul class="classlist"> <table width="80%" align="center"> <tr width="100%"> <td colspan="2" align="center"><%=news.getNtitle() %></td> </tr> <tr> <td colspan="2"><hr /> </td> </tr> <tr> <td align="center"><%=news.getNcreatedate() %></td> <td align="left"><%=news.getNauthor() %> </td> </tr> <tr> <td colspan="2" align="center"></td> </tr> <tr> <td colspan="2"><%=news.getNcontent() %></td> </tr> <tr> <td colspan="2"><hr /> </td> </tr> </table> </ul> <ul class="classlist"> <table width="80%" align="center"> <% CommentBiz commentBiz = new CommentBiz(); List<Comment> commentList = commentBiz.getCommentList(nid); if(commentList.size() > 0){ for(Comment comment : commentList){ %> <table width="80%" align="center"> <tr> <td>留言人:<%=comment.getCauthor() %></td> <td>IP:<%=comment.getCip() %></td> <td>留言时间:<%=comment.getCdate() %></td> </tr> <tr> <td colspan="3"><%=comment.getCcontent() %></td> </tr> <tr> <td colspan="3"><hr/></td> </tr> </table> <% } }else{ %> <tr><td colspan="6"> 暂无评论! </td></tr> <% } %> <!--tr> <td colspan="6"><hr /> </td> </tr--> </table> </ul> <ul class="classlist"> <form action="../util/doComment.jsp" method="post" onsubmit="return check()"> <input type="hidden" name="nid" value="<%=nid %>"/> <table width="80%" align="center"> <tr> <td> 评 论 </td> </tr> <tr> <td> 用户名: </td> <td><input id="cauthor" name="cauthor" value="这家伙很懒什么也没留下"/> IP: <input name="cip" value="<%=request.getRemoteAddr() %>" readonly="readonly"/> </td> </tr> <tr> <td colspan="2"><textarea name="ccontent" cols="70" rows="10"></textarea> </td> </tr> <td><input name="submit" value="发 表" type="submit"/> </td> </table> </form> </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      <a href="#">常见问题解答</a>      新闻热线:010-627488888 <br /> 文明办网文明上网举报电话:010-627488888      举报邮箱: <a href="#">[email protected]</a> </p> <p class="copyright"> Copyright © 1999-2009 News China gov, All Right Reserver <br /> 新闻中国 版权所有 </p> </div> </body> </html>
效果图: