sql 分页查询

	public List<Student> findAllStudent(int page){
		List<Student>list = new ArrayList<>();
		     
		    try {
		        Class.forName("com.mysql.jdbc.Driver");
		    } catch (ClassNotFoundException e1) {
		        e1.printStackTrace();
		    }
		     
		    try (Connection connection = DriverManager.getConnection(url, user, pass)) {
		        int size =2;
		         
		        String sql = "SELECT * FROM student order by id asc limit ?,?";
		        java.sql.PreparedStatement statement = connection.prepareStatement(sql);
		 
		        int p = (page-1)*size;//分页id算法,用于计算开始id
		         
		        statement.setInt(1, p);
		        statement.setInt(2,size);
		 
		    } catch (SQLException e) {
		        e.printStackTrace();
		    }
		    return list;
	}

  

原文地址:https://www.cnblogs.com/max-hou/p/12090741.html