jsp+servlet+javabean+mysql实现分页。

想要做分页先熟悉数据库,咋们用mysql的sqlserver的以后弄。

看看效果

jsp+servlet+javabean+mysql实现分页。_第1张图片


1.连接数据库大家都应该会吧:分页mysql相对sqlserver简单,
分页mysql例如:

"select * from name limit "+m+","+n+""

; 其中m是指记录开始的index,从m开始,n表示几条记录
 2.创建分页的javabean


public class Page {
	private int totalPagecount=1;//总页数
	private  int pagesize=10;//每一页显示的记录数;
	private int totalcount=0;//记录总数;
	private int currpageno=1;//当前页码;
	List namesList;//实体集合
	public int getTotalPagecount() {
		return totalPagecount;
	}
	public void setTotalPagecount(int totalPagecount) {
		this.totalPagecount = totalPagecount;
	}
	public int getPagesize() {
		return pagesize;
	}
	public void setPagesize(int pagesize) {
		if(this.pagesize>0)
		this.pagesize = pagesize;
	}
	public int getTotalcount() {
		return totalcount;
	}
	public void setTotalcount(int totalcount) {
		if(totalcount>0)
		this.totalcount = totalcount;
		//计算总页数
		totalPagecount=totalcount%pagesize==0?(totalcount/pagesize)
				:totalcount/pagesize+1;
	}
	public int getCurrpageno() {
		return currpageno;
	}
	public void setCurrpageno(int currpageno) {
		this.currpageno = currpageno;
	}
	public List getNamesList() {
		return namesList;
	}
	public void setNamesList(List namesList) {
		this.namesList = namesList;
	}
	
}

3.创建对象实体

//我的对象就3条记录大家根据自己数据库创建
public class Name {
	private int id;
	private String name;
	private String img;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getImg() {
		return img;
	}
	public void setImg(String img) {
		this.img = img;
	}
	
}

4.下来应该从数据库查表多少条数据和显示的数据了

     4.1查询对少条记录的mysql

select count(*) as tc from name

     4.2连接数据库查找数据的dao如果不会连接数据库看这里    数据库连接

public class PageDao {
	private static ComboPooledDataSource datasource;
	private static Connection con;
	static {
		datasource=new ComboPooledDataSource();
		try {
		con=datasource.getConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
	/*
	 * 获取总条数
	 */
	public int getTotalCount() {
		String sql="select count(*) as tc from name";
		PreparedStatement ps;
		int totalcount = 0;
		try {
			ps = con.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while(rs.next()) {
				totalcount=rs.getInt("tc");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return totalcount;
	}
	/*
	 * pagesize//每一页显示的记录数
	 * pageno //页码
	 */
	public List getPageNameList(int pageno,int pageSize) throws SQLException{
		String sql="select * from name limit "+pageSize*(pageno-1)+","+pageSize+"";
		//从多少个显示,显示多少个
		PreparedStatement ps=con.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		List  listname = new ArrayList<>();
		while(rs.next()) {
			Name name=new Name();
			name.setId(rs.getInt("id"));
			name.setName(rs.getString("name"));
			name.setImg(rs.getString("image"));
			listname.add(name);
		}
		return listname;
	}
}

5,接下来去servlet里面去掉用对应的方法,这个方法已经把对象传给前面也面了。也面用el表达式获取值。

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import huomin.Dao.PageDao;
import huomin.entity.Name;
import huomin.entity.Page;
@WebServlet("/NewFile")
public class NewFileServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String currpageno = req.getParameter("page");
		String pageSize = req.getParameter("pageSize");
		Page p=new Page();
		if(pageSize!=null|| pageSize!="") {
			p.setPagesize(Integer.parseInt(pageSize));
		}
		System.out.println("3333333333333333333333333333=="+currpageno);
		if(currpageno==null||currpageno=="") {
			p.setCurrpageno(1);
		}else {
			p.setCurrpageno(Integer.parseInt(currpageno));
		}
		PageDao pd=new PageDao();
		System.out.println(pd.getTotalCount());
		p.setTotalcount((pd.getTotalCount()));
		List pageList=new ArrayList<>();
		try {
			pageList = pd.getPageNameList(p.getCurrpageno(), p.getPagesize());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// TODO Auto-generated method stub
		req.setAttribute( "Page",p);
		req.setAttribute( "pageList",pageList);
		req.getRequestDispatcher("/NewFile.jsp").forward(req,resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(req, resp);
	}

}

6,下来就是在jsp也面获取,我也面做得非常简单,不喜欢做前端功能基本都实现了





Insert title here
	



id name image
${pageList.id} ${pageList.name} ${pageList.img}
总共[${Page.totalcount}]记录,[${Page.totalPagecount}]页, ,每页显示 条 当前页码 [] ... []

;这就一个分页结束。

你可能感兴趣的:(java)