壹个丶蓝人 2019-10-21 16:52 采纳率: 0%
浏览 475
已采纳

调用webService接口将大量数据分批上传,在Java中实现分批会造成内存崩盘吗?比如说这个代码。———

没用插件,分页之类的,就直接用代码实现。。。

    private int uploadDataType_1() throws SQLException{
        //获取数据
        ResultSet resultSet = DBConnection.executeQuery(
                "SELECT * from TB_StoreHouse WHERE provice_id ='430000'"
                );
        //数据集结构
        ResultSetMetaData reMetaData = resultSet.getMetaData();
        System.out.println("数据集的结构---"+reMetaData);  
        //数据集的列数
        int columnCount = reMetaData.getColumnCount();  
        System.out.println("数据集的列数---"+columnCount);
        JSONArray array = new JSONArray();
        int responseCode=0;
        /*
         * int a 分批上传  本批次数据的位数
         * 
         * int b 为总共上传几批次
        */
        int a = 0;
        int b =0;
        boolean  result=true;
        while ( result= resultSet.next()) {
            //转json
            JSONObject jsonObject = new JSONObject();
            if (++a<200) {
                for (int i = 1; i <= columnCount; i++) {
                    String columnLabel = reMetaData.getColumnLabel(i);
                    String value = resultSet.getString(columnLabel);
                    jsonObject.put(columnLabel, value);
                }
            array.add(jsonObject);
            }else {
                for (int i = 1; i <= columnCount; i++) {
                    String columnLabel = reMetaData.getColumnLabel(i);
                    String value = resultSet.getString(columnLabel);
                    jsonObject.put(columnLabel, value);
                }
                array.add(jsonObject);
                //调用ws
                GrainServiceImplService rs = new GrainServiceImplService();
                GrainService re = rs.getGrainServiceImplPort();
                responseCode =re.test(null, "1", 1, "3", "4");
                System.out.println("上传第"+ ++b +"批数据,上传累计条数:"+b*200+"条");
                a=0;
                array= new JSONArray();
                System.out.println("执行结果为 " + responseCode);
            }
        }
        if (result) {
        }else {
            //调用ws
            GrainServiceImplService rs = new GrainServiceImplService();
            GrainService re = rs.getGrainServiceImplPort();
            responseCode=   re.test(null, "1", 1, "3", "4");
            int c=a+(b*200);
            System.out.println("本批次数据不满20条,共"+ c +"条");
            System.out.println("整表数据传输完成");
            System.out.println("执行结果为" + responseCode);
        }
        return responseCode;
    }
  • 写回答

2条回答 默认 最新

  • va_jav 2019-10-21 17:36
    关注

    老铁 十几几十G说的是机器内存 一般程序内存都只是1G、2G
    你这代码太烂了 我给你改了下 还有你并没有把你查询的结果传给ws啊

    最后分批上传的话 每一批用完马上把组装参数的list置成null,不要用这个list一直装参数,那样内存会爆,这样的话只要每批不超过内存,总数多大全ok

    package util;
    
    import java.sql.ResultSetMetaData;
    import java.sql.SQLException;
    
    public class TestCSDN {
    
        private int uploadDataType_1() throws SQLException {
            //获取数据
            ResultSet resultSet = DBConnection.executeQuery(
                    "SELECT * from TB_StoreHouse WHERE provice_id ='430000'"
            );
            //数据集结构
            ResultSetMetaData reMetaData = resultSet.getMetaData();
            System.out.println("数据集的结构---"+reMetaData);
            //数据集的列数
            int columnCount = reMetaData.getColumnCount();
            System.out.println("数据集的列数---"+columnCount);
            JSONArray array = new JSONArray();
            /*
             * int a 分批上传  本批次数据的位数
             *
             * int b 为总共上传几批次
             */
            int a = 0;
            int b =0;
            boolean  result=true;
            while ( result= resultSet.next()) {
                //转json
                JSONObject jsonObject = new JSONObject();
                if (++a<200) {
                    for (int i = 1; i <= columnCount; i++) {
                        String columnLabel = reMetaData.getColumnLabel(i);
                        String value = resultSet.getString(columnLabel);
                        jsonObject.put(columnLabel, value);
                    }
                    array.add(jsonObject);
                }else {
                    for (int i = 1; i <= columnCount; i++) {
                        String columnLabel = reMetaData.getColumnLabel(i);
                        String value = resultSet.getString(columnLabel);
                        jsonObject.put(columnLabel, value);
                    }
                    array.add(jsonObject);
                    //调用ws
                    System.out.println("上传第"+ ++b +"批数据,上传累计条数:"+b*200+"条");
                    toWS();
                    a=0;
                    array= new JSONArray();
                }
            }
            if (result) {
            }else {
                //调用ws
                System.out.println("本批次数据不满20条,共"+ a+(b*200) +"条");
                toWS();
                System.out.println("整表数据传输完成");
    
            }
            return responseCode;
        }
    
        private void toWS(){
            GrainServiceImplService rs = new GrainServiceImplService();
            GrainService re = rs.getGrainServiceImplPort();
            System.out.println("执行结果为" + re.test(null, "1", 1, "3", "4"));
        }
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大