欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jsp实现页面分页功能代码

程序员文章站 2023-08-13 22:33:30
核心代码: <%@ page contenttype="text/html" pageencoding="gb2312" language="java"%&...

核心代码:

<%@ page contenttype="text/html" pageencoding="gb2312" language="java"%>
<%@ page import="java.sql.*"%>
<html>
	<head>
		<title>hello</title>
	</head>
	<body>
	<table border="1" spacing="2">
<%!
	public static final string driver = "com.mysql.jdbc.driver";
	public static final string user = "root";
	public static final string pass = "";
	public static final string url = "jdbc:mysql://localhost:3306/teachinfo";
	public static final int pagesize = 5;
	int pagecount;
	int curpage = 1;
%>
<%
	//一页放5个
	string user = null;
	string pass = null;
	try{
		class.forname(driver);
		connection con = drivermanager.getconnection(url,user,pass);
		string sql = "select * from department";
		preparedstatement stat = con.preparestatement(sql,resultset.type_forward_only,resultset.concur_read_only);
		resultset rs = stat.executequery();
		rs.last();
		int size = rs.getrow();
		pagecount = (size%pagesize==0)?(size/pagesize):(size/pagesize+1);
		string tmp = request.getparameter("curpage");
		if(tmp==null){
			tmp="1";
		}
		curpage = integer.parseint(tmp);
		if(curpage>=pagecount) curpage = pagecount;
		boolean flag = rs.absolute((curpage-1)*pagesize+1);
		out.println(curpage);//输出到屏幕上
		int count = 0;
		
		do{
			if(count>=pagesize)break;
			int departmentid = rs.getint(1);
			string departmentname = rs.getstring(2);
			count++;
			%>
		<tr>
			<td><%=departmentid%></td>
			<td><%=departmentname%></td>
			
		</tr>
			<%
		}while(rs.next());
		con.close();
	}
	catch(exception e){
		
	}
%>
</table>
<a href = "fenye.jsp?curpage=1" >首页</a>
<a href = "fenye.jsp?curpage=<%=curpage-1%>" >上一页</a>
<a href = "fenye.jsp?curpage=<%=curpage+1%>" >下一页</a>
<a href = "fenye.jsp?curpage=<%=pagecount%>" >尾页</a>
第<%=curpage%>页/共<%=pagecount%>页

</body>
</html>

本篇代码希望各位朋友喜欢!