gu123xin 2018-08-01 06:56 采纳率: 95.2%
浏览 2785
已采纳

java用limit循环读取mysql直到全部读完,并能显示读了多少条

数据库数据很多,每次读5000条左右,用分页的方式循环读完,下面是测试代码,现在需要分页的代码,可以写在我发的测试代码上面,尽量详细,谢谢。
package webtest;
import java.sql.*;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.File;

public class test5{

static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";

static final String DB_URL = "jdbc:mysql://localhost:3306/gumysql";

static final String USER = "root";
static final String PASS = "123456";
public static final String FILE_NAME = "again1.txt";//要创建的文件名
public static final String fn = "F:/NEWtest/";//文件指定存放的路径
public static void creatFile(String fn, String fileName) {
File folder = new File(fn);
//文件夹路径不存在
if (!folder.exists() && !folder.isDirectory()) {
System.out.println("文件夹路径不存在,创建路径:" + fn);
folder.mkdirs();
} else {
System.out.println("文件夹路径存在:" + fn);
}

// 如果文件不存在就创建
File file = new File(fn + fileName);
if (!file.exists()) {
    System.out.println("文件不存在,创建文件:" + fn+ fileName);
    try {
        file.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }
} else {
    System.out.println("文件已存在,文件为:" + fn+ fileName);
}

}

public static void wf(String file, String conent) {
BufferedWriter out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(file, true)));
out.write(conent+"\r\n");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
creatFile(fn, FILE_NAME);
Class.forName("com.mysql.jdbc.Driver");

System.out.println("连接数据库...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);


System.out.println(" 实例化Statement对象...");
stmt = conn.createStatement();
String sql;
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);
}

rs.close();
stmt.close();
conn.close();

}catch(SQLException se){

se.printStackTrace();

}catch(Exception e){

e.printStackTrace();

}finally{

try{
    if(stmt!=null) stmt.close();
}catch(SQLException se2){
}
try{
    if(conn!=null) conn.close();
}catch(SQLException se){
    se.printStackTrace();
}

}
System.out.println("Goodbye!");
}

}

  • 写回答

4条回答 默认 最新

  • crazydddfz 2018-08-01 10:04
    关注

    可以先查询表里所有数据的总数 sumNum;
    SELECT count(*) FROM websites;
    System.out.print("共获取到"+sumNum+"条数据");
    再根据得到的总数值去除以5000,再用总数求余,这样获取循环的次数pageSize
    int pageSize = 0;
    pageSize = sumNum / 5000;
    int tmpPage = 0;
    tmpPage = sumNum % 5000 == 0 ? 0 : 1;
    pageSize = pageSize + tmpPage;
    然后循环
    for(int i=0;i<=pageSize;i++){
    // 处理逻辑查询
    SELECT id, name, url ,country FROM websites limit i*5000,5000;
    // 遍历ResultSet 得到数据去处理
    }
    既然分页查询那就要建立很多连接,注意关闭

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • cashT 2018-08-01 07:15
    关注

    ResultSet rs 这种结果集不支持滚动的读去功能,所以,如果获得这样一个结果集,只能使用它里面的next()方法,逐个的读去数据;
    建议你自己new 一个新的List 在你使用next()方法的时候将获取到的逐个添加进List; 之后使用list.subList(fromIndex , toIndex)方法实现分页;
    参数代表下标区间,左闭右开.

    评论
  • cavehe 2018-08-01 15:13
    关注
    评论
  • _zming 2018-08-02 01:44
    关注
    package test;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Types;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    import java.util.concurrent.atomic.AtomicInteger;
    
    public class DBUtil {
    
        public static void closeResult(ResultSet rs) {
            try {
                if (rs != null) {
                    rs.close();
                }
            } catch (Exception e) {
            }
        }
    
        public static void closeStmt(PreparedStatement stmt) {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (Exception e) {
            }
        }
    
        public static void closeConn(Connection con) {
            try {
                if (con != null) {
                    con.close();
                }
            } catch (Exception e) {
            }
        }
    
        public static Connection openConn() {//创建连接,自己写
            return null;
        }
    
        public static int getTotalCount(Connection con, String sql, Object... params) {
            PreparedStatement stmt = null;
            ResultSet rs = null;
            try {
                 stmt = con.prepareStatement(sql);
                 int idx = 1;
                 if (params != null) {
                     for (Object param : params) {
                         if (param == null) {
                             stmt.setObject(idx++, param, Types.VARCHAR);
                         } else {
                             stmt.setObject(idx++, param);
                         }
                     }
                 }
                rs = stmt.executeQuery();
                if (rs.next()) {
                    return rs.getInt(1);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                closeResult(rs);
                closeStmt(stmt);
            }
            return 0;
        }
    
        public static <T> List<T> queryList(Connection con, String sql, ResultMapping<T> mapper, Object... params) {
            PreparedStatement stmt = null;
            ResultSet rs = null;
            List<T> list = new ArrayList<T>();
            try {
                 stmt = con.prepareStatement(sql);
                 int idx = 1;
                 if (params != null) {
                     for (Object param : params) {
                         if (param == null) {
                             stmt.setObject(idx++, param, Types.VARCHAR);
                         } else {
                             stmt.setObject(idx++, param);
                         }
                     }
                 }
                rs = stmt.executeQuery();
                while (rs.next()) {
                    list.add(mapper.convert(rs));
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                closeResult(rs);
                closeStmt(stmt);
            }
            return list;
        }
    
        public interface ResultMapping<T> {
            public T convert(ResultSet rs);
        }
    
        public static void main(String[] args) {
            String sql = "select * from t";
            String totlaSql = "select count(1) from (" + sql + ") as tmp";
            String pagesql = "select * from (" + sql + ") as tmp limit ?, ?";
            System.out.println(sql);System.out.println(totlaSql);System.out.println(pagesql);
            int pageSize = 100;
            Connection con = openConn();
            int total = getTotalCount(con, totlaSql);
            int fetchCount = (int) Math.floor(total * 1.0/pageSize);
            final AtomicInteger readcount = new AtomicInteger();
            ResultMapping mapper = new ResultMapping<Map>() {//假设查询的是map类型
    
                @Override
                public Map convert(ResultSet rs) {
                    //这里解析取rs,取出来想怎么弄怎么弄,如果关系返回值就返回
                    readcount.incrementAndGet();
                    return null;
                }
            };
            for (int i = 0; i < fetchCount; i++) {
                queryList(con, pagesql,mapper , i * pageSize, pageSize, i * pageSize, pageSize);
            }
    
            closeConn(con);
        }
    }
    
    
    
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 很简单的c#代码补全
  • ¥15 按要求用C语言编出程序,要求标明注释,数据可以不用打出来,只要求有程序就行,没办法了在等待中,有没有人帮个忙,明天就要求要
  • ¥15 复杂表达式求值程序里的函数优先级问题
  • ¥15 求密码学的客成社记ji着用
  • ¥35 POI导入树状结构excle
  • ¥15 初学者c语言题目解答
  • ¥15 div editable中的光标问题
  • ¥15 mysql报错1415Not allowed to return a result set from a trigger 不知如何修改
  • ¥60 Python输出Excel数据整理,算法较为复杂
  • ¥15 回答几个问题 关于数据库