谁有jsp分页的例子啊 发给我啊 邮箱:[email protected] 最好是连接数据库的啊 感谢
解决方案 »
- MyEclipse下运行Tomcat6 的 Comet框架
- java中jacob调用ocx出错!!!
- getJdbcTemplate 获取插入数据的行号
- 关于在websphere上部署axis2的问题
- 请问高手,关于用JS同时拖拽多个图片的问题
- 请问导入JSP的问题?
- tomcat 使用了开发模式,依然要重启服务器才能生效
- tomcat 配置问题,解决就给分!
- 请问!JBUILDER X下配置struts1.1的DataSource数据源详细配置方法
- 利用ajax从jsp页面向Action传值的一个错误,求解惑...
- 集结Struts2方面的朋友
- 什么OpenSource软件可以import a javaProject,自动给出a Class/Relation diagram?
<%@page contentType="text/html;charset=gb2312"%>
<%@page import="java.sql.*"%>
<HTML><BODY>
<%
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String strSQL = "";
int PageSize = 5;//没页显示的数量
int Page = 1;
int totalPage = 1;
int totalrecord = 0;//总记录数
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException ce){
out.println(ce.getMessage());
}
try{
conn=DriverManager.getConnection("jdbc:odbc:grade");
stmt=conn.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
//输出记录
strSQL = "SELECT * FROM grade";
rs = stmt.executeQuery(strSQL);
//算出总行数
if (rs.next()){
rs.last();
totalrecord = rs.getRow();//得到记录集的总记录数(总行数);
rs.first();
}
if(totalrecord % PageSize ==0)// 如果是当前页码的整数倍
totalPage = totalrecord / PageSize;
else // 如果最后还空余一页
totalPage = (int) Math.floor( totalrecord / PageSize ) + 1;
if(totalPage == 0) totalPage = 1;
if(request.getParameter("Page")==null || request.getParameter("Page").equals(""))
Page = 1;
else
try {
Page = Integer.parseInt(request.getParameter("Page"));
}
catch(java.lang.NumberFormatException e){
// 捕获用户从浏览器地址拦直接输入Page=sdfsdfsdf所造成的异常
Page = 1;
}
if(Page < 1) Page = 1;
if(Page > totalPage) Page = totalPage;
rs.absolute((Page-1) * PageSize + 1);
out.print("<TABLE BORDER='1'>");
for(int iPage=1; iPage<=PageSize; iPage++)
{
out.print("<TR><TD>"+rs.getString("学号")+"</TD>");
out.print("<TD>"+rs.getString("姓名")+"</TD>");
out.print("<TD>"+rs.getString("语文")+"</TD>");
out.print("<TD>"+rs.getString("数学")+"</TD>");
out.print("<TD>"+rs.getString("英语")+"</TD></TR>");
if(!rs.next()) break;
}
out.print("</TABLE>");
}
catch(SQLException e){
System.out.println(e.getMessage());
}
finally{
stmt.close();
conn.close();
}
%>
<FORM Action="xxx.jsp" Method="GET">
<%
if(Page != 1) {
out.print(" <A HREF=xxx.jsp?Page=1>第一页</A>");
out.print(" <A HREF=xxx.jsp?Page=" + (Page-1) + ">上一页</A>");
}
if(Page != totalPage) {
out.print(" <A HREF=xxx.jsp?Page=" + (Page+1) + ">下一页</A>");
out.print(" <A HREF=xxx.jsp?Page=" + totalPage + ">最后一页</A>");
}
%>
<BR>输入页数:<input TYPE="TEXT" Name="Page" SIZE="3">
页数:<font COLOR="Red"><%=Page%>/<%=totalPage%></font>
</FORM>
create proc [dbo].[sp_pro]
@page int,
@count int,
as
select * from(
select pname,pnew,price,sellprice,photo,uname,state,description,invoice,number,isSend,contact,date, from products where rownum between ((@page-1)*@count)+1 and @page*@count
也可以用像2楼所说的方法
sql语句分页效率高,只是应用单一
package back.common;import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;public class PageService extends HibernateDaoSupport { public void setParamater(String from, String where, String orderby,
String groupby, int pageSize, String curPage, HashMap hmValues) {
this.from = from;
this.where = where;
this.orderby = orderby;
this.groupby = groupby;
this.pageSize = pageSize;
this.curPage = curPage;
this.hmValues = hmValues;
} private String from;
private String where;
private String orderby;
private String groupby;
private int pageSize;
private String curPage;
private HashMap hmValues; /**
* 获得表的记录条数
*
* @return
* @throws SQLException
* @throws ClassNotFoundException
*/
public int getTotalCount() {
// String
// query="from Book where status='1' and shelfTime between to_date('2009-05-01','yyyy-MM-dd') and to_date('2010-07-21','yyyy-MM-dd') ";
String query = from + where;
Query q = this.getSessionFactory().openSession().createQuery(query);
q = SetValue(q);
int count = 0;
count = q.list().size();
return count;
} /**
* 给Query赋值
*
* @param q
* @return
*/
private Query SetValue(Query q) {
if (this.hmValues != null && this.hmValues.size() > 0) {
for (Object key : this.hmValues.keySet()) {
String k = key.toString();
Object v = hmValues.get(k);
if (v instanceof Date) {
q.setDate(k, (Date) v);
} else {
q.setString(k, v.toString());
}
}
}
return q;
} /**
* 获得总页数
*
* @return
* @throws SQLException
* @throws ClassNotFoundException
*/
public int getTotalPageCount() {
int totalCount = getTotalCount();
int totalPageCount = 0;
if (totalCount % pageSize == 0) {
totalPageCount = totalCount / pageSize;
} else {
totalPageCount = totalCount / pageSize + 1;
}
return totalPageCount;
} /**
* 获得分页
*/
public ArrayList getPage() {
ArrayList pageList = new ArrayList();
int startPos = 0;
startPos = (Integer.parseInt(getCurPage()) - 1) * this.pageSize;
String query = from + " " + where + " " + groupby + " " + orderby;
System.out.println("==========" + query);
Query q = this.getSessionFactory().openSession().createQuery(query);
q = SetValue(q);
pageList = (ArrayList) q.setFirstResult(startPos).setMaxResults(
this.pageSize).list();
return pageList;
} /**
* 获得当前第几页
*
* @return
* @throws ClassNotFoundException
* @throws SQLException
* @throws NumberFormatException
*/
public String getCurPage() {
int totalPageCount = getTotalPageCount();
int curpage = 1;
try {
curpage = Integer.parseInt(this.curPage);
if (curpage > totalPageCount) {
curpage = totalPageCount;
} else if (curpage < 1) {
curpage = 1;
}
} catch (Exception e) {
curpage = 1;
}
return curpage + "";
}
}