ggx1abc
gu123xin
2018-08-01 02:26
采纳率: 95.2%
浏览 1.4k
已采纳

java读取mysql数据了过大,如何实现分页读取,求代码

sql = "SELECT id, name, url ,country FROM websites";/*需要从表里面读取的数据*/
ResultSet rs = stmt.executeQuery(sql);

String fn = "F://NEWtest//again1.txt";
wf(fn,"ID ,站点名称, 站点,country.");
while(rs.next()){

    int id  = rs.getInt("id");            
    String name = rs.getString("name");
    String url = rs.getString("url");
    String country = rs.getString("country");



    System.out.print("ID: " + id);

    System.out.print(", 站点名称: " + name);

    System.out.print(", 站点 URL: " + url);
    System.out.print(", country: " + country);

    System.out.print("\n");
    wf(fn,id+"," + name+ "," + url+ "," + country);
}
    这是读取数据库的代码,需要分页读取的代码,麻烦详细些,我刚刚接触java,谢谢
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

14条回答 默认 最新

  • qq_32126131
    qq_32126131 2018-08-01 02:39
    已采纳

    下面方法select(String page)的page是前台传到后台的 ,然后处理下page从数据库中分页顺序取出数据
    public JSONArray select(String page) {

        int limit = 10;
        JSONArray jsonArr = new JSONArray();
        Statement stmt = null;
        int start = (Integer.valueOf(page).intValue()-1) * limit;
        String sql = "SELECT id, name, url ,country FROM websites ORDER BY CREATTIME DESC LIMIT " + start + ",10;";
        ResultSet rs = stmt.executeQuery(sql);
                while(rs.next()){
    
    int id  = rs.getInt("id");            
    String name = rs.getString("name");
    String url = rs.getString("url");
    String country = rs.getString("country");
    
    
    
    System.out.print("ID: " + id);
    
    System.out.print(", 站点名称: " + name);
    
    System.out.print(", 站点 URL: " + url);
    System.out.print(", country: " + country);
    
    System.out.print("\n");
    wf(fn,id+"," + name+ "," + url+ "," + country);
    

    }
    }

    点赞 评论
  • qq_34360219
    陌小好 2018-08-01 02:34

    mysql 可以用limit

    点赞 评论
  • sysmwx
    倾世烟火为你而唱 2018-08-01 02:36

    SELECT id, name, url ,country FROM websites limit offset,limit
    例如:你要查询数据库结果集中1-5的数据可以 SELECT id, name, url ,country FROM websites limit 0,5 结果就是五条,其他依次类推
    offset是起始坐标,limit代表每页记录数

    如果你传的参数是页码如第一页,可以
    SELECT id, name, url ,country FROM websites limit (offset-1)* limit , limit

    点赞 评论
  • qq_20989105
    Little Coder 2018-08-01 02:37

    把sql = "SELECT id, name, url ,country FROM websites";
    改为sql = "SELECT id, name, url ,country FROM websites limit (pageNo-1)*pageSize, pageSize";
    pageNo为(页码)、pageSize(每页条数)
    其他的不需要怎么改

    点赞 评论
  • qq459805661
    蔡吉奏 2018-08-01 02:48

    没用框架吗?一般框架都有分页功能,使用JDBC分页的话MYSQL可以使用LIMIT,不要查所有数据,只查你需要展示的,这样速度就快了

    点赞 评论
  • batterMRTAN
    batterMRTAN 2018-08-01 02:55

    如果你只是显示部分代码到页面,使用LIMIT
    如果你数据量十分大,但都要查询出来写到文件,为避免内存溢出,可以多次查询,但还是用LIMIT限制查询数量

    点赞 评论
  • chenwucsdn
    chenwucsdn 2018-08-01 03:05

    sql = "SELECT id, name, url ,country FROM websites limit 1,10;##查询前10条数据

    点赞 评论
  • jql5528
    jql5528 2018-08-01 03:15

    limit 起始数 , 每页显示个数

    点赞 评论
  • HXNLYW
    葫芦胡 2018-08-01 03:51

    int pageSize = 10 ;// 每页显示条数(根据业务自行设置)
    int pageNumber = 1;//查询的页数
    int beginIndex = (pageNumber - 1) * pageSize;

    sql = "SELECT id, name, url ,country FROM websites limit beginIndex , pageSize;

    点赞 评论
  • qq_32332777
    vamViolet 2018-08-01 04:31

    首先。你需要一个pageBean对象
    /**
    * 查询结果并分页显示
    */
    @Override
    public Page queryMessageListForPageOne(Map params, int page, int pageSize) {
    int allRow = selectMessageListSize(params);// 总记录数
    int totalPage = PageBean.countTotalPage(pageSize, allRow);// 总页数
    final int offset = PageBean.countOffset(pageSize, page);// 当前页开始记录
    final int length = pageSize;// 每页记录数
    final int currentPage = PageBean.countCurrentPage(page);
    final int start = (currentPage - 1) * length;
    final int end = currentPage * length;

        StringBuffer sql = new StringBuffer("select * from hfvast_smg_2 o where 1 = 1 ");
        // 短信编号
        String id = replaceStr((String) params.get("id"));
        if (null != id && !"".equals(id)) {
            sql.append(" and  o.ID ='" + id + "' ");
        }
        // 目标号码
        String tagNum = replaceStr((String) params.get("tagNum"));
        if (null != tagNum && !"".equals(tagNum)) {
            sql.append("  and o.tagNum = '" + tagNum + "' ");
        }
    
        // 短信类型
        String type = replaceStr((String) params.get("type"));
        if (null != type && !"".equals(type)) {
            sql.append("  and  o.type = '" + type + "' ");
        }
        // 发送开始时间
        String startTime = replaceStr((String) params.get("startTime"));
        if (null != startTime && !"".equals(startTime)) {
            sql.append("  and  o.createdDate >= '" + startTime + " 00:00:00' ");
        }
        // 发送结束时间
        String endTime = replaceStr((String) params.get("endTime"));
        if (null != endTime && !"".equals(endTime)) {
            sql.append("  and  o.createdDate <= '" + endTime + " 23:59:59' ");
        }
    
        sql.append(" order by o.createdDate desc ");
    
        if (currentPage == 1) {
            sql.append(" LIMIT " + start + "," + end + " ");
        } else {
            sql.append(" LIMIT " + start + "," + length + " ");
        }
    
        Connection conn = null;
        PreparedStatement ptmt = null;
        ResultSet rss = null;
        Map messageMap = null;
        List<Map> list = new ArrayList<Map>();
        Page pageBean = new Page();
        try {
            conn = getOpenedConnection();
            ptmt = conn.prepareStatement(sql.toString());
            rss = ptmt.executeQuery();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            while (rss.next()) {
                messageMap = new HashMap();
    
                messageMap.put("id", rss.getString("o.id"));
                messageMap.put("serverNum", rss.getString("o.serverNum"));
                messageMap.put("tagNum", rss.getString("o.tagNum"));
                messageMap.put("content", rss.getString("o.content"));
                messageMap.put("msgFormat", rss.getString("o.msgFormat"));
                messageMap.put("type", rss.getString("o.type"));
                if (!"".equals(rss.getString("o.createdDate")) && null != rss.getString("o.createdDate")) {
                    messageMap.put("createdDate", sdf.parse(rss.getString("o.createdDate")));
                }
    
                list.add(messageMap);
            }
            pageBean = new Page(currentPage, pageSize, totalPage, list, allRow);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询短信分页信息异常" + e.getMessage(), e);
        } finally {
            JdbcManager.closeQuietly(rss);
            JdbcManager.closeQuietly(ptmt);
            closeConnection(conn);
        }
        return pageBean;
    }
    
    点赞 评论
  • zhaomin_g
    _zming 2018-08-01 05:46

    如果你需要在mybatis下分页,也可以看看这个博客:
    https://blog.csdn.net/zhaomin_g/article/details/81190016

    点赞 评论
  • cff775071456
    三目少爷 2018-08-01 12:04

    用limit字段 有个 beginIndex ,pageSize

    点赞 评论
  • weixin_42668285
    96张振东 2018-08-01 12:12
    点赞 评论
  • helam
    lamyuu 2018-08-01 03:25

    SELECT id, name, url ,country FROM websites limit 0,20 0是起始记数(从0开始),20是查询记录个数

    点赞 评论

相关推荐