分页查询信息eclipse+jdbc+mysql_第1页
分页查询信息eclipse+jdbc+mysql_第2页
分页查询信息eclipse+jdbc+mysql_第3页
分页查询信息eclipse+jdbc+mysql_第4页
分页查询信息eclipse+jdbc+mysql_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

分页查询信息使用jdbc连接mysql数据库实现分页查询任务通过mysql数据库提供的分页机制,实现商品信息的分页查询功能,将查询到的信息显示到jsp页面上。本项目时一个简单的运用eclipse+jdbc+mysql的小程序。连接的数据库名称为db_database11,属性如下:1.创建名为com.pmf.bean的包,包中是名为Product的类,用于封装商品信息。全部代码如下:packagecom.pmf.bean;/***商品**/publicclassProduct{ publicstaticfinalintPAGE_SIZE=2; //编号 privateintid; //名称 privateStringname; //价格 privatedoubleprice; //数量 privateintnum; //单位 privateStringunit; publicintgetId(){ returnid; } publicvoidsetId(intid){ this.id=id; } publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ =name; } publicdoublegetPrice(){ returnprice; } publicvoidsetPrice(doubleprice){ this.price=price; } publicintgetNum(){ returnnum; } publicvoidsetNum(intnum){ this.num=num; } publicStringgetUnit(){ returnunit; } publicvoidsetUnit(Stringunit){ this.unit=unit; }}创建名为“BookDao”的类,主要用于封装商品数据库的相关操作。在BookDao类中首先编写getConnection()方法,用于创建Connection对象。接着创建商品信息的分页查询方法find(),其中page参数用于传递要查询的页码。在分页查询过程中还需要获取信息的总记录数,用于计算商品信息的总页数。此方法写在findCount()方法中。代码如下:packagecom.pmf.bean;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;importjava.util.ArrayList;importjava.util.List; *查询总记录数 *@return总记录数 */ publicintfindCount(){ //总记录数 intcount=0; //获取数据库连接 Connectionconn=getConnection(); //查询总记录数SQL语句 Stringsql="selectcount(*)fromtb_product"; try{ //创建Statement Statementstmt=conn.createStatement(); //查询并获取ResultSet ResultSetrs=stmt.executeQuery(sql); //光标向后移动,并判断是否有效 if(rs.next()){ //对总记录数赋值 count=rs.getInt(1); } //关闭ResultSet rs.close(); //关闭Connection conn.close(); }catch(SQLExceptione){ e.printStackTrace(); } //返回总记录数 returncount; }}创建一个名为“FindServlet”的类位于com.pmf.servlet中。此类是分页查询商品信息的Servlet对象。在该类中写doGet()方法处理分页请求。代码如下:packagecom.pmf.servlet;importjava.io.IOException;importjava.util.List;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importcom.pmf.bean.Product;importcom.pmf.bean.BookDao;/***ServletimplementationclassFindServlet*/publicclassFindServletextendsHttpServlet{ privatestaticfinallongserialVersionUID=1L; protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ //当前页码 intcurrPage=1; //判断传递页码是否有效 if(request.getParameter("page")!=null){ //对当前页码赋值 currPage=Integer.parseInt(request.getParameter("page")); } //实例化ProductDao BookDaodao=newBookDao(); //查询所有商品信息 List<Product>list=dao.find(currPage); //将list放置到request之中 request.setAttribute("list",list); //总页数 intpages; //查询总记录数 intcount=dao.findCount(); //计算总页数 if(count%Product.PAGE_SIZE==0){ //对总页数赋值 pages=count/Product.PAGE_SIZE; }else{ //对总页数赋值 pages=count/Product.PAGE_SIZE+1; } //实例化StringBuffer StringBuffersb=newStringBuffer(); //通过循环构建分页条 for(inti=1;i<=pages;i++){ //判断是否为当前页 if(i==currPage){ //构建分页条 sb.append("『"+i+"』"); }else{ //构建分页条 sb.append("<ahref='FindServlet?page="+i+"'>"+i+"</a>"); } //构建分页条 sb.append(""); } //将分页条的字符串放置到request之中 request.setAttribute("bar",sb.toString()); //转发到product_list.jsp页面 request.getRequestDispatcher("product_list.jsp").forward(request,response); }}创建product_list.jsp页面,此页面通过获取查询结果List与分页条来分页显示商品的数据。代码如下:<%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""/TR/html4/loose.dtd"><%@pageimport="java.util.List"%><%@pageimport="com.pmf.bean.Product"%><html><head><metahttp-equiv="Content-Type"content="text/html;charset=UTF-8"><title>所有商品信息</title><styletype="text/css"> td{font-size:12px;} h2{margin:0px}</style></head><body><tablealign="center"width="450"border="1"height="180"bordercolor="white"bgcolor="black"cellpadding="1"cellspacing="1"> <trbgcolor="white"> <tdalign="center"colspan="5"> <h2>所有商品信息</h2> </td> </tr> <tralign="center"bgcolor="#e1ffc1"> <td><b>ID</b></td> <td><b>商品名称</b></td> <td><b>价格</b></td> <td><b>数量</b></td> <td><b>单位</b></td> </tr> <% List<Product>list=(List<Product>)request.getAttribute("list"); for(Productp:list){ %> <tralign="center"bgcolor="white"> <td><%=p.getId()%></td> <td><%=p.getName()%></td> <td><%=p.getPrice()%></td> <td><%=p.getNum()%></td> <td><%=p.getUnit()%></td> </tr> <% } %> <tr> <tdalign="center"colspan="5"bgcolor="white"> <%=request.getAttribute("bar")%> </td> </tr></table></body></html>编写程序中的主页index.jsp,在其中编写分页查询商品信息的超链接指向FindServlet.代码如下:<%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%><!

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论