这是csdn保存的源码,如有需要,可自行下载:https://download.csdn.net/download/fzt12138/11776339
另外,根据各位下载后的反馈,系统中缺少jar包,现已补齐,下载地址为:https://download.csdn.net/download/fzt12138/12525349
如有需要,可自行下载,该系统为MySQL数据库,idea开发。
一、后台代码
①文件路径
src/com/news/bean/NewsComment.java
package com.news.bean;
import java.sql.Date;
public class NewsComment {
private int id;
private int newsid;
private String content;
private String author;
private Date createdate;
@Override
public String toString() {
return newsid +
"," + content+
"," + author+
"," + createdate;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getNewsid() {
return newsid;
}
public void setNewsid(int newsid) {
this.newsid = newsid;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Date getCreatedate() {
return createdate;
}
public void setCreatedate(Date createdate) {
this.createdate = createdate;
}
}
src/com/news/bean/NewsDetail.java
package com.news.bean;
import java.sql.Date;
public class NewsDetail {
private int id;
private String title;
private String summary;
private String author;
private Date createdate;
public String toString1() {
return title+
"," + summary+
"," + author +
"," + createdate;
}
@Override
public String toString() {
return "NewsDetail{" +
"id=" + id +
", title='" + title + '\'' +
", summary='" + summary + '\'' +
", author='" + author + '\'' +
", createdate=" + createdate +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Date getCreatedate() {
return createdate;
}
public void setCreatedate(Date createdate) {
this.createdate = createdate;
}
}
src/com/news/bean/PageBean.java
package com.news.bean;
public class PageBean {
private int currPage=1;
private int totalPage;
private int size=4;
private int count;
private String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getCurrPage() {
return currPage;
}
public void setCurrPage(int currPage) {
this.currPage = currPage;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
src/com/news/bean/PinLun.java
package com.news.bean;
import java.sql.Date;
public class PinLun {
private int id;
private Date createdate;
public Date getCreatedate() {
return createdate;
}
public void setCreatedate(Date createdate) {
this.createdate = createdate;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
private int newsid;
@Override
public String toString() {
return content+
"," + author;
}
public int getNewsid() {
return newsid;
}
public void setNewsid(int newsid) {
this.newsid = newsid;
}
private String content;
private String author;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
src/com/news/dao/impl/NewsDaoImpl.java
package com.news.dao.impl;
import com.news.bean.NewsDetail;
import com.news.bean.PageBean;
import com.news.dao.INewsDao;
import com.news.utils.JdbcUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import java.util.List;
public class NewsDaoImpl implements INewsDao {
@Override
public List<NewsDetail> selectLikePage(PageBean page) {
QueryRunner qr = new QueryRunner(JdbcUtil.getDS());
int startRow=(page.getCurrPage()-1)*page.getSize();
String sql="select * from news_detail where 1=1";
if(page.getTitle()!=null&&!page.getTitle().equals("")){
sql+=" and title like '%"+page.getTitle()+"%'";
}
sql+=" limit ?,?";
try {
return qr.query(sql,new BeanListHandler<NewsDetail>(NewsDetail.class),startRow,page.getSize());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public int selectCount() {
QueryRunner qr = new QueryRunner(JdbcUtil.getDS());
try {
return qr.query("select count(*) from news_detail",new ScalarHandler<Long>()).intValue();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
src/com/news/dao/impl/PinLunDaoImpl.java
package com.news.dao.impl;
import com.news.bean.PinLun;
import com.news.dao.IPinLunDao;
import com.news.utils.JdbcUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
public class PinLunDaoImpl implements IPinLunDao {
@Override
public List<PinLun> selectPinLun(String id) {
QueryRunner qr = new QueryRunner(JdbcUtil.getDS());
try {
return qr.query("select * from news_comment where newsid=?",new BeanListHandler<PinLun>(PinLun.class),id);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public int addPinLun(PinLun pinlun) {
QueryRunner qr = new QueryRunner(JdbcUtil.getDS());
Date date = new Date();
try {
return qr.update("insert into news_comment(newsid,content,author,createdate) values(?,?,?,?)",pinlun.getId(),pinlun.getContent(),pinlun.getAuthor(),date);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public int delete(String id) {
QueryRunner qr = new QueryRunner(JdbcUtil.getDS());
try {
qr.update("delete from news_comment where newsid=?",id);
return qr.update("delete from news_detail where id=?",id);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
src/com/news/dao/INewsDao.java
package com.news.dao;
import com.news.bean.NewsDetail;
import com.news.bean.PageBean;
import java.util.List;
public interface INewsDao {
List<NewsDetail> selectLikePage(PageBean page);
int selectCount();
}
src/com/news/dao/IPinLunDao.java
package com.news.dao;
import com.news.bean.PinLun;
import java.util.List;
public interface IPinLunDao {
List<PinLun> selectPinLun(String id);
int addPinLun(PinLun pinlun);
int delete(String id);
}
src/com/news/service/impl/NewsServiceImpl.java
package com.news.service.impl;
import com.news.bean.NewsDetail;
import com.news.bean.PageBean;
import com.news.dao.INewsDao;
import com.news.dao.impl.NewsDaoImpl;
import com.news.service.INewsService;
import java.util.List;
public class NewsServiceImpl implements INewsService {
private INewsDao dao=new NewsDaoImpl();
@Override
public List<NewsDetail> selectLikePage(PageBean page) {
int count=dao.selectCount();
int totalPage=(count%4==0)?(count/4):(count/4+1);
page.setCount(count);
page.setTotalPage(totalPage);
return dao.selectLikePage(page);
}
}
src/com/news/service/impl/PinLunServiceImpl.java
package com.news.service.impl;
import com.news.bean.PinLun;
import com.news.dao.IPinLunDao;
import com.news.dao.impl.PinLunDaoImpl;
import com.news.service.IPinLunService;
import java.util.List;
public class PinLunServiceImpl implements IPinLunService {
private IPinLunDao dao=new PinLunDaoImpl();
@Override
public List<PinLun> selectPinLun(String id) {
return dao.selectPinLun(id);
}
@Override
public int addPinLun(PinLun pinlun) {
return dao.addPinLun(pinlun);
}
@Override
public int delete(String id) {
return dao.delete(id);
}
}
src/com/news/service/INewsService.java
package com.news.service;
import com.news.bean.NewsDetail;
import com.news.bean.PageBean;
import java.util.List;
public interface INewsService {
List<NewsDetail> selectLikePage(PageBean page);
}
src/com/news/service/IPinLunService.java
package com.news.service;
import com.news.bean.PinLun;
import java.util.List;
public interface IPinLunService {
List<PinLun> selectPinLun(String id);
int addPinLun(PinLun pinlun);
int delete(String id);
}
src/com/news/utils/JdbcUtil.java
package com.news.utils;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.SqlDateConverter;
import javax.sql.DataSource;
import java.util.Map;
public class JdbcUtil {
//读取c3p0配置文件中的参数 四大参数
private static DataSource ds = new ComboPooledDataSource();
public static DataSource getDS() {
return ds;
}
static {
ConvertUtils.register(new SqlDateConverter(null), java.sql.Date.class);
ConvertUtils.register(new SqlDateConverter(null), java.util.Date.class);
}
public static <T> T mapToBean(Map map, Class<T> c) {
try {
T t = c.newInstance();
BeanUtils.populate(t, map);
return t;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
src/com/news/web/AddPinLunServlet.java
package com.news.web;
import com.news.bean.PinLun;
import com.news.service.IPinLunService;
import com.news.service.impl.PinLunServiceImpl;
import com.news.utils.JdbcUtil;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
public class AddPinLunServlet extends HttpServlet {
private IPinLunService service=new PinLunServiceImpl();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
Map<String, String[]> map = request.getParameterMap();
PinLun pinlun = JdbcUtil.mapToBean(map, PinLun.class);
int i=service.addPinLun(pinlun);
String msg=i==1?"新增成功!":"新增失败!";
request.getSession().setAttribute("msg",msg);
response.getWriter().write(msg);
response.sendRedirect("/News/show");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.getRequestDispatcher("/jsp/addPinLun.jsp").include(request,response);
// response.sendRedirect("/News/jsp/addPinLun.jsp");
}
}
src/com/news/web/DeleteServlet.java
package com.news.web;
import com.news.service.IPinLunService;
import com.news.service.impl.PinLunServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class DeleteServlet extends HttpServlet {
IPinLunService service=new PinLunServiceImpl();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String id = request.getParameter("id");
int i=service.delete(id);
HttpSession session = request.getSession();
String msg=i==1?"":"";
session.setAttribute("msg",msg);
response.getWriter().write(msg);
response.sendRedirect("/News/show");
}
}
src/com/news/web/showPinLunServlet.java
package com.news.web;
import com.news.bean.PinLun;
import com.news.service.IPinLunService;
import com.news.service.impl.PinLunServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
public class showPinLunServlet extends HttpServlet {
private IPinLunService service=new PinLunServiceImpl();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String id=request.getParameter("id");
List<PinLun> list=service.selectPinLun(id);
HttpSession session = request.getSession();
session.setAttribute("list",list);
request.getRequestDispatcher("/jsp/showPinLun.jsp").forward(request,response);
}
}
src/com/news/web/ShowServlet.java
package com.news.web;
import com.news.bean.NewsDetail;
import com.news.bean.PageBean;
import com.news.service.INewsService;
import com.news.service.impl.NewsServiceImpl;
import com.news.utils.JdbcUtil;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
public class ShowServlet extends HttpServlet {
private PageBean page=new PageBean();
private INewsService service=new NewsServiceImpl();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
Map<String, String[]> map = request.getParameterMap();
page = JdbcUtil.mapToBean(map, PageBean.class);
List<NewsDetail> list=service.selectLikePage (page);
request.getSession().setAttribute("list",list);
request.getSession().setAttribute("page",page);
request.getRequestDispatcher("/jsp/show.jsp").forward(request,response);
}
}
src/c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<!--默认配置-->
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/K9503?characterEncoding=utf-8</property>
<property name="user">root</property>
<property name="password">123456</property>
</default-config>
</c3p0-config>
图片
[外链图片转存失败(img-wF376FDS-1562068507980)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\1562067860616.png)]
js插件
[外链图片转存失败(img-cuw3bj8F-1562068507982)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\1562067886806.png)]
二、前端代码:
web/jsp/addPinLun.jsp
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2019/6/10
Time: 12:15
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
新闻管理系统
web/jsp/login.jsp
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2019/6/6
Time: 16:07
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
新闻管理系统