- 浏览: 116702 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
darrendu:
如果我们不准备实现一个接口的全部方法时,因为我们这个类不需要用 ...
缺省适配器模式 -
cwalet:
显然这个错误是在使用struts标签时产生的,原因是没有在st ...
彻底解决 警告: No configuration found for the specified action -
jzinfo:
{} xtcpcgx 写道还是有点不明白,感觉缺省适配类跟接口 ...
缺省适配器模式 -
xtcpcgx:
还是有点不明白,感觉缺省适配类跟接口代码一模一样啊。
缺省适配器模式 -
googya:
这段代码的结果是,dopost根本没有执行?
servlet中service doGet doPost 的关系
针对分页,首先开发一个 PageBean 用来控制页面参数:
package com.longweir; //分页后的javaBean import java.sql.*; import com.longweir.util.*; public class PageBean { private int pageSize=5; // 每页显示的记录数5个 private int currentPage=1; // 当前页码 private int pageCount=1; // 总页数 private int totalCount=0; // 总记录数 // 计算总页数 public void setPageCount() { this.pageCount=(this.totalCount-1)/this.pageSize+1; } //获取总页数 public int getPagecount() { return this.pageCount; } //设置并修正当前页码, public void setCurrentPage(int currentpage) { //校验当前页码 if (currentPage>this.pageCount) this.currentPage=this.pageCount; else if (currentPage<1) this.currentPage=1; else this.currentPage=currentpage; } //获取当前页码 public int getCurrentPage() { return this.currentPage; } //获取全部记录数 public int getTotalCount() { return this.totalCount; } //设置总共记录数 public void setTotalCount(int totalcount) { this.totalCount =totalcount; //设置总共记录数后,同时需要校正计算总页数 this.pageCount=(this.totalCount-1)/this.pageSize+1; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; //设置每页显示的记录个数后 同时需要校正计算后的总页数 this.pageCount=(this.totalCount-1)/this.pageSize+1; } }
okay,然后我们开发一个接口 SpiltPage,接口中共有两个方法。
package com.longweir; //任何业务逻辑类只要实现了该接口 就可以进行数据的分页显示 import java.util.*; import com.longweir.*; public interface SpiltPage { //根据分页对象中的参数 来分页获取数据 public Collection getPageData(PageBean pagebean) throws Exception; //获取所有的记录个数 public int getAvailableCount() throws Exception; }
这样以来,主要的关于分页的方法就完成了,我们开发一个针对数据库表操作的业务逻辑类 ProductUtil,来实现上述接口
下面这个类用来操作product表的数据,将分页或所有数据:
package com.longweir; /* * 此类包含所有的产品信息的操作业务逻辑 * */ import java.io.*; import java.sql.*; import java.util.*; import com.longweir.SpiltPage; import com.longweir.bean.ProductInfoVOBean; import com.longweir.util.DatabaseConnection; public class ProductUtil implements SpiltPage { private Connection conn; //重写无参构造方法来获取数据库连接 public ProductUtil() { this.conn=DatabaseConnection.getConnection(); //获取数据库连接对象 } //实现接口中的方法 来分页获取数据显示 public Collection getPageData(PageBean pagebean) throws Exception { Statement stmt=null; ResultSet rs=null; Collection ret=new ArrayList(); if (conn.isClosed()) conn=DatabaseConnection.getConnection(); String sqlstr="select * from productInfo order by productid limit "+(pagebean.getCurrentPage()-1)*pagebean.getPageSize()+","+pagebean.getPageSize(); try { stmt=conn.createStatement(); rs=stmt.executeQuery(sqlstr); while (rs.next()) { ProductInfoVOBean productInfo=new ProductInfoVOBean(); productInfo.setCategoryid(rs.getString("catid")); productInfo.setProductname(rs.getString("productName")); productInfo.setProductid(rs.getString("productid")); productInfo.setPublishment(rs.getString("publishment")); productInfo.setPrice(rs.getFloat("price")); productInfo.setDescription(rs.getString("descn")); ret.add(productInfo); } stmt.close(); rs.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } return ret; } //实现接口方法 获取记录的总共个数 public int getAvailableCount() { Statement stmt=null; ResultSet rs=null; int counter=0; try{ if (conn.isClosed()) conn=DatabaseConnection.getConnection(); stmt=conn.createStatement(); rs=stmt.executeQuery("select count(*) from productInfo"); while (rs.next()) { counter=rs.getInt(1); } stmt.close(); rs.close(); conn.close(); } catch (Exception e){} return counter; }
分页的关键技术就是mysql中的这条分页查询语句:
"select * from productInfo order by productid limit "+(pagebean.getCurrentPage()-1)*pagebean.getPageSize()+","+pagebean.getPageSize();
开发一个viewProduct.jsp的页面,来分页显示数据:
<%@ page contentType="text/html;charset=GBK"%> <%@ page import="java.util.*" %> <%@ page import="java.io.*" %> <%@ page import="com.longweir.bean.*" %> <jsp:useBean id="product" class="com.longweir.ProductUtil" scope="session" /> <jsp:useBean id="pagebean" class="com.longweir.PageBean" scope="session" /> <html> <head> <title>查看所有的产品的信息</title> <link rel="stylesheet" type="text/css" href="css/style.css"> </head> <body> <h3 align="center">查看所有的产品信息</h3> <table width="960" align="center" border="0" cellpadding="2" cellspacing="1" bgcolor="#999999"> <tr bgcolor="#EFEEED"> <td width="100">商品编号</td> <td width="100">类别</td> <td width="200">名称</td> <td width="100">出版商</td> <td width="80">售价</td> <td width="200">描述</td> <td colspan="2" width="100" align="center">管理</td> </tr> <% String temppage=request.getParameter("page"); int pno=1; if (temppage!=null && !("").equals(temppage)) { try { pno=Integer.parseInt(temppage); //获取提交的页面编号 } catch (Exception e) { pno=1; //有异常 则直接跳转到首条 } } //每次刷新页面时都应当重新获得表中的记录数,因为翻页过程中表的记录可能随时都会更新 pagebean.setTotalCount(product.getAvailableCount()); pagebean.setCurrentPage(pno); %> <% Collection products=product.getPageData(pagebean); //分页显示 Iterator it=products.iterator(); while (it.hasNext()) { ProductInfoVOBean temp=(ProductInfoVOBean)it.next(); out.println("<tr bgcolor=\"#FFFFFF\">"); out.println("<td>"+temp.getProductid()+"</td>"); out.println("<td>"+temp.getCategoryid()+"</td>"); out.println("<td>"+temp.getProductname()+"</td>"); out.println("<td>"+temp.getPublishment()+"</td>"); out.println("<td>"+temp.getPrice()+"</td>"); out.println("<td>"+temp.getDescription()+"</td>"); out.println("<td algin=\"center\">"+"<a href=#>修改</a>"+"</td>"); out.println("<td align=\"center\">"+"<a href=\"/product/servlet/DeleteProductServlet?productid="+temp.getProductid()+"\">删除</a</td>"); out.println("</tr>"); } %> </table> <table width="960" align="center" border="0" cellpadding="1" cellspacing="2"> <tr> <td></td> <tr> <tr> <td align="right"> 共<%=pagebean.getPagecount()%>页 <% for (int i=1;i<=pagebean.getPagecount();i++) out.println("<a href=/product/viewProduct.jsp?page="+i+">"+i+"</a>"); %> </td> </tr> </table> </body> </html>
jsp中有很多java代码,可以考虑使用标签代替现实 呵呵。
分页的效果如下啦:
发表评论
-
rerrtert
2010-08-26 15:49 18rew -
MyEclipse 快捷键大全!
2009-12-31 21:12 1655Ctrl+1 快速修复 Ctrl+D: 删除当前行 Ctrl ... -
JfreeChart中文乱码解决方案
2009-12-31 14:30 2273原帖地址:http://blog.csdn.net/wangh ... -
【转载】实施Dbutils
2009-12-04 17:01 1792JDBC码是Java译码的一个部分,它给已写的编码带来了数量惊 ... -
使用三方数据库连接池 Commons DBCP
2009-11-28 11:17 4090本内容转帖,原文地址:http://www.iteye.com ... -
读源码学习 Servlet过滤器
2009-11-26 15:09 2376过滤器在Servlet2.3中规范的,能够对Servlet容器 ... -
javaBean为什么要实现Serializable接口?
2009-11-18 11:37 1239Java的"对象序列化"能让你将一个实现了 ... -
使用JSP标签实例 实现Tag接口
2009-10-30 15:47 3423创建的标签类,必须实现javax.servlet.jsp.ta ... -
读源码学jsp自定义标签 (2) IteataionTag接口
2009-10-29 23:48 1324IterationTag接口继承了Tag接口,增加了一个方法和 ... -
读源码学jsp自定义标签 (1) Tag接口
2009-10-29 22:58 2137最近开始学些jsp的自定 ... -
读源码学Servlet(2)Servlet实现的ServletConfig接口的应用
2009-10-24 12:20 1893GenericServlet抽象类实现了ServletConf ... -
关于jsp页面中文字符集的设置
2009-10-24 11:29 2274为解决JSP中显示中文乱码的问题,一般我们在HttpRespo ... -
读源码学Servlet(5)GenericServlet中的service 方法
2009-10-23 16:36 2228观察GenericServlet源码中关于service()方 ... -
读源码学Servlet(4)关于覆盖GenericServlet的Init()方法
2009-10-23 16:17 3483之前提到servlet 生命周期中的三个阶段,第一个阶段中se ... -
读源码学Servlet(3)Servlet的生命周期
2009-10-23 15:31 1962在javax.servlet.Servlet接口中,定义了针对 ... -
读源码学Servlet(1)GenericServlet 源码分析
2009-10-23 14:05 5511Servlet API的核心就是javax.servlet.S ... -
SVN 客户端使用培训
2009-09-03 18:10 1108最近公司上SVN版本控制系统,今天给他们做了一次培训。 讲的 ... -
servlet中service doGet doPost 的关系
2009-08-25 23:25 6548在servlet中默认情况下 ... -
JavaBean的作用域
2009-08-19 01:18 979等待添加~~ -
JavaBean使用之基本规范
2009-08-19 01:12 1461JavaBean是一种特殊的Java类,他遵从一定的设计模式, ...
相关推荐
JSP+DAO和MVC+DAO(基于MySQL数据库分页)-v笔记JSP+DAO和MVC+DAO(基于MySQL数据库分页)-v笔记JSP+DAO和MVC+DAO(基于MySQL数据库分页)-v笔记
JSP+JavaBean实现MySQL子查询数据库分页 我在Eclipse3.4环境下调通了我在Eclipse3.4环境下调通了
jsp中数据库在页面分页的实现 用存储过程实现分页 servlet分页 Oracle分页 MySQL 分页 自带数据库内容
里面有2个关于分页(JSP+DAO和MVC+DAO(基于MySQL数据库分页))
jsp真分页,是mysql的数据库,里面包含jsp源码和sql语句!
Mysql java MySQL数据库下的JSP分页查询模块源码
使用JSP JavaBean来实现对数据库分页显示与搜索 <br/>1、数据库MySQL,创建数据库Pagination MySQL.sql用来创建表结构 <br/>2、配置把PaginationJSPJavaBean.rar解压到%Tomcat%\webapps\...
Struts实现MySQL数据库分页.chm。教程比较详细,也比较通用,pagebean作为分页bean。
NULL 博文链接:https://aa00aa00.iteye.com/blog/666583
java连mysql接数据库,通过jsp访问数据库并实现分页功能
简介:自幼受贵州大山的熏陶,养成了诚实质朴的性格。经过寒窗苦读,考入BIT,为完成自己的教师梦,放弃IT、航天等工作,成为贵财一名大学教师,并想把自己所学所感真
JAVA,JSP,实现各类数据库分页实现大全。通用数据分页实现设计。
用JSTL实现JSP分页显示数据代码,MySQL数据库,好用,有数据库和表创建代码!!!
用jsp、servlet、有后台管理功能 购物车 订单管理 注册登录 会员管理功能 功能强大
本程序实现jsp的分页功能,结合servlet动态显示数据库MySQL内已存储的信息
JSP/Servlet数据库分页原理解析源码下载,注释详尽、简单易懂 将下载后的压缩文件解压后可以得到page_csdn文件夹,用eclipse导入即可,运行之前请运行page.sql数据库脚本,我使用的是MySQL数据库。 部署到Tomcat...
jsp+jdbc+servlet+mysql数据库增删改查sql命令 分页查询等功能
本程序为JSP编程,实现连接MySql数据库,客户端分页显示。
经典 jsp mysql数据库 修改 分页功能 功能非常强大
用法:不用写连接数据库等多余代码(在ConnLink.java文件中修改一下相关属性就可以了,如果不想写分页,在ConnLink.java文件中DBlink()方法可以返回一个Statement对象,然后就可以利用这个返回值去写你想要的代码了...