方法一:mysql数据库分页 lt;% //定义每一页显示的记录 int pageSize = 3; String strPageNo = request.getParameter(pageNo)
姓名 |
薪水 |
<%
//连接数据库,加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection ct = DriverManager.getConnection
("jdbc:oracle:thin:@127.0.0.1:1521:ORCL","scott","tiger");
Statement sm = ct.createStatement();
//总共有多少页
int pageCount = 0;
//总共有多少行记录
int rowCount = 0;
//每页有3行记录
int pageSize = 3;
//接收pageNow
String s_pageNow = (String)request.getParameter("pageNow");
//当前在第一页
int pageNow = 1;
if(s_pageNow != null)
{
//把s_pageNow转化为数值型
pageNow = Integer.parseInt(s_pageNow);
}
//查询表中共有多少条记录
ResultSet rs = sm.executeQuery("select count(*) from emp");
if(rs.next())
{
rowCount = rs.getInt(1);
//如果整除就是商,否则就是商加上1 可以用三元表达式代替 rowCount%pageSize==0 ? rowCount/pageSize : rowCount/pageSize +1
if(rowCount%pageSize == 0)
{
pageCount = rowCount/pageSize;
}
else
{
pageCount = rowCount/pageSize +1;
}
}
//执行分页查询
rs = sm.executeQuery
("select * from (select a1.*,rownum rn from(select * from emp) a1 where rownum<="+pageNow*pageSize+")where rn>="+((pageNow-1)*pageSize+1)+" ");
while(rs.next())
{
out.println("
");
out.println("
"+rs.getString(2)+" | ");
out.println("
"+rs.getString(6)+" | ");
out.println("
");
}
//输出每一页要查找的连接
for(int i=1; i<=pageCount; i++)
{
out.print("["+i+"]");
}
%>