如何用javaBean 实现分页

kililisa 2008-07-17 11:03:15
如何用javaBean 实现分页
算法,最好是源代码?
...全文
113 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiangfeng08 2008-07-24
  • 打赏
  • 举报
回复
rs = new DBConnBean().execQuery(sql);
是我调用封装的数据库操作方法
你只需执行下查询就行
jiangfeng08 2008-07-24
  • 打赏
  • 举报
回复

//DAO中的内容
package com.jinluopan.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.jinluopan.bean.DBConnBean;
import com.jinluopan.bean.StockArticleBean;

public class StockArticleDao {
public StockArticleBean getStockArticle(String sql) throws SQLException {
StockArticleBean sab = null;
ResultSet rs = null;
rs = new DBConnBean().execQuery(sql);
while (rs.next()) {
sab = new StockArticleBean(rs.getInt(1), rs.getString(2), rs
.getString(3), rs.getString(4), rs.getString(5), rs
.getString(6), rs.getString(7), rs.getString(8));
}
if (rs != null) {
rs.close();
}
return sab;
}

public ArrayList<StockArticleBean> getStockArticleForAmend(String sql)
throws SQLException {
ArrayList<StockArticleBean> list = new ArrayList<StockArticleBean>();
ResultSet rs = null;
rs = new DBConnBean().execQuery(sql);
while (rs.next()) {
list.add(new StockArticleBean(rs.getInt(1), rs.getString(2), rs
.getString(3), rs.getString(4), rs.getString(5), rs
.getString(6), rs.getString(7)));
}
if (rs != null) {
rs.close();
}
return list;
}
}

action中的内容

/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.jinluopan.struts.action;

import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.jinluopan.bean.StockArticleBean;
import com.jinluopan.dao.StockArticleDao;
import com.jinluopan.dto.StockArticlePage;
import com.jinluopan.struts.form.StockarticleForm;

/**
* MyEclipse Struts Creation date: 07-21-2008
*
* XDoclet definition:
*
* @struts.action path="/stockarticle" name="stockarticleForm"
* input="/stockarticle.jsp" scope="request" validate="true"
* @struts.action-forward name="view" path="/stockarticle.jsp"
* @struts.action-forward name="fail" path="/getdatafail"
*/
public class StockarticleAction extends Action {
/*
* Generated Methods
*/
private StockArticlePage page;

/**
* Method execute
*
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
StockarticleForm stockarticleForm = (StockarticleForm) form;
ArrayList<StockArticleBean> list = new ArrayList<StockArticleBean>();
String sql = "select id,title,orgname,url,daytime,upv,downv from stockarticle";
String action = null;
action = request.getParameter("action");
if (action == null || action.equals("null")) {
try {
list = new StockArticleDao().getStockArticleForAmend(sql);
} catch (SQLException e) {
e.printStackTrace();
return mapping.findForward("fail");
}
if (list.size() == 0) {
request.setAttribute("stockarticle", list);
request.setAttribute("page", page);
} else {
page = new StockArticlePage(list);
list = page.getPageList();
request.setAttribute("stockarticle", list);
request.setAttribute("page", page);
}
} else {
if (action == "firstPage" || action.equals("firstPage")) {
page.getFirstPage();
list = page.getPageList();
request.setAttribute("stockarticle", list);
request.setAttribute("page", page);
}
if (action == "nextPage" || action.equals("nextPage")) {
page.getNextPage();
list = page.getPageList();
request.setAttribute("stockarticle", list);
request.setAttribute("page", page);
}
if (action == "previousPage" || action.equals("previousPage")) {
page.getPreviousPage();
list = page.getPageList();
request.setAttribute("stockarticle", list);
request.setAttribute("page", page);

}
if (action == "lastPage" || action.equals("lastPage")) {
page.getLastPage();
list = page.getPageList();
request.setAttribute("stockarticle", list);
request.setAttribute("page", page);

}
if ("goto".equals(action)) {
int pages = Integer.parseInt(stockarticleForm.getPages());
page.setGotoPage(pages);
list = page.getPageList();
request.setAttribute("stockarticle", list);
request.setAttribute("page", page);
}
}
return mapping.findForward("view");
}
}

另外pages是从form中传来


希望对你有帮助!
jiangfeng08 2008-07-24
  • 打赏
  • 举报
回复
现在提供一种struts的分页方法
package com.jinluopan.dto;

import java.util.ArrayList;

import com.jinluopan.bean.StockArticleBean;

BEAN:
package com.jinluopan.bean;

public class StockArticleBean {
private int id; // 股评文章ID
private String title; // 股评标题
private String orgname; // 股评机构名
private String context; // 股评内容
private String url; // 股评来源
private String dayime; // 股评日期
private String upv; // 是否上涨
private String downv; // 是否下降

public StockArticleBean(int id, String title, String orgname,
String context, String url, String dayime, String upv, String downv) {
super();
this.id = id;
this.title = title;
this.orgname = orgname;
this.context = context;
this.url = url;
this.dayime = dayime;
this.upv = upv;
this.downv = downv;
}

public StockArticleBean(int id, String title, String orgname, String url,
String dayime, String upv, String downv) {
super();
this.id = id;
this.title = title;
this.orgname = orgname;
this.url = url;
this.dayime = dayime;
this.upv = upv;
this.downv = downv;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getOrgname() {
return orgname;
}

public void setOrgname(String orgname) {
this.orgname = orgname;
}

public String getContext() {
return context;
}

public void setContext(String context) {
this.context = context;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getDaytime() {
return dayime;
}

public void setDaytime(String datetime) {
this.dayime = datetime;
}

public String getUpv() {
return upv;
}

public void setUpv(String upv) {
this.upv = upv;
}

public String getDownv() {
return downv;
}

public void setDownv(String downv) {
this.downv = downv;
}

}
分页辅助方法
/**
* 组织机构分页辅助类
*
* @author wjf
*
*/
public class StockArticlePage {
private int currentPage = 1;// 当前页
private int totalPages = 0;// 总页数
private int pageRecorders = 20;// 每页10行
private int totalRows = 0;// 总行数
private int pageStartRow = 0;// 每页的起始行
private int pageEndRow = 0;// 每页显示的终止数
private boolean hasNextPage = false;// 有没有下一页
private boolean hasPreviousPage = false;// 有没有上一页
@SuppressWarnings("unused")
private ArrayList<StockArticleBean> list;

/**
* 用于初始化, 也就是说,传过来一个list就可以分页
*
* @param list
*/
public StockArticlePage(ArrayList<StockArticleBean> list) {
this.list = list;
totalRows = list.size();
hasPreviousPage = false;
currentPage = 1;
if (totalRows % pageRecorders == 0) {
totalPages = totalRows / pageRecorders;

} else {
totalPages = totalRows / pageRecorders + 1;
}
if (currentPage >= totalPages) {
hasNextPage = false;
} else {
hasNextPage = true;
}
if (totalRows < pageRecorders) {
this.pageStartRow = 0;
this.pageEndRow = totalRows;
} else {
this.pageStartRow = 0;
this.pageEndRow = pageRecorders;
}

}

public int getCurrentPage() {
return currentPage;
}

public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}

public boolean isHasNextPage() {
return hasNextPage;
}

public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}

public boolean isHasPreviousPage() {
return hasPreviousPage;
}

public void setHasPreviousPage(boolean hasPreviousPage) {
this.hasPreviousPage = hasPreviousPage;
}

public int getPageEndRow() {
return pageEndRow;
}

public void setPageEndRow(int pageEndRow) {
this.pageEndRow = pageEndRow;
}

public int getPageRecorders() {
return pageRecorders;
}

public void setPageRecorders(int pageRecorders) {
this.pageRecorders = pageRecorders;
}

public int getPageStartRow() {
return pageStartRow;
}

public void setPageStartRow(int pageStartRow) {
this.pageStartRow = pageStartRow;
}

public String getTotalPages() {
return this.toString(totalPages);
}

public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}

public String getTotalRows() {
return this.toString(totalRows);
}

public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}

public void getFirstPage() {
currentPage = 1;
hasPreviousPage = false;
if (currentPage >= totalPages) {
hasNextPage = false;
} else {
hasNextPage = true;
}
}

public void getNextPage() {
currentPage = currentPage + 1;
if ((currentPage - 1) > 0) {
hasPreviousPage = true;
} else {
hasPreviousPage = false;
}
if (currentPage >= totalPages) {
hasNextPage = false;
} else {
hasNextPage = true;
}
}

public void getPreviousPage() {
currentPage = currentPage - 1;
if (currentPage == 0) {
currentPage = 1;
}
if (currentPage >= totalPages) {
hasNextPage = false;
} else {
hasNextPage = true;
}
if ((currentPage - 1) > 0) {
hasPreviousPage = true;
} else {
hasPreviousPage = false;
}
}

public void getLastPage() {
currentPage = totalPages;
if (currentPage == 0) {
currentPage = 1;
}
hasNextPage = false;
if ((currentPage - 1) > 0) {
hasPreviousPage = true;
} else {
hasPreviousPage = false;
}
}

public void setGotoPage(int gotoPage) {
this.currentPage = gotoPage;
hasNextPage = true;
hasPreviousPage = true;
if (currentPage <= 1) {
currentPage = 1;
hasNextPage = true;
hasPreviousPage = false;
}
if (currentPage >= totalPages) {
currentPage = totalPages;
hasNextPage = false;
hasPreviousPage = true;
}
}

@SuppressWarnings("unchecked")
public ArrayList<StockArticleBean> getPageList() {
if (currentPage * pageRecorders < totalRows) {
pageEndRow = currentPage * pageRecorders;
pageStartRow = pageEndRow - pageRecorders;

} else {
pageEndRow = totalRows;
pageStartRow = pageRecorders * (totalPages - 1);
}
ArrayList<StockArticleBean> templist = new ArrayList<StockArticleBean>();
int j = 0;
for (int i = pageStartRow; i < pageEndRow; i++) {
StockArticleBean ob = (StockArticleBean) list.get(i);
templist.add(j, ob);
j++;
}
return templist;
}

public String toString(int temp) {
return Integer.toString(temp);
}

}
hyxwxy 2008-07-24
  • 打赏
  • 举报
回复
pagebean.java如下

package page;

import page.*;

public class pagebean {
private int currentPage;//当前页数
private int countRecord;//总记录条数
private int countPage;//总页数
private int sizePage;//每页记录条数
public void setAll(int _countRecord,int _sizePage)
//设置四个成员变量的值
{
countRecord=_countRecord;
sizePage=_sizePage;
if(countRecord%sizePage==0)
countPage=countRecord/sizePage;
else
countPage=countRecord/sizePage+1;
currentPage=1;
}
public int getCurrentPage() {
return currentPage;
}
public int getCountPage() {
return countPage;
}
public long getCountRecord() {
return countRecord;
}
public int getSizePage() {
return sizePage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
}
hyxwxy 2008-07-24
  • 打赏
  • 举报
回复
conndb.java如下:

package page; //本javabean处于page包中

import java.sql.*;


public class conndb {
private Connection con;
private ResultSet rs;
public static Connection getConnection() throws SQLException {
try {
//连接MS SQL Server数据库
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
return null;
}
return DriverManager.getConnection(
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=guestinfo","sa","");
//设置连接的数据库名,登陆的用户名和密码
}

//执行SQL语句的查询操作
public ResultSet executeQuery(String sql) {
try {
con = conndb.getConnection();
Statement statement = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = statement.executeQuery(sql);
}
catch(SQLException ex)
{
}
return rs;
}

//执行SQL语句的更新操作
public int executeUpdate(String sql)
{
int count = 0;
Statement stmt = null;
try
{
con = conndb.getConnection();
stmt = con.createStatement();
count = stmt.executeUpdate(sql);
}
catch(SQLException ex)
{
}
finally
{
try
{
if(stmt != null)
stmt.close();
if(con != null)
con.close();
}
catch(SQLException ex)
{
System.err.print(ex);
}
}
return count;
}

//释放数据集rs,关闭数据库连接
public void freeRs(ResultSet rs)
{
try
{
if(rs != null)
{
rs.close();
con.close();
}
}
catch(Exception e)
{
}
}
}
hyxwxy 2008-07-24
  • 打赏
  • 举报
回复
用javabean实现数据库分页查询,有这样三个文件
usepage.jsp是数据库查询的显示页,conndb.java封装了数据库的连接和查询的代码,pagebean.java封装了查询分页的相关操作。
usepage.jsp如下:

<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.sql.*" %>
<%@ page import="page.*" %> //这两个javabean都在page包中
<jsp:useBean id="pagebean" scope="session" class="page.pagebean"/>
<jsp:useBean id="databean" scope="session" class="page.conndb"/>
<HTML>
<title>用户数据</title>
<BODY>
<%
ResultSet rs;
String sql="SELECT * FROM guestinfo"; //查询表guestinfo中的所有数据
rs=databean.executeQuery(sql); //执行sql语句,使用了javabean conndb中的executeQuery
String pages=request.getParameter("dipage"); //读取当前的页数

rs.last();
int countRecord=rs.getRow(); //得到记录的条数
int countPageRecord=5; //设置每页记录条数
if(pages==null||(pages.trim()).length()==0) //如果当前页数不存在,也就是初次进入页面
pagebean.setAll(countRecord,countPageRecord); //参数初始化,使用javabean pagebean中的setall,当前页数设为1
else
{
try
{
pagebean.setCurrentPage(Integer.parseInt(pages));
}
catch(Exception e)
{
out.println("参数不正确!");
}
}
//当前记录号
int currentRecord=(pagebean.getCurrentPage()-1)*pagebean.getSizePage();
if(currentRecord==0)
rs.beforeFirst();
else
rs.absolute(currentRecord);
//显示当前页数据
out.print("<Table Border style='font-size: 10pt'>");
out.print("<TR><td colspan=8 align=center>用户数据</td></tr>");
out.print("<TR>");
out.print("<Td width=60 >"+"用户ID号");
out.print("<Td width=50 >"+"用户名");
out.print("<Td width=100>"+"用户真实姓名");
out.print("<Td width=40>"+"年龄");
out.print("<Td width=40>"+"邮政编码");
out.print("<Td width=100>"+"联系地址");
out.print("<Td width=100>"+"联系电话");
out.print("<Td width=100>"+"email");
out.print("</TR>");
int i=0;
while(rs.next())
{ out.print("<TR>");
out.print("<TD >"+rs.getLong(1)+"</TD>");
out.print("<TD >"+rs.getString("guestname")+"</TD>");
out.print("<TD >"+rs.getString("name")+"</TD>");
out.print("<TD >"+rs.getInt("age")+"</TD>");
out.print("<TD >"+rs.getString("postcode")+"</TD>");
out.print("<TD >"+rs.getString("address")+"</TD>");
out.print("<TD >"+rs.getString("tel")+"</TD>");
out.print("<TD >"+rs.getString("email")+"</TD>");
out.print("</TR>") ;
i++;
if(i>=pagebean.getSizePage()) break; //当前页显示完,则退出循环
}
out.print("<TR><td colspan=8 align=center>");
out.print("共"+pagebean.getCountRecord()+"条记录,共"+pagebean.getCountPage()+"页,当前第"+pagebean.getCurrentPage()+"页,每页"+pagebean.getSizePage()+"条记录,");
if(pagebean.getCurrentPage()==1)//当前是首页
;
else//当前不是首页
{
out.print("<a href=usepage.jsp?dipage=1>首页</a>,");
out.print("<a href=usepage.jsp?dipage="+(pagebean.getCurrentPage()-1)+">上一页</a>,");
}
if(pagebean.getCurrentPage()==pagebean.getCountPage())//当前是末页
;
else//当前不是末页
{
out.print("<a href=usepage.jsp?dipage="+(pagebean.getCurrentPage()+1)+">下一页</a>,");
out.print("<a href=usepage.jsp?dipage="+pagebean.getCountPage()+">末页</a>");
}

out.print("</td></tr>");
out.print("</Table>");
databean.freeRs(rs); //释放rs

%>
</BODY>
</HTML>


81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧