JDBC 03: 分页查询

1.  SQL查询语句

SELECT * FROM web01.user limit X,Y; (从第【X】行开始, 查询Y行)

2.  实现

 1 public static boolean selectByPage(int pageNum, int pageSize) {
 2         Connection con = null;
 3         Statement stmt = null;
 4         ResultSet rs = null;
 5         
 6         try {
 7             Class.forName("com.mysql.jdbc.Driver");
 8             
 9             String url = "jdbc:mysql://localhost:3306/web01?useUnicode=true&CharacterEncoding=UTF8&useSSL=false";
10             con = DriverManager.getConnection(url,"root","root");
11             
12             String sql = "select * from user limit ?,?";
13             PreparedStatement pstmt = con.prepareStatement(sql);
14             
15             pstmt.setInt(1, (pageNum-1)*pageSize);
16             pstmt.setInt(2, pageSize);   
17 rs = pstmt.executeQuery(); 19 while(rs.next()) { 20 System.out.println(rs.getInt("id")+","+rs.getString("username")+","+rs.getString("password")); 21 } 22 23 }catch (Exception e) { 24 e.printStackTrace(); 25 } finally {
          // Omitted
26 }48 return false; 49 }

 

 输出

 

posted @ 2020-08-21 09:22  Jasper2003  阅读(179)  评论(0编辑  收藏  举报