完整的新闻项目【JavaWeb】
前言:本项目主要用eclipse和orcl数据库进行搭建。重点是代码封装!
基本上是“换汤不换药”,代码大都“神似且形似”。
一、项目功能:
1-1:用户登录
1-2:用户注册
1-3:新闻页面的数据维护(添加、修改、删除、查询)
1-4:新闻主题页面的数据维护(添加、修改、删除)
1-5:阅读新闻页面的数据维护(添加评论、删除评论)
二、准备工作
1、在orcl数据库中创建 新闻表(news)、新闻主题表(subject)、用户表(User)、评论表(ptext)
2、打开eclipse新建一个web项目
3、在webroot下的WEB-INF下的lib中导入数据库的驱动jar包

4、创建对应的包结构
优化新闻管理系统(分层)
entity:新闻、主题、评论、用户
dao:新闻dao、主题dao、评论dao、用户dao
com.china.dao ——数据操作类
com.china.entity ——实体类(实体即抽象出来的用户对象,对应数据库中的User表,表中的每个字段在实体中为一个属性)
com.china.util ——帮助类

三、进入主题
帮助类:DBHelper
package com.china.util;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;public class DBHelper {//OracleDriver alt+?private static String cname = "oracle.jdbc.driver.OracleDriver";private static String url = "jdbc:oracle:thin:@localhost:1521:orcl";private static String user = "scott";private static String upwd = "tiger";//注册驱动类static {try {Class.forName(cname);} catch (Exception e) {e.printStackTrace();}}/*** 连接数据库* @return*/public static Connection getCon() {Connection con = null;try {con = DriverManager.getConnection(url, user, upwd);} catch (Exception e) {e.printStackTrace();}return con;}/*** 关闭连接* @param con* @param ps* @param rs*/public static void closeDB(Connection con,PreparedStatement ps,ResultSet rs) {try {if(con!=null) {con.close();}if(ps!=null) {ps.close();}if(rs!=null) {rs.close();}} catch (Exception e) {e.printStackTrace();}}/*** 下一个获取下一个编号的方法* @return 下一个编号*/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);//执行sql语句rs = ps.executeQuery();if(rs.next()) {id = rs.getInt(1)+1;}} catch (Exception e) {e.printStackTrace();}finally {DBHelper.closeDB(con, ps, rs);}return id;}
}
实体类(仅展示一个,其他类以此类推):News
package com.china.entity;
import java.sql.Date;public class News {private int nid;private int tid;private String ntitle;private String nzz;private String nnr;private Date ndate;private String nzy;private int nlook;private String nimg;public News() {super();}public News(int nid, int tid, String ntitle, String nzz, String nnr, Date ndate, String nzy, String nimg) {super();this.nid = nid;this.tid = tid;this.ntitle = ntitle;this.nzz = nzz;this.nnr = nnr;this.ndate = ndate;this.nzy = nzy;this.nimg = nimg;}public News(int tid, String ntitle, String nzz, String nnr, Date ndate, String nzy, int nlook, String nimg) {super();this.tid = tid;this.ntitle = ntitle;this.nzz = nzz;this.nnr = nnr;this.ndate = ndate;this.nzy = nzy;this.nlook = nlook;this.nimg = nimg;}public News(int tid, String ntitle, String nzz, String nnr, String nzy, String nimg) {super();this.tid = tid;this.ntitle = ntitle;this.nzz = nzz;this.nnr = nnr;this.nzy = nzy;this.nimg = nimg;}public News(String ntitle, String nzz, String nnr, String nzy, String nimg) {super();this.ntitle = ntitle;this.nzz = nzz;this.nnr = nnr;this.nzy = nzy;this.nimg = nimg;}public News(int nid, int tid, String ntitle, String nzz, String nnr, String nzy, String nimg) {super();this.nid = nid;this.tid = tid;this.ntitle = ntitle;this.nzz = nzz;this.nnr = nnr;this.nzy = nzy;this.nimg = nimg;}public News(int nid, int tid, String ntitle, String nzz, String nnr, Date ndate, String nzy, int nlook,String nimg) {super();this.nid = nid;this.tid = tid;this.ntitle = ntitle;this.nzz = nzz;this.nnr = nnr;this.ndate = ndate;this.nzy = nzy;this.nlook = nlook;this.nimg = nimg;}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 Date getNdate() {return ndate;}public void setNdate(Date ndate) {this.ndate = ndate;}public String getNzy() {return nzy;}public void setNzy(String nzy) {this.nzy = nzy;}public int getNlook() {return nlook;}public void setNlook(int nlook) {this.nlook = nlook;}public String getNimg() {return nimg;}public void setNimg(String nimg) {this.nimg = nimg;}@Overridepublic String toString() {return "News [nid=" + nid + ", tid=" + tid + ", ntitle=" + ntitle + ", nzz=" + nzz + ", nnr=" + nnr + ", ndate="+ ndate + ", nzy=" + nzy + ", nlook=" + nlook + ", nimg=" + nimg + "]";}}
数据操作类:NewsDao
package com.china.dao;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.sql.Date;import com.china.entity.News;
import com.china.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(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());// 执行sql语句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);//执行sql语句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=?,nimg=? 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.getNimg());//给占位符赋值i = ps.executeUpdate();} catch (Exception e) {e.printStackTrace();}finally {DBHelper.closeDB(con, ps, null);}return i;}//查询单个public News getByid(int xy) {News n = null;Connection con = null;PreparedStatement ps = null;ResultSet rs = null;try {//连接数据库con = DBHelper.getCon();ps = con.prepareStatement("select * from news where nid="+xy);//执行sql语句rs = ps.executeQuery();//处理结果if(rs.next()) {int nid = rs.getInt(1);int tid = rs.getInt(2);String ntitle = rs.getString(3);String nzz = rs.getString(4);String nnr = rs.getString(5);Date ndate = rs.getDate(6);int nlook = rs.getInt(7);String nzy = rs.getString(8);String nimg = rs.getString(9);//创建对象n = new News(nid, tid, ntitle, nzz, nnr, ndate, nzy, nlook, nimg);}} catch (Exception e) {e.printStackTrace();}finally {DBHelper.closeDB(con, ps, rs);}return n;}//分页查询:/*** 分页查询* @param pageIndex 页码* @param pageSize 每页数据条数* @return 查询到的集合*/ public ArrayList pageNews(int pageIndex,int pageSize){//声明对象ArrayList 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);//执行sql语句rs = ps.executeQuery();//处理结果while(rs.next()) {int nid = rs.getInt(1);int tid = rs.getInt(2);String ntitle = rs.getString(3);String nzz = rs.getString(4);String nnr = rs.getString(5);Date ndate = rs.getDate(6);int nlook = rs.getInt(7);String nzy = rs.getString(8);String nimg = rs.getString(9);//实例化对象News news = new News(nid, tid, ntitle, nzz, nnr, ndate, nzy, nlook, nimg);//把新闻对象放到集合中nlist.add(news);}} catch (Exception e) {e.printStackTrace();}finally {DBHelper.closeDB(con, ps, rs);}return nlist;}//最大页码方法/*** 求出最大页码* @param pageSize 每页数据条数* @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+"%'");//执行sql语句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 getMaxPageByTid(int pageSize,int tid) {//声明对象Connection con = null;PreparedStatement ps = null;ResultSet rs = null;int maxPage = 0;int count = 0;try {//连接数据库con = DBHelper.getCon();ps = con.prepareStatement("select count(*) from news where tid="+tid);//执行sql语句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 pageIndex 页码* @param pageSize 每页的数据条数* @param str 模糊查询的关键字* @return 返回查询到的数据*/public ArrayList pageLikeNews(int pageIndex,int pageSize,String str){//声明对象ArrayList 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);//执行sql语句rs = ps.executeQuery();//处理结果while(rs.next()) {int nid = rs.getInt(1);int tid = rs.getInt(2);String ntitle = rs.getString(3);String nzz = rs.getString(4);String nnr = rs.getString(5);Date ndate = rs.getDate(6);int nlook = rs.getInt(7);String nzy = rs.getString(8);String nimg = rs.getString(9);//实例化对象News news = new News(nid, tid, ntitle, nzz, nnr, ndate, nzy, nlook, nimg);//把新闻对象放到集合中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 pageNewsByTid(int pageIndex,int pageSize,int tid){//声明对象ArrayList 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);//执行sql语句rs = ps.executeQuery();//处理结果while(rs.next()) {int nid = rs.getInt(1);String ntitle = rs.getString(3);String nzz = rs.getString(4);String nnr = rs.getString(5);Date ndate = rs.getDate(6);int nlook = rs.getInt(7);String nzy = rs.getString(8);String nimg = rs.getString(9);//实例化对象News news = new News(nid, tid, ntitle, nzz, nnr, ndate, nzy, nlook, nimg);//把新闻对象放到集合中nlist.add(news);}} catch (Exception e) {e.printStackTrace();}finally {DBHelper.closeDB(con, ps, rs);}return nlist;}//根据新闻编号查询public ArrayList getAll(int jk){ArrayList nlist = new ArrayList<>();Connection con = null;PreparedStatement ps = null;ResultSet rs = null;try {//连接数据库con = DBHelper.getCon();ps = con.prepareStatement("select * from news where tid="+jk);rs = ps.executeQuery();while(rs.next()) {int tid = rs.getInt(1);int nid = rs.getInt(2);String ntitle = rs.getString(3);String nzz = rs.getString(4);String nnr = rs.getString(5);Date ndate = rs.getDate(6);int nlook = rs.getInt(7);String nzy = rs.getString(8);String nimg = rs.getString(9);//实例化一个对象News n = new News(nid, tid, ntitle, nzz, nnr, ndate, nzy, nlook, nimg);//将对象添加到集合中nlist.add(n);}} catch (Exception e) {e.printStackTrace();}finally {DBHelper.closeDB(con, ps, rs);}return nlist;}}
1、 用户登录界面效果图展示:(登录代码在上一章节有展示)

2、主页面:(admin.jsp代码在上一章节有展示)

3、阅读新闻页面:read_news.jsp
主要代码展示:
<%@page import="com.china.entity.User"%>
<%@page import="com.china.entity.Ptext"%>
<%@page import="com.china.dao.PtextDao"%>
<%@page import="com.china.entity.News"%>
<%@page import="com.china.dao.NewsDao"%>
<%@page import="java.util.ArrayList"%>
<%@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"%>
<%@taglib prefix="fc" uri="http://java.fckeditor.net" %>
新闻中国
<%//判断用户是不是为空Object obj = session.getAttribute("u");if(obj==null){out.print("");}
%>
返回后台
返回首页
<%//实例化一个NewsDao对象NewsDao ad = new NewsDao();ArrayList gss = ad.getAll(1);//遍历集合for(News x:gss){%>- <%=x.getNtitle() %>
<%} %>
<% //实例化NewsDao NewsDao ed = new NewsDao();ArrayList ljj = ed.getAll(2);for(News y:ljj){%>- <%=y.getNtitle() %>
<%} %>
<% ArrayList lxy=ed.getAll(5);for(News y:lxy){%>- <%=y.getNtitle() %>
<%} %>
<%// 获取新闻编号String id=request.getParameter("nid");int nid=Integer.valueOf(id);//实例化一个dao类NewsDao an=new NewsDao();News end=an.getByid(nid);if(end!=null){%><%=end.getNtitle() %>
<%=end.getNdate() %> <%=end.getNzz() %> <%=end.getNnr() %><% }%>
<%//查询新闻的评论PtextDao wan=new PtextDao();ArrayList ptx=wan.getPtextByid(nid);for(Ptext y:ptx){%>- <%=y.getPnr() %>删除回复
<%} %>
4、添加评论 and 删除评论(doaddpl.jsp and dodelpl.jsp)
重点代码展示:doaddpl.jsp
<%//设置编码格式request.setCharacterEncoding("utf-8");//接收评论人的编号(用户编号)int uuid = ((User)session.getAttribute("u")).getUuid();//强转//评论人的IPString pip = request.getParameter("cip");//接收新闻编号(给哪一篇新闻评论)int nid = Integer.valueOf(request.getParameter("nid"));session.setAttribute("nid", nid);//评论内容String pnr = request.getParameter("content");//实例化PtextDao对象PtextDao pd=new PtextDao();//实例化Ptext对象Ptext p = new Ptext(uuid,nid,pnr,pip);//调用添加评论方法int i = pd.addPl(p);if(i>0){out.print("");}else{out.print("");}
%>
dodel.jsp
<%//设置编码格式request.setCharacterEncoding("utf-8");//接收要评论编号int pid=Integer.valueOf(request.getParameter("pid"));//接收要删除的新闻编号Object id = session.getAttribute("nid");int nid = (Integer)id;//强转//实例化一个PtextDao类对象PtextDao pd = new PtextDao();//执行sql语句int i = pd.delete(pid);if(i>0){out.print("");}else{out.print("");}
%>
5、添加新闻:add_news.jsp doadd_news.jsp
重点代码展示:add_news.jsp
<%@page import="com.china.entity.User"%>
<%@page import="com.china.entity.Subject"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.china.dao.SubjectDao"%>
<%@page import="com.china.util.DBHelper"%>
<%@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"%>
添加新闻--管理后台
<%//判断用户是不是为空Object obj = session.getAttribute("u");if(obj==null){out.print("");}
%>
欢迎使用新闻管理系统!
管理员: <%=((User)session.getAttribute("u")).getUname() %> 登录 login out
添加新闻:
关于我们| Aboue Us| 联系我们| 广告服务| 供稿服务| 法律声明| 招聘信息| 网站地图| 留言反馈
24小时客户服务热线:010-68988888 常见问题解答 新闻热线:010-627488888
文明办网文明上网举报电话:010-627488888 举报邮箱:jubao@jb-aptech.com.cn
Copyright © 1999-2009 News China gov, All Right Reserver
新闻中国 版权所有
doadd_new.jsp
<%@page import="com.china.entity.News"%>
<%@page import="com.china.dao.NewsDao"%>
<%@page import="com.china.util.DBHelper"%>
<%@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(60000);//设置总文件大小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");//设置编码格式//request.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 n = new News(tid,ntitle,nzz,nzy,nnr,filePath);//实例化一个NewsDao类对象NewsDao nd = new NewsDao();//添加新闻--调用添加方法//执行sql语句int i = nd.addNews(n);if(i>0){out.print("");}else{out.print("");}%>
6、修改新闻:update_news.jsp doupdate_news.jsp
重点代码展示:update_news.jsp
<%@page import="com.china.entity.User"%>
<%@page import="com.china.entity.News"%>
<%@page import="com.china.dao.NewsDao"%>
<%@page import="com.china.entity.Subject"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.china.dao.SubjectDao"%>
<%@page import="com.china.util.DBHelper"%>
<%@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"%>
修改新闻--管理后台
<%//判断用户是不是为空Object obj = session.getAttribute("u");if(obj==null){out.print("");}
%>欢迎使用新闻管理系统!
管理员:<%=((User)session.getAttribute("u")).getUname() %> 登录 login out修改新闻:
<%//接收要修改的编号,把字符串转换为整数编号int xy = Integer.valueOf(request.getParameter("nid"));//实例化一个dao类对象NewsDao nd = new NewsDao();//实例化一个对象News n = new News();n = nd.getByid(xy);if(n!=null){%><%} %>关于我们| Aboue Us|联系我们| 广告服务|供稿服务| 法律声明|招聘信息| 网站地图|留言反馈24小时客户服务热线:010-68988888 常见问题解答 新闻热线:010-627488888
文明办网文明上网举报电话:010-627488888 举报邮箱:jubao@jb-aptech.com.cn
Copyright © 1999-2009 News China gov, All Right Reserver
新闻中国 版权所有
doupdate_news.jsp
<%@page import="com.china.dao.NewsDao"%>
<%@page import="com.china.entity.News"%>
<%@page import="com.china.util.DBHelper"%>
<%@page import="com.jspsmart.upload.Request"%>
<%@page import="com.jspsmart.upload.File"%>
<%@page import="com.jspsmart.upload.SmartUpload"%>
<%@page import="java.sql.Date"%>
<%@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(60000);//设置总文件大小su.setTotalMaxFileSize(80000);//设置编码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");//获取隐藏标签的值(分类编号)String id = req.getParameter("nid");int nid = Integer.valueOf(id);//获取修改页面的数据,把字符串转换为整数编号(新闻主题编号)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对象News y = new News(tid,ntitle,nzz,nzy,nnr,filePath);//实例化一个NewsDao类对象NewsDao nd = new NewsDao();//执行sql语句int i = nd.upNews(nid, y);if(i>0){out.print("");}else{out.print("");}
%>
7、删除新闻:dodelnews.jsp
<%@page import="com.china.dao.NewsDao"%>
<%@page import="com.china.entity.News"%>
<%@page import="com.china.util.DBHelper"%>
<%@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"%>
<%//接收要删除的新闻编号int nid = Integer.valueOf(request.getParameter("nid"));//实例化一个dao类对象NewsDao nd = new NewsDao();//执行sql语句int i = nd.delete(nid);if(i>0){out.print("");}else{out.print("");}%>
这就完了???对你没看错!其余添加主题、修改主题、删除主题都可以此类推 !
简简单单,轻轻松松!!!
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
