zoukankan      html  css  js  c++  java
  • 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 }

     输出

  • 相关阅读:
    php 创建多级文件夹
    php 格式化文件大小
    php 微信授权登录
    获取单据编号 不重复 骚
    js获取数组中的最后一个
    php截取
    循序栈
    链表
    顺序表
    halcon学习之阈值算子threshold operators
  • 原文地址:https://www.cnblogs.com/JasperZhao/p/13539268.html
Copyright © 2011-2022 走看看