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

下载本文档

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

文档简介

分页查询信息 使用jdbc连接mysql数据库实现分页查询任务通过mysql数据库提供的分页机制,实现商品信息的分页查询功能,将查询到的信息显示到jsp页面上。本项目时一个简单的运用eclipse+jdbc+mysql的小程序。连接的数据库名称为db_database11,属性如下: 1.创建名为com.pmf.bean的包,包中是名为Product的类,用于封装商品信息。全部代码如下:package com.pmf.bean;/* * 商品 * */public class Product public static final int PAGE_SIZE = 2;/ 编号private int id;/ 名称private String name;/ 价格private double price;/ 数量private int num;/ 单位private String unit;public int getId() return id;public void setId(int id) this.id = id;public String getName() return name;public void setName(String name) = name;public double getPrice() return price;public void setPrice(double price) this.price = price;public int getNum() return num;public void setNum(int num) this.num = num;public String getUnit() return unit;public void setUnit(String unit) this.unit = unit;2. 创建名为“BookDao”的类,主要用于封装商品数据库的相关操作。在BookDao类中首先编写getConnection()方法,用于创建Connection对象。接着创建商品信息的分页查询方法find(),其中page参数用于传递要查询的页码。在分页查询过程中还需要获取信息的总记录数,用于计算商品信息的总页数。此方法写在findCount()方法中。代码如下:package com.pmf.bean;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;import java.util.List;/* * 商品数据库操作 * */public class BookDao /* * 获取数据库连接 * return Connection对象 */public Connection getConnection()/ 数据库连接Connection conn = null;try / 加载数据库驱动,注册到驱动管理器Class.forName(com.mysql.jdbc.Driver);/ 数据库连接字符串String url = jdbc:mysql:/localhost:3306/db_database11;/ 数据库用户名String username = root;/ 数据库密码String password = 123123;/ 创建Connection连接conn = DriverManager.getConnection(url,username,password); catch (ClassNotFoundException e) e.printStackTrace(); catch (SQLException e) e.printStackTrace();/ 返回数据库连接return conn;/* * 分页查询所有商品信息 * param page 页数 * return ListProduct */public ListProduct find(int page)/ 创建ListListProduct list = new ArrayListProduct();/ 获取数据库连接Connection conn = getConnection();/ 分页查询的SQL语句String sql = select * from tb_product order by id desc limit ?,?;try / 获取PreparedStatementPreparedStatement ps = conn.prepareStatement(sql);/ 对SQL语句中的第1个参数赋值ps.setInt(1, (page - 1) * Product.PAGE_SIZE);/ 对SQL语句中的第2个参数赋值ps.setInt(2, Product.PAGE_SIZE);/ 执行查询操作ResultSet rs = ps.executeQuery();/ 光标向后移动,并判断是否有效while(rs.next()/ 实例化ProductProduct p = new Product();/ 对id属性赋值p.setId(rs.getInt(id);/ 对name属性赋值p.setName(rs.getString(name);/ 对num属性赋值p.setNum(rs.getInt(num);/ 对price属性赋值p.setPrice(rs.getDouble(price);/ 对unit属性赋值p.setUnit(rs.getString(unit);/ 将Product添加到List集合中list.add(p);/ 关闭ResultSetrs.close();/ 关闭PreparedStatementps.close();/ 关闭Connectionconn.close(); catch (SQLException e) e.printStackTrace();return list;/* * 查询总记录数 * return 总记录数 */public int findCount()/ 总记录数int count = 0;/ 获取数据库连接Connection conn = getConnection();/ 查询总记录数SQL语句String sql = select count(*) from tb_product;try / 创建StatementStatement stmt = conn.createStatement();/ 查询并获取ResultSetResultSet rs = stmt.executeQuery(sql);/ 光标向后移动,并判断是否有效if(rs.next()/ 对总记录数赋值count = rs.getInt(1);/ 关闭ResultSetrs.close();/ 关闭Connectionconn.close(); catch (SQLException e) e.printStackTrace();/ 返回总记录数return count;3. 创建一个名为“FindServlet”的类位于com.pmf.servlet中。此类是分页查询商品信息的Servlet对象。在该类中写doGet()方法处理分页请求。代码如下:package com.pmf.servlet;import java.io.IOException;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.pmf.bean.Product;import com.pmf.bean.BookDao;/* * Servlet implementation class FindServlet */public class FindServlet extends HttpServlet private static final long serialVersionUID = 1L;protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException / 当前页码int currPage = 1;/ 判断传递页码是否有效if(request.getParameter(page) != null)/ 对当前页码赋值currPage = Integer.parseInt(request.getParameter(page);/ 实例化ProductDaoBookDao dao = new BookDao();/ 查询所有商品信息ListProduct list = dao.find(currPage);/ 将list放置到request之中request.setAttribute(list, list);/ 总页数int pages ;/ 查询总记录数int count = dao.findCount();/ 计算总页数if(count % Product.PAGE_SIZE = 0)/ 对总页数赋值pages = count / Product.PAGE_SIZE;else/ 对总页数赋值pages = count / Product.PAGE_SIZE + 1;/ 实例化StringBufferStringBuffer sb = new StringBuffer();/ 通过循环构建分页条for(int i=1; i = pages; i+)/ 判断是否为当前页if(i = currPage)/ 构建分页条sb.append( + i + );else/ 构建分页条sb.append(a href=FindServlet?page= + i + + i + /a);/ 构建分页条sb.append();/ 将分页条的字符串放置到request之中request.setAttribute(bar, sb.toString();/ 转发到product_list.jsp页面request.getRequestDispatcher(product_list.jsp).forward(request, response);4. 创建product_list.jsp页面,此页面通过获取查询结果List与分页条来分页显示商品的数据。代码如下:% page language=java contentType=text/html; charset=UTF-8 pageEncoding=UTF-8%!DOCTYPE html PUBLIC -/W3C/DTD HTML 4.01 Transitional/EN /TR/html4/loose.dtd%page import=java.util.List%page import=com.pmf.bean.Product%htmlheadmeta http-equiv=Content-Type content=text/html; charset=UTF-8title所有商品信息/titlestyle type=text/csstdfont-size: 12px;h2margin: 0px/style/headbodytable align=center width=450 border=1 height=180 bordercolor=white bgcolor=black cellpadding=1 cellspacing=1tr bgcolor=whitetd align=center colspan=5h2所有商品信息/h2/td/trtr align=center bgcolor=#e1ffc1 tdbID/b/tdtdb商品名称/b/tdtdb价格/b/tdtdb数量/b/tdtdb单位/b/td/tr%ListProduct list = (ListProduct)request.getAttribute(list);for(Product p : list)%tr align=center bgcolor=whitetd%=p.getId()%/tdtd%=p.getName()%/tdtd%=p.getPrice()%/tdtd%=p.getNum()%/tdtd%=p.getUnit()%/td/tr%trtd align=center colspan=5 bgcolor=white%=request.getAttribute(bar)%/td/tr/table/body/html5. 编写程序中的主页index.jsp,在其中编写分页查询商品信息的超链接指向FindServlet.代码如下:% page language=

温馨提示

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

评论

0/150

提交评论