JDBC的分页查询
首先我们要知道为什么要分页?分页有什么好处呢?
1、提高性能,一次查20个,比一次查20000个性能肯定更好;另外如果数据量很大,一次性将内容都查询出来,查询出来的结果是放在内存里面的,内存没有这么大 2、不需要这么多数据,如新闻,一般人可能只看最近前20条;如果我们将后面的也都查询出来了,就是浪费 3、展现层面的考虑:如果一次展现太多的数据,不管是排版,还是美观上都不好
下面我就来以一个例子来实现JDBC的分页查询
首先我们需要建立数据库表格,我以mysql数据库为例:
+-----------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+----------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | char(20) | YES | | NULL | |
| price | double | YES | | NULL | |
| bookCount | int(11) | YES | | NULL | |
| author | char(20) | YES | | NULL | |
+-----------+----------+------+-----+---------+-------+
Mysql语句如下:
CREATE table tb_book
(id int,
name char(20),
price DOUBLE,
bookCount int,
author char(20));
下面我们给已经建好的表格添加数据:
+----+----------+-------+-----------+--------+
| id | name | price | bookCount | author |
+----+----------+-------+-----------+--------+
| 1 | English | 12.5 | 10 | kimi |
| 2 | china | 12.5 | 10 | tom |
| 3 | math | 12.5 | 10 | lilei |
| 4 | computer | 12.5 | 10 | rose |
+----+----------+-------+-----------+--------+
添加语句为:
INSERT INTO book VALUES ('1','English','12.5','10','kimi');
INSERT INTO book VALUES ('2','china','12.5','10','tom');
INSERT INTO book VALUES ('3','math','12.5','10','lilei');
INSERT INTO book VALUES ('4','computer','12.5','10','rose');
下面我们来写java语句:
package jdbc; 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; } @Override public String toString() { return "BookBean [id=" + id + ", name=" + name + ", price=" + price + ", bookCount=" + bookCount + ", author=" + author + "]"; } }相关的数据操作类
package jdbc; import java.sql.CallableStatement; 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 { List<BookBean> list = new ArrayList<BookBean>(); public Connection getConnection() { Connection conn = null; // 数据库连接 try { Class.forName("com.mysql.jdbc.Driver"); // 加载数据库驱动,注册到驱动管理器 // 数据库连接字符串 String url = "jdbc:mysql://localhost:3306/emp"; String username = "root"; // 数据库用户名 String password = "123"; // 数据库密码 // 创建Connection连接 conn = DriverManager.getConnection(url, username, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; // 返回数据库连接 } public void find(int page) { // 创建List Connection conn = getConnection(); // 获取数据库连接 // 分页查询的SQL语句 String sql = "select * from 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(); } } 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; // 返回总记录数 } } 测试类 package jdbc; import jdbc.BookDao; public class Test { public static void main(String[] args) { BookDao bb = new BookDao(); bb.find(1); System.out.println(bb.list.toString()); } }本人初学java,希望大家多多指正,O(∩_∩)O谢谢。