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

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);
        }
    }
    
    
    
    打赏 评论

相关推荐 更多相似问题