找了n久没找到java分页.谁能给我个啊...不要jsp的
要通用的分页所有数据库通用.不要框架的分页
谢谢了啊 注释多点的...
要通用的分页所有数据库通用.不要框架的分页
谢谢了啊 注释多点的...
解决方案 »
- 求解:Tomcat启动时,其catalina.out文件老是有错,但是应用还是能正常访问。
- 初学Struts2 遇到个小问题 ,配置方面的,求教……
- RSA开发
- MyEclipse配置Tomcat问题!
- spring 中的JdbcTemplate和hibernate中对数据库的封装是否重复
- jboss7+mod_jk+apache负载均衡
- 用axis来调用php开发的webService,急!!!
- 谁能发份STRUTS 1.0.2给我,不盛感谢!立即给分
- 请问一个JNDI绑定的问题
- spring mvc里面java bean中date类型的绑定问题?
- 大家帮忙解释一段Js代码----Unicode 转 Utf8 代码
- net.sf.json.JSONArray 对象 toCollection
以现在的面象对象基础还没点概念
写不出来
没次都要连接一次...很浪费啊
就没有办法一次取出来后在分页的
我指的当然是要哪一页就仅仅查这一页的数据啦。常见数据库分页 SQL 语句的关键:MySQL, PostgreSQL 使用 LIMIT 子句
Oracle 使用 rownum 伪列或者 row_number() 的分析函数
MS SQLServer 2005 和 DB2 使用 row_number() 的分析函数
MS SQLServer 2000 和 Sybase 的 12.5 以上版本,只能使用 TOP 子句
总记录,每页大小
里面的事情就是判断了
SQL语句楼上已经写出来了,每个数据库都不一样,但你可以写一个通用的接口,以后换数据库也方便
最好封装个javabean要不jsp太难看了
<pg:param name="parentId"/>
<pg:first>
<a href="${pageUrl}">首页</a>
</pg:first>
<pg:prev>
<a href="${pageUrl }">前页</a>
</pg:prev>
<pg:pages>
<c:choose>
<c:when test="${currentPageNumber eq pageNumber }">
<font color="red">${pageNumber }</font>
</c:when>
<c:otherwise>
<a href="${pageUrl }">${pageNumber }</a>
</c:otherwise>
</c:choose>
</pg:pages>
<pg:next>
<a href="${pageUrl }">后页</a>
</pg:next>
<pg:last>
<a href="${pageUrl }">尾页</a>
</pg:last>
</pg:pager>其中,url你应该明白,就是request的请求路径,pm是我在ActionForm里面设置的通用分页处理类PagerModel的一个对象,下面是它的代码。你可以用PagerModel对象来保存任意你想在页面上传递的用于分页的实体类的对象结果集。中间用JSTL表达式实现的是用红色显示当前页页码的特别提示。最好去看看Pager-taglib的文档,你会受益匪浅……import java.util.List;
public class PagerModel {
/**
* 总记录数
*/
private int total;
/**
* 当前页结果集
*/
private List datas; public List getDatas() {
return datas;
} public void setDatas(List datas) {
this.datas = datas;
} public int getTotal() {
return total;
} public void setTotal(int total) {
this.total = total;
}
}
自己写个标签,调用后台返回的结果不就实现了么。
非得弄得通用些。就自己写出各种数据库的分页SQL。用什么数据库调用什么SQL不就通用了。
* <p> Title: 分页 </p>
* <p> Description:分页 </p>
* <p> Copyright: Copyright (c) 2003 </p>
* <p> Company:工科 </p>
* @author not attributable
* @version 1.0
*/
import java.sql.*;
import java.util.*;
import sun.jdbc.rowset.CachedRowSet; public class Paging {
private OpenDbBean db = new OpenDbBean();
private Connection conn = null;
private ResultSet rs = null;
private Statement st = null;
private String tableName = null;//需要分页的数据库表名称
private String whereCondition = null;//查询语句的where条件
private String orderby = null;//查询语句的orderby条件
private int countPerPage = 20; //默认每页的数目
//输入:无
//输出:所有的页数
public int getAllpage() {
int i = 0;
try {
conn = db.getConnection();
st = conn.createStatement();
String sql = "select count(*) from " + this.tableName;
if (this.whereCondition != null) {
sql += " where " + this.whereCondition;
}
System.out.println(sql);
rs = st.executeQuery(sql);
if (rs.next()) {
i = (rs.getInt(1) / this.countPerPage)+1;
}
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
finally {
try {
db.CleanConnection(conn, st, rs);
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
}
return i;
}
//输入:每页显示的条目数
public void setCountPerPage(int i) {
this.countPerPage = i;
}
//输入:要分页的表名
public Paging(String tableName) {
this.tableName = tableName;
}
//输入:查询的where条件
public void setWhere(String str) {
this.whereCondition = str;
}
//输入:当前页面号
//输出:下一个页面号
public int getNexepage(int pageNum){
if (pageNum==this.getAllpage())
return this.getAllpage();
else{
return pageNum+1;
}
}
//输入:当前页面号
//输出:前一个页面号
public int getPreviouspage(int pageNum){
if (pageNum==1)
return 1;
else{
return pageNum-1;
}
}
//输入:查询的orderby条件
public void setOrderby(String str) {
this.orderby = str;
}
//输出:查询的sql语句
public String getSql() {
String sql = "select * from " + this.tableName;
if (this.whereCondition != null) {
sql += " where " + this.whereCondition;
}
if (this.orderby != null) {
sql += " order by " + this.orderby;
}
return sql;
}
//输入:当前页面号
//输出:取得当前页面结果集的sql语句
public String getSql(int pageNum) {
String sql = "select * from " + this.tableName;
if (this.whereCondition != null) {
sql += " where " + this.whereCondition;
}
sql+= " limit "+(pageNum-1)*this.countPerPage+ ", "+this.countPerPage;
return sql;
}
//输入:当前页面号
//输出:当前页面的结果集
public CachedRowSet getPageRecord(int pageNum) throws SQLException{
CachedRowSet cst=new CachedRowSet();
try {
conn = db.getConnection();
st = conn.createStatement();
rs = st.executeQuery(this.getSql(pageNum));
cst.populate(rs);
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
finally {
try {
db.CleanConnection(conn, st, rs);
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
}
return cst;
}
//测试例子
public static void main(String[] args) {
Paging p = new Paging( "table1 ");
p.setCountPerPage(5);
System.out.println(p.getSql());
System.out.println( "total page= " + p.getAllpage());
try{
CachedRowSet cs=p.getPageRecord(1);
while(cs.next()){
System.out.println(cs.getString(1));
}
}
catch(Exception e){
System.out.println(e.getMessage());
}
} }
jsp调用代码
<table width= "100% " border= "0 " cellspacing= "0 " cellpadding= "0 ">
<%
sdk.Paging pageBean=new sdk.Paging( "news ");
pageBean.setCountPerPage(20);
pageBean.setWhere( "n_onpub= '1 ' and nt_id=1 and n_isuse= '1 ' "); String str=request.getParameter( "page_count ");
if (str==null) str= "1 ";
int p_num=Integer.parseInt(str); try{
CachedRowSet cs=pageBean.getPageRecord(pageBean.getAllpage()-p_num+1);
cs.afterLast();
while(cs.previous()){
%>
<tr>
<td height= "22 "> <img src= "images/news01.gif " width= "9 " height= "12 ">
<a href= "viewnews.jsp?n_id= <%=cs.getString( "n_id ")%> ">
<%=sdk.Tools.toGBKString(cs.getString( "n_title "))%> </a> ( <%=sdk.Tools.toGBKString(cs.getString( "n_time "))%> ) </td>
</tr>
<%
}
}
catch(Exception e){
System.out.println(e.getMessage());
}
%>
</table> </td>
</tr>
<tr>
<td height= "13 "> </td>
</tr>
<tr>
<td height= "22 " bgcolor= "#E8E8E8 "> <p> <img src= "images/left.gif " width= "7 " height= "11 "> <img src= "images/left.gif " width= "7 " height= "11 ">
<a href= "news.jsp?page_count=1 "> 第一页 </a> <img src= "images/left.gif " width= "7 " height= "11 ">
<a href= "news.jsp?page_count= <%=pageBean.getPreviouspage(p_num)%> ">
上一页 </a> <img src= "images/right.gif " width= "7 " height= "11 ">
<a href= "news.jsp?page_count= <%=pageBean.getNexepage(p_num)%> ">
下一页 </a> <img src= "images/right.gif " width= "7 " height= "11 "> <img src= "images/right.gif " width= "7 " height= "11 ">
<a href= "news.jsp?page_count= <%=pageBean.getAllpage()%> ">
最后一页 </a> 第 <font color= "#FF0000 "> <%=p_num%> </font> 页/共 <font color= "#FF0000 "> <%=pageBean.getAllpage()%> </font> 页 </p> </td>
</tr>
</table> </td>
</tr>
</table> </td>
<td width= "21 " valign= "top "> </td>
</tr>
</table>