
正文
分页查询的java代码 分页查询的java代码是什么
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
每翻一页,都去数据库中查一次。这种分页效果用java怎么写?
在用jdbc访问某个数据库,并读出一个resultset数据记录集时,如果记录数太大,则会占用客户端(运行java程式分页查询的java代码的机器)的大量内存(如果客户端是某个大企业的营业部门的代理点分机,则有可能java程式会占用完全部内存,然后报错),并且会造成客户端负载过重,运行速度极度缓慢(在sybase数据库中,我曾做过一个测试。选择某个大数据记录,sql语句运行完需要4分钟,而在java客户端完全显示出来,则需要将近10分钟)。解决方案分页查询的java代码:
给用户提供一个可选择分页显示的选项,如果用户不想分页显示则完全显示,否则分页显示。
1.定义一个分页数全局常量,即每页显示的数据条数。
private final static int skip = 100;
2.定义一个确定某个分页条数的全局变量,即该显示页的当前显示数据条数。
private static int cur = 0;
3.定义一个resultset全局变量,以便多次使用
private static java.sql.result rs = null;
4.打开一个数据库连接[/pre]
class.forname( sqldriver );java.sql.connection conn = drivermanager.getconnection( url, (string)username,(string)passwd)statement stmt = conn.createstatement();string searchsql = "......";rs = stmt.executequery(searchsql);
相关问答
Q1: java 中查询数据库后的分页咋做,管理系统的查询显示方面,上一页,下一页的实现
数据库 分页的查询语句为 select top pageSize * from objectTable obj where obj.id not in(select top (pageNumber-1)*pageSize id from objectTable) 然后将查询结果放到一个List集合中 返回回去。在页面进行显示就可以了。但注意的是页面要控制好当前页码。 pageSize: 每页显示的信息条数、pageNumber:当前页码
Q2: 求java分页代码,急用!
package common.util;
import java.util.*;
public class PageController implements IPageModel {
private Collection model;
//数据总行数
private int totalRowCount = 0; //
//总页数
private int pageCount = 0;
//每页应显示分页查询的java代码的行数
private int maxPageRowCount = 0;
//当前页行数
private int currPageRowCount = 0;
//当前页号
private int currPageNum;
//默认构造
public PageController() {
super();
}
//传入模型
public PageController(Collection model) {
setPageController(model);
}
//设一个分页模型
public void setPageController(Collection model) {
this.model = model;
this.totalRowCount = model.size();
}
/**
* 总页数
* @return int
*/
public int getPageCount() {
return this.pageCount;
}
/**
* getPageContents
*
* @param intPageNum int
* @return Object
*/
public Object getPageContents(int intPageNum) {
//非法数据
if(intPageNum1){
intPageNum=1;
}
if(intPageNumpageCount){
intPageNum=pageCount;
}
//指定当前页
this.currPageNum=intPageNum;
int i = 0;
ArrayList arr = new ArrayList();
//如果是合法分页查询的java代码的范围
if (intPageNum 0 intPageNum = pageCount) {
//计算该页的开始号和结束号
int lfromrow = (intPageNum - 1) * maxPageRowCount;
arr = (ArrayList) getElementsAt(model, lfromrow, lfromrow + maxPageRowCount-1);
}
currPageNum=intPageNum;
return arr;
}
public Object getLastPage() {
return this.getPageContents(pageCount);
}
public Object getFirstPage() {
return this.getPageContents(0);
}
/**
* getCurrentPageRowsCount
*
* @return int
*/
public int getCurrentPageRowsCount() {
if(currPageNumpageCount){
return maxPageRowCount;
}
else{//最后一页
return totalRowCount-(pageCount-1)*maxPageRowCount;
}
}
public int getCurrentPageNum(){
return currPageNum;
}
/**
* setMaxPageRows
*
* @return int
*/
public void setMaxPageRows(int rowCount) {
maxPageRowCount = rowCount;
//计算总页数
if (totalRowCount % maxPageRowCount 0) { //有余数
pageCount = totalRowCount / maxPageRowCount + 1;
}
else {
pageCount = totalRowCount / maxPageRowCount;
}
}
/**
* getMaxPageRows
*/
public int getMaxPageRows() {
return maxPageRowCount;
}
//私有方法分页查询的java代码,返回集合中指定范围的数据
private Object getElementsAt(Collection model, int fromIndex, int toIndex) {
Iterator iter = model.iterator();
ArrayList arr = new ArrayList();
if (iter != null) {
int i = 0;
while (iter.hasNext()) {
Object obj=iter.next();
if (i = fromIndex i = toIndex) {
arr.add(obj);
}
if (i toIndex) {
break;
}
i = i + 1;
}
}
return arr;
}
}
Q3: java中如何实现百度中的分页
/**
* 分页代码
*
* @author Star
* @version 1.0 2008/07/08
*/
public class CutPage implements Serializable{
private static Log log = LogFactory.getLog(CutPage.class);
private int curPageNo = 0; // 当前页数,从0开始
private int size = 0; // 所有数据条数
private String url; // 页面跳转的路径
private List showList; // 当前页面需要显示的数据列表
private int pageSize = 20;// 每页显示的数据条数
private int groupSize = 1;// 多少页为一组
private String pageNavigation;// 导航条
/**
* 每次通过sql语句从数据库里面分组取出需要显示的数据
*
* @param request
* javax.servlet.http.HttpServletRequest对象
* @param sql
* String 查询数据库的sql语句
* @param pageSize
* int 每页显示的条数
* @param groupSize
* int 分成多少组
* @param url
* String 页面跳转的路径,若没有特殊的参数传递,可以传入null或"",
* 如是在aciton里面调用,并且action是继承自DispatherAction的话最好传入完整的路径
*/
public void init(HttpServletRequest request, String sql, int pageSize,
int groupSize, int pageNo, String url) {
// 上一页、下一页跳转路径
if (url != null) {
this.url = url;
} else {
this.url = request.getRequestURL() + "";
}
if (pageSize 0)
this.pageSize = pageSize;// 每页多少条记录
if (groupSize 0)
this.groupSize = groupSize;
// 当前第几页
if (pageNo 0) {
this.curPageNo = 0;
} else {
this.curPageNo = pageNo;
}
int curGroup = this.curPageNo / this.groupSize + 1;
// 是否是新的一组数据,如果是则到数据库取数据
this.size = parseInt(request.getSession().getAttribute("page_all_size")
+ "", 0);
if (this.curPageNo % this.groupSize == 0
|| (request.getSession().getAttribute("cur_group") != null parseInt(
"" + request.getSession().getAttribute("cur_group"), 1) != curGroup)
|| this.size == 0 || request.getParameter("reload") != null) {
request.getSession().setAttribute("cur_group", curGroup);
if (pageNo 0
request.getSession().getAttribute("page_sql") != null) {
sql = request.getSession().getAttribute("page_sql") + "";
} else {
request.getSession().setAttribute("page_sql", sql);
}
this.size = getTotalCount(sql);
List list = getPageData(sql, (this.curPageNo / this.groupSize)
* this.pageSize * this.groupSize, this.pageSize
* this.groupSize);
request.getSession().setAttribute("page_all_size", this.size);
request.getSession().setAttribute("page_cur_list", list);
this.setShowList(list);// 设置页面上的显示数据
} else {
this.setShowList((List) request.getSession().getAttribute(
"page_cur_list"));// 设置页面上的显示数据
}
}
/**
* 每次通过sql语句从数据库里面分组取出需要显示的数据
*
* @param request
* javax.servlet.http.HttpServletRequest对象
* @param sql
* String 查询数据库的sql语句
* @param pageSize
* int 每页显示的条数
* @param groupSize
* int 分成多少组
* @param url
* String 页面跳转的路径,若没有特殊的参数传递,可以传入null或"",
* 如是在aciton里面调用,并且action是继承自DispatherAction的话最好传入完整的路径
*/
public void init(HttpServletRequest request, String sql, int pageSize,
int groupSize, String url) {
// 当前第几页
String curPage = request.getParameter("pageNo");
init(request, sql, pageSize, groupSize, parseInt(curPage, -1), url);
}
/**
* 每次通过sql语句从数据库里面分组取出需要显示的数据
*
* @param request
* javax.servlet.http.HttpServletRequest对象
* @param sql
* String 查询数据库的sql语句
* @param pageSize
* int 每页显示的条数
* @param groupSize
* int 分成多少组
* @param url
* String 页面跳转的路径,若没有特殊的参数传递,可以传入null或"",
* 如是在aciton里面调用,并且action是继承自DispatherAction的话最好传入完整的路径
*/
public void init(HttpServletRequest request, String sql, int pageSize,
int groupSize, int pageNo) {
init(request, sql, pageSize, groupSize, pageNo, "");
}
太多了,贴不下,见附件
分页查询的java代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于分页查询的java代码是什么、分页查询的java代码的信息别忘了在本站进行查找喔。







