你可以把数据存放到session中,这样就可以很容易控制了
然后通过js实现分页,你可以在把当前页码存放到session中
当然,开始时当前页码为0,随着前后翻页不断改变当前页码
应该能够实现分页,我是用java实现的,这是我的代码
其中的newPageServlet是这样的
newPageServletpackage cn.dl.hx.servlet;import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2004-7-29
* Time: 9:18:15
* To change this template use File | Settings | File Templates.
*/
public class newPageServlet extends HttpServlet {
public void doPost(HttpServletRequest req,
HttpServletResponse resp)
{
try {
resp.sendRedirect("../jsp/mainMenu.jsp");
HttpSession session = req.getSession();
session.setAttribute("HIDDEN",req.getParameter("myhidden"));
session.setAttribute("COUNT",req.getParameter("myhidden"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
jsp页面
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<%@ page import="java.sql.ResultSet,
java.util.Vector"%>
<html>
<head>
<script language="javascript">
function nextPage()
{
hnumber = parseInt(document.page.myhidden.value);
max = parseInt(document.page.max.value);
if(hnumber+10<max)
{
document.page.myhidden.value=hnumber+10+"";
document.page.submit();
}
else
{
alert("This is the last page!");
return false;
}
}
function unload()
{
alert("Unload the window.ss");
}
function previousPage()
{
pnumber = parseInt(document.page.myhidden.value);
if(pnumber>=10)
{
document.page.myhidden.value=pnumber-10+"";
document.page.submit();
}
else
{
alert("This is the first page!");
return false;
}
}
function test()
{
document.page.previous.onclick=previousPage;
}</script>
</head>
<body>
<form method="POST" name="page" action="newPageServlet">
<table align="center" border="1" cellspacing="1" width="100%" >
<tr bgcolor="blue" ><td>Number</td><td>Name</td><td>Sex</td><td>Age</td><td>BirthDay</td></tr>
<%
Vector table = (Vector) session.getAttribute("STUDENT");
int count = Integer.parseInt(session.getAttribute("COUNT").toString());
for(int i = count ; i < count+10 ; i++)
{
if (i >= table.size()) break;
Vector row = (Vector) table.elementAt(i);
out.println("<tr><td>"+row.elementAt(0)+"</td><td>"+
row.elementAt(1)+"</td><td>"+row.elementAt(2)+
"</td><td>"+row.elementAt(3)+"</td><td>"+
row.elementAt(4)+"</td></tr>");
}
out.println("<input type=hidden name = myhidden value ="+String.valueOf(count)+">");
out.println("<input type=hidden name = max value ="+String.valueOf(table.size())+">");%>
</table>
<table width="%100">
<tr>
<td align="right" >
<img src="img/previous.gif" onclick="previousPage()" name="previous"
onmouseover="javascript:previous.style.cursor='hand'"
onmouseout="javascript:previous.style.cursor='default'">
<img src="img/next.gif" onclick="nextPage()" name="next"
onmouseover="javascript:next.style.cursor='hand'"
onmouseout="javascript:next.style.cursor='default'"></td></tr>
</table>
<form>
</body>
</html>
然后通过js实现分页,你可以在把当前页码存放到session中
当然,开始时当前页码为0,随着前后翻页不断改变当前页码
应该能够实现分页,我是用java实现的,这是我的代码
其中的newPageServlet是这样的
newPageServletpackage cn.dl.hx.servlet;import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2004-7-29
* Time: 9:18:15
* To change this template use File | Settings | File Templates.
*/
public class newPageServlet extends HttpServlet {
public void doPost(HttpServletRequest req,
HttpServletResponse resp)
{
try {
resp.sendRedirect("../jsp/mainMenu.jsp");
HttpSession session = req.getSession();
session.setAttribute("HIDDEN",req.getParameter("myhidden"));
session.setAttribute("COUNT",req.getParameter("myhidden"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
jsp页面
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<%@ page import="java.sql.ResultSet,
java.util.Vector"%>
<html>
<head>
<script language="javascript">
function nextPage()
{
hnumber = parseInt(document.page.myhidden.value);
max = parseInt(document.page.max.value);
if(hnumber+10<max)
{
document.page.myhidden.value=hnumber+10+"";
document.page.submit();
}
else
{
alert("This is the last page!");
return false;
}
}
function unload()
{
alert("Unload the window.ss");
}
function previousPage()
{
pnumber = parseInt(document.page.myhidden.value);
if(pnumber>=10)
{
document.page.myhidden.value=pnumber-10+"";
document.page.submit();
}
else
{
alert("This is the first page!");
return false;
}
}
function test()
{
document.page.previous.onclick=previousPage;
}</script>
</head>
<body>
<form method="POST" name="page" action="newPageServlet">
<table align="center" border="1" cellspacing="1" width="100%" >
<tr bgcolor="blue" ><td>Number</td><td>Name</td><td>Sex</td><td>Age</td><td>BirthDay</td></tr>
<%
Vector table = (Vector) session.getAttribute("STUDENT");
int count = Integer.parseInt(session.getAttribute("COUNT").toString());
for(int i = count ; i < count+10 ; i++)
{
if (i >= table.size()) break;
Vector row = (Vector) table.elementAt(i);
out.println("<tr><td>"+row.elementAt(0)+"</td><td>"+
row.elementAt(1)+"</td><td>"+row.elementAt(2)+
"</td><td>"+row.elementAt(3)+"</td><td>"+
row.elementAt(4)+"</td></tr>");
}
out.println("<input type=hidden name = myhidden value ="+String.valueOf(count)+">");
out.println("<input type=hidden name = max value ="+String.valueOf(table.size())+">");%>
</table>
<table width="%100">
<tr>
<td align="right" >
<img src="img/previous.gif" onclick="previousPage()" name="previous"
onmouseover="javascript:previous.style.cursor='hand'"
onmouseout="javascript:previous.style.cursor='default'">
<img src="img/next.gif" onclick="nextPage()" name="next"
onmouseover="javascript:next.style.cursor='hand'"
onmouseout="javascript:next.style.cursor='default'"></td></tr>
</table>
<form>
</body>
</html>
解决方案 »
- ie6下png背景消除完美办法?
- jsp留言板如何实何回复
- 购物系统支付功能求助
- jsp通过request向action传值
- 您的帮助是我学习最大的动力:我用WSAD写数据库连接的JAVABean出错!
- hibernate中属性能映射成nvarchar类型吗
- 怎样在一个jsp页面同时上传文件和传递参数啊?
- [200分,全部家产]用JSP如何控制扫描仪,而且把扫描仪扫描的图片传给jsp,jsp进行处理,最好给个例子,先谢了!急等
- 两应用服务器的数据传递
- expected at least 1 bean which qualifies as autowire candidate. 应该是没有创建bean?
- 送分谁不想要?
- 关于从库中读取信息的编码问题
这样查询呀!如果几百人同时访问,每人都要这么存200万,能不崩溃吗?
根据不同的数据库,应该采用不同的sql
oracle:select * from table where rownum<10 .....反正是运用rownum进行分页,好像要2重或3重嵌套,具体的忘了
mysql: select * from table where xxxx limit start,num
sqlserver:采用 2个select top 的嵌套查询 select top <pagesize> from (select top xx)不过简单的方法是用hibernate,那东西很好用 http://www.hibernate.org.cn/
{
Connection conn = null;
Statement stat = null;
ResultSet rs = null;
String aa[][] = new String[0][0];
try
{
conn = getDataSource().getConnection();
stat = conn.createStatement(1004,1007);
rs = stat.executeQuery(sql);
int rownum = 0;
int colnum=0; rs.last();
rownum = rs.getRow();
colnum = rs.getMetaData().getColumnCount();
if(rownum<start)
{
return aa;
}
else
{
if(rownum>=(start+each))
{
aa = new String[each][colnum];
}
else
{
aa = new String[rownum-start][colnum];
}
}
try
{
rs.absolute(start+1);
for (int i = 0; i < aa.length; i++)
{
for (int j = 0; j < colnum; j++)
{ aa[i][j] = rs.getString(j + 1);
}
rs.next();
}
}
catch(Exception e)
{
aa=new String[0][0];
}
rs.close();
stat.close();
conn.close();
rs = null;
stat = null;
conn = null;
}
catch (Exception e)
{
System.out.println("dbcon:executQuery error :" + e);
}
return aa;
}
public DataSource getDataSource()
{
DataSource ds = null;
try
{
InitialContext initialcontext = new InitialContext();
ds = (DataSource)initialcontext.lookup("OceanDataSource");
}
catch(NamingException namingexception)
{
System.out.println("获取数据连接池错误!");
namingexception.printStackTrace();
}
return ds;
}
你只需要在JSP中拼凑URL就可以了,分页的事都留给它去做了...
哎,这就难怪了,加上一大堆条件,查起来会很慢的!如果一次性查询要花多少时间?
~~~~~~~~~~~~~~~~~~~~~~~~~~为什么会很慢呀,曾经对6,7个上百万的表(最多600万)做联合查询,也就0.几秒,
对sql语句和数据库优化一下吧
sql语句花费8秒左右,取记录倒是挺快。
如你们所说,数据库该如何优化,sql语句是否还需优化,我每次分页查找都加入了rownum,
效率还是很低。
rs.absolute(posion);[定位]
for (int i=1;i<=pagesize;i++)[用这个显示记录]
http://www.getjob.com.cn/aboutus/downfiles/JSP/JSP-09.rar
querystr = "select * from (" + querystr1 + ") where r between "+pagestart+" and "+pageend;
pagestart和pageend通过页码计算出来
提示好象是游标是forward之类的错误
Connection conn=DriverManager.getConnection("jdbc:odbc:shop");
================注意这里==================
java.sql.Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
=============
String query="select * from sp";