如何实现高效的 jsp 分页显示功能(数据库是sqlserver),最好有实例;
展开全部
//下面是带数据库连接池的分页beans,比较通用
package xcfi.utily;
import java.io.*;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
public class common_pages{
Context ctx=null;
Connection cnn=null;
Statement stmt=null;
ResultSet rs=null;
ResultSet rs1=null;
public void close_pages(){
try
{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(cnn!=null)
cnn.close();
if(ctx!=null)
ctx.close();
}catch(Exception e){System.out.println(e.toString());}
}
public ResultSet get_pages(String table_name,String item_name,int current_page,int page_size){
try
{
ctx=new InitialContext();
if(ctx==null)
throw new Exception("没有匹配的环境");
DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/my_mysql");
if(ds==null)
throw new Exception("没有匹配数据库");
cnn=ds.getConnection();
int from_no=0;
int end_no=0;
from_no=(current_page-1)*page_size;
end_no=page_size;
String sql="select * from "+table_name+" order by "+item_name+" desc limit "+Integer.toString(from_no)+","+Integer.toString(end_no);
stmt=cnn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(sql);
}
catch(Exception E){System.out.println(E.toString());}
return rs;
}
public ResultSet get_pages_with_count(String table_name,String item_name,int current_page,int page_size){
try
{
ctx=new InitialContext();
if(ctx==null)
throw new Exception("没有匹配的环境");
DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/my_mysql");
if(ds==null)
throw new Exception("没有匹配数据库");
cnn=ds.getConnection();
int from_no=0;
int end_no=0;
int count=0;
from_no=(current_page-1)*page_size;
end_no=page_size;
String sql="select count(*) as count from "+table_name;
stmt=cnn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs1=stmt.executeQuery(sql);
if(rs1.next()){
count=rs1.getInt("count");
}
rs1.close();
sql="select *,"+count+" as count from "+table_name+" order by "+item_name+" desc limit "+Integer.toString(from_no)+","+Integer.toString(end_no);
stmt=cnn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(sql);
}
catch(Exception E){System.out.println(E.toString());}
return rs;
}
}
package xcfi.utily;
import java.io.*;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
public class common_pages{
Context ctx=null;
Connection cnn=null;
Statement stmt=null;
ResultSet rs=null;
ResultSet rs1=null;
public void close_pages(){
try
{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(cnn!=null)
cnn.close();
if(ctx!=null)
ctx.close();
}catch(Exception e){System.out.println(e.toString());}
}
public ResultSet get_pages(String table_name,String item_name,int current_page,int page_size){
try
{
ctx=new InitialContext();
if(ctx==null)
throw new Exception("没有匹配的环境");
DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/my_mysql");
if(ds==null)
throw new Exception("没有匹配数据库");
cnn=ds.getConnection();
int from_no=0;
int end_no=0;
from_no=(current_page-1)*page_size;
end_no=page_size;
String sql="select * from "+table_name+" order by "+item_name+" desc limit "+Integer.toString(from_no)+","+Integer.toString(end_no);
stmt=cnn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(sql);
}
catch(Exception E){System.out.println(E.toString());}
return rs;
}
public ResultSet get_pages_with_count(String table_name,String item_name,int current_page,int page_size){
try
{
ctx=new InitialContext();
if(ctx==null)
throw new Exception("没有匹配的环境");
DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/my_mysql");
if(ds==null)
throw new Exception("没有匹配数据库");
cnn=ds.getConnection();
int from_no=0;
int end_no=0;
int count=0;
from_no=(current_page-1)*page_size;
end_no=page_size;
String sql="select count(*) as count from "+table_name;
stmt=cnn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs1=stmt.executeQuery(sql);
if(rs1.next()){
count=rs1.getInt("count");
}
rs1.close();
sql="select *,"+count+" as count from "+table_name+" order by "+item_name+" desc limit "+Integer.toString(from_no)+","+Integer.toString(end_no);
stmt=cnn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(sql);
}
catch(Exception E){System.out.println(E.toString());}
return rs;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<%@ 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>分页</title>
</head>
<body>
<%
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=pubs";
Connection con=DriverManager.getConnection(url,"sa","sql");
int i=1;
int numPages=5;
String pages = request.getParameter("page");
int currentPage = 1;
int intPageCount=1;
currentPage=((pages==null)? 1 :(Integer.parseInt(pages)));
if (currentPage<1)
currentPage=1;
String sql = "select count(au_id) as id from authors";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//
if(rs.next())
intPageCount = rs.getInt("id");
rs.close();
int PageCount=(intPageCount%numPages==0)?(intPageCount/numPages):(intPageCount/numPages+1);
int nextPage;
int upPage;
nextPage = currentPage+1;
if (nextPage>=PageCount)
nextPage=PageCount;
upPage = currentPage-1;
if (upPage<=1)
upPage=1;
sql="select au_fname,contract,zip from authors";
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(sql);
i=0;
while((i<numPages*(currentPage-1)) &&rs.next())
{
i++;
}
while((i<numPages*currentPage)&&rs.next())
{
i++;
out.println("au_fname:"+rs.getString("au_fname")+" contract"+rs.getString("contract")+" zip:"+rs.getString("zip")+"<br/>");
}
rs.close();
stmt.close(); //(4)关闭
con.close();
%><p></p>合计:<%=currentPage%>/<%=PageCount%>
<a href="fy.jsp?page=1">第一页</a><a href="fy.jsp?page=<%=upPage%>">上一页</a>
<%
for(int j=1;j<=PageCount;j++)
{
if(currentPage!=j)
{
%> <a href="fy.jsp?page=<%=j%>">[<%=j%>]</a> <%
}
else
{
out.println(j);
}
} %>
<a href="fy.jsp?page=<%=nextPage%>">下一页</a><a href="fy.jsp?page=<%=PageCount%>">最后页 </a>
<%
}
catch(Exception e)
{
e.printStackTrace();
}
%>
</body>
</html>
<!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>分页</title>
</head>
<body>
<%
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=pubs";
Connection con=DriverManager.getConnection(url,"sa","sql");
int i=1;
int numPages=5;
String pages = request.getParameter("page");
int currentPage = 1;
int intPageCount=1;
currentPage=((pages==null)? 1 :(Integer.parseInt(pages)));
if (currentPage<1)
currentPage=1;
String sql = "select count(au_id) as id from authors";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//
if(rs.next())
intPageCount = rs.getInt("id");
rs.close();
int PageCount=(intPageCount%numPages==0)?(intPageCount/numPages):(intPageCount/numPages+1);
int nextPage;
int upPage;
nextPage = currentPage+1;
if (nextPage>=PageCount)
nextPage=PageCount;
upPage = currentPage-1;
if (upPage<=1)
upPage=1;
sql="select au_fname,contract,zip from authors";
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(sql);
i=0;
while((i<numPages*(currentPage-1)) &&rs.next())
{
i++;
}
while((i<numPages*currentPage)&&rs.next())
{
i++;
out.println("au_fname:"+rs.getString("au_fname")+" contract"+rs.getString("contract")+" zip:"+rs.getString("zip")+"<br/>");
}
rs.close();
stmt.close(); //(4)关闭
con.close();
%><p></p>合计:<%=currentPage%>/<%=PageCount%>
<a href="fy.jsp?page=1">第一页</a><a href="fy.jsp?page=<%=upPage%>">上一页</a>
<%
for(int j=1;j<=PageCount;j++)
{
if(currentPage!=j)
{
%> <a href="fy.jsp?page=<%=j%>">[<%=j%>]</a> <%
}
else
{
out.println(j);
}
} %>
<a href="fy.jsp?page=<%=nextPage%>">下一页</a><a href="fy.jsp?page=<%=PageCount%>">最后页 </a>
<%
}
catch(Exception e)
{
e.printStackTrace();
}
%>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用sql分页。
select top 10 from tablename where id not in (select top 10 id from tablename)
一般的分页是一次性把数据库中数据全部取出来,在页面进行分页。
mssql 可以通过top关键字来实现查询的时候就只查显示的部分。
select top 10 from tablename where id not in (select top 10 id from tablename)
一般的分页是一次性把数据库中数据全部取出来,在页面进行分页。
mssql 可以通过top关键字来实现查询的时候就只查显示的部分。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
最高效率的分页是在数据库中使用存储过程做分页
分页的例子网上很多的,多找找看
有个ecside的插件很好用的,去试试看,作者还有使用视频的
分页的例子网上很多的,多找找看
有个ecside的插件很好用的,去试试看,作者还有使用视频的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
分页和jsp有什么关系?
主要和SQL
有关。
根据查询条件优化,加索引之类的。
查询的时候尽量走索引,like
、in
之类不要用,可以用其他方式替换。
主要和SQL
有关。
根据查询条件优化,加索引之类的。
查询的时候尽量走索引,like
、in
之类不要用,可以用其他方式替换。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询