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

MySQL和Oracle数据库中的分页查询

程序员文章站 2022-06-08 18:47:07
...

方法一: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-1)*pageSize+1)+" ");

while(rs.next())

{

out.println("

");

out.println("

");

out.println("

");

out.println("

");

}

//输出每一页要查找的连接

for(int i=1; i

{

out.print("["+i+"]");

}

%>

姓名 薪水
"+rs.getString(2)+" "+rs.getString(6)+"