1。整个新闻发布系统全架构
2。实现登陆
public boolean isLogin(UserInfo info) throws Exception { getConection(); boolean flag=false; String sql="select count(1)as con from userinfo where uname=? and upwd=?"; Object[]paras={info.getUname(),info.getUpwd()}; rs=executeQuery(sql,paras); if(rs.next()){ int count=rs.getInt("con"); if(count>0){ flag=true; } } return flag; } }
serlvet层代码:
//解决乱码 request.getCharacterEncoding(); //解析 String uname=request.getParameter("uname"); String upwd=request.getParameter("upwd"); UserInfo info=new UserInfo(); info.setUname(uname); info.setUpwd(upwd); IUserInfoService service=new UserInfoServiceImpl(); try{ boolean flag=service.isLogin(info); if(flag){ request.getSession().setAttribute("uname", uname); request.getRequestDispatcher("/newspages/admin.jsp").forward(request, response); }else{ //request.getRequestDispatcher("/index.jsp").forward(request, response); response.sendRedirect("/NewsManagerSystem/index.jsp"); } }catch(SQLException e){ e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }else{ request.getRequestDispatcher("/index.jsp").forward(request, response); } } }
3.实现注销
String action=request.getParameter("action"); if("logout".equals(action)){ request.getSession().removeAttribute("uname"); //response.sendRedirect("/NewsManagerSystem/index.jsp"); request.getRequestDispatcher("/index.jsp").forward(request, response);
4.显示新闻列表
public List<NewsInfo> getAllNews() throws Exception { getConection(); List<NewsInfo>list=new ArrayList<NewsInfo>(); String sql="select * from newsinfo"; rs=executeQuery(sql); while(rs.next()){ NewsInfo info=new NewsInfo(); info.setNtitle(rs.getString("ntitle")); info.setNcreateda(rs.getDate("ncreateda")); list.add(info); } return list; }
5.编辑主题
public List<Topic> getAllTopics() throws Exception { List<Topic> list=new ArrayList<Topic>(); getConection(); String sql="select * from topic"; ResultSet rs= executeQuery(sql); while (rs.next()) { Topic topic=new Topic(); topic.setTid(rs.getInt("tid")); topic.setTname(rs.getString("tname")); list.add(topic); } return list; }
6.添加主题
public boolean addTopic(Topic topic) throws SQLException { getConection(); boolean flag=false; String sqlString="insert into topic values(?,?)"; int count=0; try { count=exeuteUpdate(sqlString,topic.getTid(),topic.getTname()); if (count>0) { flag=true; } } catch (Exception e) { e.printStackTrace(); } return flag; }
7.分页显示
public List<NewsInfo> getOnePageData(int pageindex, int pageSize) throws Exception{ List<NewsInfo>list=new ArrayList<NewsInfo>(); String sql="select * from newsinfo limit ?,?"; Object[]paras={(pageindex-1)*pageSize,pageSize}; getConection(); rs=executeQuery(sql,paras); while(rs.next()){ NewsInfo info=new NewsInfo(); info.setNtitle(rs.getString("ntitle")); info.setNcreateda(rs.getDate("ncreateda")); list.add(info); } return list; }
以上就是我的新闻发布系统大概进程
增删改还没有写