JDBC的分页查询以及hibernate高级查询分页
2017-06-12 17:50
411 查看
JDBC的分页查询以及hibernate高级查询分页
第一种方法:、
BookBean.java
package com.mingrisoft.copy2; /** * Title: BookBean * Description: 数据库tb_book表对应实体类 * @author Peng * @date 下午7:39:41 */ public class BookBean { public static final int PAGE_SIZE = 2; // 每页记录数 private int id;// 编号 private String name;// 图书名称 private double price;// 定价 private int bookCount;// 数量 private String author;// 作者 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getBookCount() { return bookCount; } public void setBookCount(int bookCount) { this.bookCount = bookCount; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } }BookDao.java
package com.mingrisoft.copy2; 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/tb_Book"; String username = "root"; // 数据库用户名 String password = "root"; // 数据库密码 // 创建Connection连接 conn = DriverManager.getConnection(url, username, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; // 返回数据库连接 } /** * 分页查询所有商品信息 * * @param page * 页数 * @return List<Book> */ public List<BookBean> find(int page,int pageSize) { List<BookBean> list = new ArrayList<>(); // 创建List Connection conn = getConnection(); // 获取数据库连接 // 分页查询的SQL语句 String sql = "select * from tb_Book order by id asc limit ?,?"; try { PreparedStatement ps = conn.prepareStatement(sql); // 获取PreparedStatement ps.setInt(1, (page - 1) * pageSize); // 对SQL语句中的第1个参数赋值 ps.setInt(2, pageSize); // 对SQL语句中的第2个参数赋值 ResultSet rs = ps.executeQuery(); // 执行查询操作 while (rs.next()) { // 光标向后移动,并判断是否有效 BookBean b = new BookBean(); // 实例化BookBean b.setId(rs.getInt("id")); // 对id属性赋值 b.setName(rs.getString("name")); // 对name属性赋值 b.setPrice(rs.getFloat("price")); // 对price属性赋值 b.setBookCount(rs.getInt("bookCount")); // 对bookCount属性赋值 b.setAuthor(rs.getString("author")); // 对author属性赋值 list.add(b); // 将BookBean添加到List集合中 } rs.close(); // 关闭ResultSet ps.close(); // 关闭PreparedStatement conn.close(); // 关闭Connection } catch (SQLException e) { e.printStackTrace(); } return list; } /** * 查询总记录数 * * @return 总记录数 */ public int findCount() { int count = 0; // 总记录数 Connection conn = getConnection(); // 获取数据库连接 String sql = "select count(*) from tb_book"; // 查询总记录数SQL语句 try { Statement stmt = conn.createStatement(); // 创建Statement ResultSet rs = stmt.executeQuery(sql); // 查询并获取ResultSet if (rs.next()) { // 光标向后移动,并判断是否有效 count = rs.getInt(1); // 对总记录数赋值 } rs.close(); // 关闭ResultSet conn.close(); // 关闭Connection } catch (SQLException e) { e.printStackTrace(); } return count; // 返回总记录数 } }
bookList2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.*"%> <%@ page import="com.mingrisoft.copy2.*"%> <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>显示图书列表</title> <style type="text/css"> td,th { padding: 5px; background-color: #FFFFFF; } .div1{ width:98%; text-align: center; } .div2{ width:98%; text-align: center; padding:10px 0 0 0 ; } table{ width:98%; border:0; text-align: center; background-color: #666666; } </style> </head> <body> <div class="div1"> <h2>分页显示图书列表</h2> </div> <table align="center" border="0" bordercolor="#ffd306"> <tr> <th>ID</th> <th>图书名称</th> <th>价格</th> <th>数量</th> <th>作者</th> </tr> <form action="bookList2.jsp?" method="post"> <% String strPageNum = request.getParameter("pageNum"); int pageNum = 1; // 当前 页面 int pageSize = 2; // 每页的记录条数 int pageSum = 0; // 总页面数 int iCount ; if(strPageNum!=null) // 如果取到了数据,则为调用而来 { pageNum = Integer.parseInt(strPageNum); } BookDao bookDao = new BookDao(); List<BookBean> list2 = (List<BookBean>) bookDao.find(pageNum, pageSize); iCount = bookDao.findCount(); // 记录总数 // 计算总页面数 pageSum = iCount/pageSize; if(iCount%pageSize!=0){ pageSum+=1; } if(list2.size()>0){ for(BookBean book:list2){ %> <tr> <td><%=book.getId()%></td> <td><%=book.getName()%></td> <td><%=book.getPrice()%></td> <td><%=book.getBookCount()%></td> <td><%=book.getAuthor()%></td> </tr> <% } } %> <tr><td colspan="9" align="center"> 每页 <input type="text" value="<%=pageSize%>" size="1"> 条记录 <% out.print(" 当前第["+pageNum+"] 页,共["+pageSum+"] 页,共 "+iCount+" 条记录"); if(pageNum<2){ %> 上一页 <% }else{ %> <a href="bookList2.jsp?pageNum=<%=pageNum-1 %>" style="color:#009"> 上一页</a> <% } %> <% if(pageNum>=pageSum){ %> 下一 页 <% }else{ %> <a href="bookList2.jsp?pageNum=<%=pageNum+1 %>" style="color:#009" > 下一页</a> <% } %> 跳转 <input type="text" value="<%=pageNum%>" name="pageNum" size="1"> 页</a> <input type="submit" value="确定"> </form> </td></tr> </table> </body> </html>最后请不要忘记添加数据驱动jar包
当然也有另外一种样式:第二种方法:
BookBean.java
package com.mingrisoft; /** * Title: BookBean * Description: 数据库tb_book表对应实体类 * @author Peng * @date 下午7:39:41 */ public class BookBean { public static final int PAGE_SIZE = 2; // 每页记录数 private int id;// 编号 private String name;// 图书名称 private double price;// 定价 private int bookCount;// 数量 private String author;// 作者 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getBookCount() { return bookCount; } public void setBookCount(int bookCount) { this.bookCount = bookCount; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } }
BookDao.java
package com.mingrisoft; 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/tb_Book"; String username = "root"; // 数据库用户名 String password = "root"; // 数据库密码 // 创建Connection连接 conn = DriverManager.getConnection(url, username, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; // 返回数据库连接 } /** * 分页查询所有商品信息 * * @param page * 页数 * @return List<Book> */ public List<BookBean> find(int page) { List<BookBean> list = new ArrayList<>(); // 创建List Connection conn = getConnection(); // 获取数据库连接 // 分页查询的SQL语句 String sql = "select * from tb_Book order by id asc limit ?,?"; try { PreparedStatement ps = conn.prepareStatement(sql); // 获取PreparedStatement ps.setInt(1, (page - 1) * BookBean.PAGE_SIZE); // 对SQL语句中的第1个参数赋值 ps.setInt(2, BookBean.PAGE_SIZE); // 对SQL语句中的第2个参数赋值 ResultSet rs = ps.executeQuery(); // 执行查询操作 while (rs.next()) { // 光标向后移动,并判断是否有效 BookBean b = new BookBean(); // 实例化BookBean b.setId(rs.getInt("id")); // 对id属性赋值 b.setName(rs.getString("name")); // 对name属性赋值 b.setPrice(rs.getFloat("price")); // 对price属性赋值 b.setBookCount(rs.getInt("bookCount")); // 对bookCount属性赋值 b.setAuthor(rs.getString("author")); // 对author属性赋值 list.add(b); // 将BookBean添加到List集合中 } rs.close(); // 关闭ResultSet ps.close(); // 关闭PreparedStatement conn.close(); // 关闭Connection } catch (SQLException e) { e.printStackTrace(); } return list; } /** * 查询总记录数 * * @return 总记录数 */ public int findCount() { int count = 0; // 总记录数 Connection conn = getConnection(); // 获取数据库连接 String sql = "select count(*) from tb_book"; // 查询总记录数SQL语句 try { Statement stmt = conn.createStatement(); // 创建Statement ResultSet rs = stmt.executeQuery(sql); // 查询并获取ResultSet if (rs.next()) { // 光标向后移动,并判断是否有效 count = rs.getInt(1); // 对总记录数赋值 } rs.close(); // 关闭ResultSet conn.close(); // 关闭Connection } catch (SQLException e) { e.printStackTrace(); } return count; // 返回总记录数 } }FindServlet.java
package com.mingrisoft; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet实现类FindServlet */ @WebServlet("/") // 配置Servlet为默认执行页 public class FindServlet extends HttpServlet { private static final long serialVersionUID = 1L; public FindServlet() { super(); } /** * 执行GET请求的方法 */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int currPage = 1; // 当前页码 if (request.getParameter("page") != null) { // 判断传递页码是否有效 currPage = Integer.parseInt(request.getParameter("page"));// 对当前页码赋值 } BookDao dao = new BookDao(); // 实例化BookDao List<BookBean> list = dao.find(currPage); // 查询所有图书信息 request.setAttribute("list", list); // 将list放置到request中 int pages; // 总页数 int count = dao.findCount(); // 查询总记录数 if (count % BookBean.PAGE_SIZE == 0) { // 计算总页数,总记录数和每页显示的数 pages = count / BookBean.PAGE_SIZE; // 对总页数赋值 } else { pages = count / BookBean.PAGE_SIZE + 1; // 对总页数赋值 } StringBuffer sb = new StringBuffer(); // 实例化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.setAttribute("bar", sb.toString()); // 将分页导航条的字符串放置到request中 // 转发到bookList.jsp页面 request.getRequestDispatcher("bookList.jsp").forward(request, response); } }
bookList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.*"%> <%@ page import="com.mingrisoft.BookBean"%> <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>显示图书列表</title> <style type="text/css"> td,th { padding: 5px; background-color: #FFFFFF; } .div1{ width:98%; text-align: center; } .div2{ width:98%; text-align: center; padding:10px 0 0 0 ; } table{ width:98%; border:0; text-align: center; background-color: #666666; } </style> </head> <body> <div class="div1"> <h2>分页显示图书列表</h2> </div> <table cellpadding="0" cellspacing="1" > <tr> <th>ID</th> <th>图书名称</th> <th>价格</th> <th>数量</th> <th>作者</th> </tr> <% // 获取图书信息集合 List<BookBean> list = (List<BookBean>) request.getAttribute("list"); // 判断集合是否有效 if (list == null || list.size() < 1) { out.print("<tr><td colspan='5'>没有任何图书信息!</td></tr>"); } else { // 遍历图书集合中的数据 for (BookBean book : list) { %> <tr align="center"> <td><%=book.getId()%></td> <td><%=book.getName()%></td> <td><%=book.getPrice()%></td> <td><%=book.getBookCount()%></td> <td><%=book.getAuthor()%></td> </tr> <% } } %> </table> <div class="div2"> <%=request.getAttribute("bar")%> </div> </body> </html>效果图:
下面讲hibernate分页查询
hibernate分页
相关文章推荐
- JDBC实现数据库的几种基本操作(查询,分页查询,根据关键字进行查询以及插入数据)
- JDBC实现增删改查、模糊查询、分页查询、子查询以及体现单例设计模式连接数据库
- org.hibernate.exception.GenericJDBCException: Cannot open cconnection(执行几次查询之后就不能查询/页面动不了/分页查询只能点击几)
- hibernate实现单个对象的增删改查,以及实现分页查询
- Hibernate(二)HQL&SQL参数绑定、投影和分页以及命名查询
- J2EE项目系列(二)--博客管理系统(Maven+SpringMVC+Hibernate以及附加分页和一对多查询功能)
- hibernate和Jdbc查询的区别以及hibernate查询普通查询和Hql
- Hibernate(二)HQL&SQL参数绑定、投影和分页以及命名查询
- Hibernate 高级查询技巧——分页查询
- Hibernate实现分页查询的原理分析
- Hibernate实现分页查询的原理分析
- 实现Hibernate分页查询原理解读
- Hibernate 高级查询技巧——集合过滤与子查询
- 实现Hibernate分页查询原理解读
- Hibernate 实现分页查询 [转]
- Hibernate 高级查询技巧——本地SQL查询
- 实现Hibernate分页查询原理解读
- Hibernate3的DetachedCriteria实现分页查询
- Hibernate 可以实现分页查询
- 实现Hibernate分页查询原理解读