Eclipse+Maven项目+分页显示MySQL中的数据

先看效果:

Eclipse+Maven项目+分页显示MySQL中的数据_第1张图片

本人数据库:数据库名bookclass, 列名有:BookClassID,BookClassMC。请大家修改对应的数据库

Eclipse+Maven项目+分页显示MySQL中的数据_第2张图片



下面开始教程:(项目名:Paging)

第一步:

1.在pom.xml中加入依赖:

    
      mysql
      mysql-connector-java
      5.1.22
    

2.写一个实体类entity 封装定义的要查询的字段:

package com.sve.dao;

public class entity {
	public static final int PAGE_SIZE = 5;//每页显示的条数 
	
	private int bookid;
	private String boookname;
	
	public int getBookid() {
		return bookid;
	}
	public void setBookid(int bookid) {
		this.bookid = bookid;
	}
	public String getBoookname() {
		return boookname;
	}
	public void setBoookname(String boookname) {
		this.boookname = boookname;
	}
}

第二步:

写一个dao类 PagDao:

package com.sve.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class PagDao {
	public static void main(String [] args) throws SQLException {
		List listentitys = new PagDao().find(1);
	}
	
	public Connection getConnection() throws SQLException {
		Connection conn = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url = "jdbc:mysql://localhost:3306";
			String username = "root";
			String password = "******";
			conn = DriverManager.getConnection(url,username,password);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}
	
	public List find(int page) throws SQLException{
		List list = new ArrayList();
		Connection conn = getConnection();
		String sql = "select * from library.bookclass order by BookClassID asc limit ?,?";
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setInt(1, (page-1)*entity.PAGE_SIZE);
			ps.setInt(2, entity.PAGE_SIZE);
			ResultSet rs = ps.executeQuery();
			while(rs.next()) {
				entity e = new entity();
				e.setBookid(rs.getInt("BookClassID"));
				e.setBoookname(rs.getString("BookClassMC"));
				list.add(e);
			}
			rs.close();
			ps.close();
			conn.close();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return list;
	}
	
	public int findCount() throws SQLException {
		int count = 0;
		Connection conn = getConnection();
		String sql = "select count(*) from library.bookclass";
		Statement sm = (Statement)conn.createStatement();
		ResultSet rs = sm.executeQuery(sql);
		if(rs.next()) {
			count = rs.getInt(1);
		}
		rs.close();
		conn.close();
		
		return count;
	}
	
}

第三步:

写一个servlet类:

public class PagServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public PagServlet() {
    	super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//当前页
		int currPage = 1;
		//判断页面是否有效
		if(request.getParameter("page") != null) {
			currPage = Integer.parseInt(request.getParameter("page"));
		}
		PagDao dao = new PagDao();

		try {
			//查询所有书籍
			List list = dao.find(currPage);
			request.setAttribute("list", list);
			//总页数
			int pages;
			//总记录数
			int count = dao.findCount();
			//总页数
			if(count % entity.PAGE_SIZE == 0) {
				pages = count / entity.PAGE_SIZE;
			}else {
				pages = count / entity.PAGE_SIZE+1;
			}
			
			StringBuffer sb = new StringBuffer();
			//
			for(int i = 1;i <= pages;i++) {
				//是否当前页
				if(i==currPage) {
					//构建分页
					sb.append("【"+i+"】");
				}else {
					sb.append(""+i+"");
				}
				sb.append(" ");
			}
			request.setAttribute("bar", sb.toString());
			request.getRequestDispatcher("entity_list.jsp").forward(request, response);
		} catch (SQLException e) {
		// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}


第四步:


新建一个index.jsp (模仿功能)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here


	查看所有书籍


第五步:

1.新建一个entity_list.jsp 显示数据页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.util.List,com.sve.dao.*" %>




所有书籍
  


	  
    <% 
    	List list = (List)request.getAttribute("list");
    	for(entity e : list){
    %>
      
    <%} %>
    

所有商品信息

ID 书籍名称
<%=e.getBookid()%> <%=e.getBoookname()%>
<%=request.getAttribute("bar")%>

2.web.xml




  Archetype Created Web Application
  
  	PagServlet
  	PagServlet
  	
  	com.sve.servlet.PagServlet
  
  
  	PagServlet
  	/PagServlet
  




最后:http://localhost:8080/Paging/index.jsp


你可能感兴趣的:(Eclipse+Maven项目+分页显示MySQL中的数据)