一、问题描述
将所有类别的新闻导入数据库中。以树形目录形式展示新闻类别,每个树形节点代表新闻分类,选择每个新闻,以列表形式显示新闻标题,点击新闻标题,可以查看详细信息。
二、实现
1、将excel导入到navicat中
2、在eclispe中编写代码:
实体层封装新闻:
1 public class Passage { 2 private int id; 3 private String content; 4 private String channelName; 5 private String title; 6 public String getContent() { 7 return content; 8 } 9 public void setContent(String content) { 10 this.content = content; 11 } 12 public String getChannelName() { 13 return channelName; 14 } 15 public void setChannelName(String channelName) { 16 this.channelName = channelName; 17 } 18 public int getId() { 19 return id; 20 } 21 public void setId(int id) { 22 this.id = id; 23 } 24 public String getTitle() { 25 return title; 26 } 27 public void setTitle(String title) { 28 this.title = title; 29 } 30 public Passage(int id,String content, String channelName, String title) { 31 super(); 32 this.id=id; 33 this.content = content; 34 this.channelName = channelName; 35 this.title = title; 36 } 37 public Passage() { 38 super(); 39 // TODO 自动生成的构造函数存根 40 } 41 42 43 }
Dao层实现查找新闻:
public class passageDao { /** * 获取总记录数和总页数 * @param count * @return */ public static int[] totalPage(int count, String keyword,String type) { // 0 总记录数, 1, 页数 int arr[] = {0, 1}; Connection conn = Basedao.getconn(); PreparedStatement ps = null; ResultSet rs = null; try { String sql = ""; if(keyword!=null) { sql = "select count(*) from "+type+" where id like ?"; ps = conn.prepareStatement(sql); ps.setString(1, "%"+keyword+"%"); }else{ sql = "select count(*) from "+type; ps = conn.prepareStatement(sql); } rs = ps.executeQuery(); while(rs.next()) { arr[0]= rs.getInt(1); if(arr[0]%count==0){ arr[1] = arr[0]/count; }else{ arr[1] = arr[0]/count+1; } } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ Basedao.closeall(rs, ps, conn); } return arr; } public static ArrayList<Passage> selectAll(int cpage, int count,String keyword,String type){ ArrayList<Passage> list = new ArrayList<Passage>(); //声明结果集 ResultSet rs = null; //获取连接对象 Connection conn = Basedao.getconn(); PreparedStatement ps = null; try { String sql = ""; if(keyword!=null) { sql = "select * from "+type+" where id like ? limit ?, ?"; ps = conn.prepareStatement(sql); ps.setString(1, "%"+keyword+"%"); ps.setInt(2, (cpage-1)*count); ps.setInt(3, count); }else{ sql = "select * from "+type+" limit ?, ?"; ps = conn.prepareStatement(sql); ps.setInt(1, (cpage-1)*count); ps.setInt(2, count); } rs = ps.executeQuery(); while(rs.next()) { Passage u = new Passage( rs.getInt("id"), rs.getString("content"), rs.getString("channelName"), rs.getString("title") ); list.add(u); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { Basedao.closeall(rs, ps, conn); } return list; } /** * 通过ID查找用户 * @param cpage * @param count * @param keyword * @return */ public static String selectById(int id,String type) { Passage u = null; //声明结果集 ResultSet rs = null; //获取连接对象 Connection conn = Basedao.getconn(); PreparedStatement ps = null; String content = null; try { String sql = "select content from "+type+" where id=?"; //执行 ps = conn.prepareStatement(sql); //传id ps.setInt(1, id); rs = ps.executeQuery(); while(rs.next()) { content=rs.getString("content"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { Basedao.closeall(rs, ps, conn); } return content; } }
Servlet层实现界面与后台交互:
1 public class select extends HttpServlet { 2 /** 3 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 4 */ 5 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 6 // TODO Auto-generated method stub 7 int cpage = 1; //当前页 8 int count = 15; //每页显示条数 9 //获取用户指定的页面 10 String cp = request.getParameter("cp"); 11 String type=request.getParameter("type"); 12 //接收用户搜索的关键字 13 String keyword = request.getParameter("keywords"); 14 if(cp!=null) 15 { 16 cpage = Integer.parseInt(cp); 17 } 18 int arr[] = passageDao.totalPage(count, keyword,type); 19 //获取所有用户记录 20 ArrayList<Passage> list= passageDao.selectAll(cpage, count, keyword,type); 21 ArrayList<Passage> json; 22 for(int i=0;i<list.size();i++) { 23 JSONObject ob=new JSONObject(); 24 ob.put("title", list.get(i).getTitle()); 25 System.out.println(list.get(i).getTitle()); 26 } 27 //放到请求对象域里 28 request.setAttribute("type", type); 29 request.setAttribute("userlist", list); 30 request.setAttribute("tsum", arr[0]); 31 request.setAttribute("tpage", arr[1]); 32 request.setAttribute("cpage", cpage); 33 if(keyword != null) { 34 request.setAttribute("searchParams", "&keywords="+keyword); 35 } 36 request.getRequestDispatcher("caijing.jsp").forward(request, response); 37 } 38 39 40 }
页面调用框架:
三、结果展示
实现分页:
点击查看可看到文章内容: