用PrepareStatement方法的纯JSP版分页(Mysql数据库)
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>JSP版分页</title>
</head>
<body>
<%! int pagecount; // pagecount为总页数
int pagesize=3;%> <%-- pagesize为每页显示的记录数 --%>
<% Connection con;
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost/qinshi","root","007");
String sql="select * from wowo order by id";
PreparedStatement ps=con.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs=ps.executeQuery();
rs.last();
int count=rs.getRow(); //count为记录总数
int i=0;
int pageI=1 ;
pagecount=(count%pagesize==0)?(count/pagesize):(count/pagesize+1);
%>
<%
String pages=request.getParameter("nowpage");
if(pages==null)
pages="1";
try{
pageI = Integer.parseInt(pages);
}
catch (NumberFormatException e){
pageI = 1;
}
if(pageI<0||pageI>pagecount)
{ pageI=pagecount;}
int position=(pageI-1)*pagesize +1;
rs.absolute(position);
%>
<table border="1" cellpadding="0" cellspacing="0"><tr>
<th>id</th>
<th>users</th> </tr>
<%
while(i<pagesize&&!rs.isAfterLast()){
%>
<tr>
<td><%=rs.getString(1)%></td>
<td><%=rs.getString(2)%></td>
</tr>
<%rs.next();i++;}%>
</table>
<form method="get" action="">
<table>
<tr>
<td><a href="fenye.jsp?nowpage=1">第一页</a></td>
<td><% if(pageI<pagecount){%>
<a href="fenye.jsp?nowpage=<%=pageI+1 %>">下一页</a><%}%></td>
<td><% if(pageI>1){%>
<a href="fenye.jsp?nowpage=<%=pageI-1 %>">上一页</a><%}%></td>
<td><a href="fenye.jsp?nowpage=<%=pagecount %>">尾页</a></td>
<td><input type="text" name="nowpage" /></td>
<td><input type="submit" name="submit" value="go"/></td></tr>
</table>
</form>
</body>
</html>