How to read all rows from huge table?

前端 未结 6 1506
长发绾君心
长发绾君心 2020-11-28 03:11

I have a problem with processing all rows from database (PostgreSQL). I get an error: org.postgresql.util.PSQLException: Ran out of memory retrieving query results.

6条回答
  •  感动是毒
    2020-11-28 03:33

    I did it like below. Not the best way i think, but it works :)

        Connection c = DriverManager.getConnection("jdbc:postgresql://....");
        PreparedStatement s = c.prepareStatement("select * from " + tabName + " where id > ? order by id");
        s.setMaxRows(100);
        int lastId = 0;
        for (;;) {
            s.setInt(1, lastId);
            ResultSet rs = s.executeQuery();
    
            int lastIdBefore = lastId;
            while (rs.next()) {
                lastId = Integer.parseInt(rs.getObject(1).toString());
                // ...
            }
    
            if (lastIdBefore == lastId) {
                break;
            }
        }
    

提交回复
热议问题