java将数据返回给java客户端

时间:2015-02-18 06:54:24

标签: java jdbc xml-rpc

我有java客户端发送到服务器(jetty-xmlrpc)查询并从服务器内部的hashmap接收数据。有时数据更大(例如3645888行),当这些数据发送到java客户端时我有错误(java堆空间)。我怎样才能将数据发送2次?或者给我解决方法

这是获取数据并将其发送到客户端的服务器功能

public HashMap getFlickValues(String query,String query2){
    System.out.println("Query is : "+query);
    System.out.println("Query2 is: "+query2);
    Connection c = null;
    Connection c2 = null;
    Statement st = null;
    Statement st2 = null;
    HashMap<String, Object[]> result = new HashMap<String, Object[]>();

    ArrayList<Double> vaArrL = new ArrayList<Double>(); 
    ArrayList<Double> vbArrL = new ArrayList<Double>(); 
    ArrayList<Double> vcArrL = new ArrayList<Double>();

    try {
    Class.forName("org.postgresql.Driver");
    String conString  = "jdbc:postgresql://" + host + ":" + port + "/" + DBName + 
            "?user=" + user + "&pass=" + pass;
    String conString1 = "jdbc:postgresql://" + host + ":" + port2 + "/" + DBName2 + 
            "?user=" + user + "&pass=" + pass;    

    //String conString1 = "jdbc:postgresql://127.0.0.1:5431/merkezdbram "  + 
           // "?user=" + user + "&pass=" + pass;
    /*c = DriverManager.getConnection(conString);
      st = c.createStatement();
      ResultSet rs = st.executeQuery(query);
      while (rs.next()){
          vaArrL.add(rs.getDouble("va"));
          vbArrL.add(rs.getDouble("vb"));
          vcArrL.add(rs.getDouble("vc"));
      }*/
      c = DriverManager.getConnection(conString);
      //c.setAutoCommit(false);
      c2 = DriverManager.getConnection(conString1);  
      //c2.setAutoCommit(false); 
      st = c.createStatement();
      //st.setFetchSize(1000);
      st2 = c2.createStatement();
      //st2.setFetchSize(1000);
          List<ResultSet> resultSets = new ArrayList<>();
          resultSets.add(st.executeQuery(query));
          resultSets.add(st2.executeQuery(query2));
          ResultSets rs = new ResultSets(resultSets);
          int count = 0;
          int ResultSetSize = rs.getFetchSize();
          System.out.println("ResultSetSize is "+ResultSetSize);
      while (rs.next()){
        //count++;
        //if ( count == 2200000) { break;}
          vaArrL.add(rs.getDoubleVa("va"));
          vbArrL.add(rs.getDoubleVb("vb"));
          vcArrL.add(rs.getDoubleVc("vc"));
      }

      int sz = vaArrL.size();

      result.put("va", vaArrL.toArray(new Object[sz]));
      result.put("vb", vbArrL.toArray(new Object[sz]));
      result.put("vc", vcArrL.toArray(new Object[sz]));
      //rs.close();
      st.close();
      c.close();
    } catch ( Exception e ) {
        System.out.println(e);
        e.printStackTrace();

    }
    System.out.println("Flicker vaArrL.size = "+vaArrL.size());
    return result;
}

和ResultSets类是:

class ResultSets {

 private java.util.List<java.sql.ResultSet> resultSets;

    private java.sql.ResultSet current;


    public ResultSets(java.util.List<java.sql.ResultSet> resultSets) {
        this.resultSets = new java.util.ArrayList<>(resultSets);
        current = resultSets.remove(0);
    }

    public boolean next() throws SQLException {
        if (current.next()) {
            return true;
        }else if (!resultSets.isEmpty()) {
            current = resultSets.remove(0);
            return next();
        }
        return false;
    }
    public Double getDoubleVa(String va) throws SQLException{
        return current.getDouble("va");
    }

    public Double getDoubleVb(String vb) throws SQLException{
        return current.getDouble("vb");
    }

    public Double getDoubleVc(String vc) throws SQLException{
        return current.getDouble("vc");
    }
}

我想在没有(java堆空间)的情况下将数据返回给客户端? 我为VM参数制作了-Xmx1024m,但是同样的问题 我想在我的代码中找到解决方案

谢谢

0 个答案:

没有答案