如何使用javascript优化数据加载?

时间:2019-09-04 10:28:59

标签: javascript java getjson

我用Java和javascript开发了一个应用程序,该应用程序运行良好,但是问题是数据加载需要很长时间(10分钟),如何优化数据加载时间? 和谢谢。我认为这行中是否有错误?
          d3.json(“ getJson.jsp?dataType = rh”,函数(数据){

代码JavaScript:

function extract_select_data(node,leaves,index){    
        leaves.push({id:++index,text:node.desc});
            if (node.children){
                for(var i = 0;i<node.children.length;i++){                    
                    index = extract_select_data(node.children[i],leaves,index)[0];
                }
            }            
            return [index,leaves];
    }

var allRHJson;
var select_data_all;
var currentTypeSelection='ALL';

d3.json("getJson.jsp?dataType=rh", function(data) {     
     spinner.stop();     
     allRHJson=data;         
     select_data_all = extract_select_data(allRHJson,[],0)[1];
     loadJson(null,allRHJson, select_data_all ) ;
     //d3.json(json, loadJson );
});

代码JAVA:

public static String getJson(String dataType) {
        Connection conn = null;
        String json = "";

        try {
            // Verifier la valeur du paramètre: dataType
            if (dataType == null || dataType.isEmpty())
                return json;
            // Chargement des paramètres de connexion.
            Properties connectionString = new Properties();
            String connectionFileName = dataType + ".properties";
            InputStream input = Hierarchies.class.getClassLoader().getResourceAsStream(connectionFileName);  
            if (input == null) {
                throw new FileNotFoundException("Unable to find " + connectionFileName);
            }

            connectionString = new Properties();
            connectionString.load(input);
            String dbURL = connectionString.getProperty("db.connection.string");
            Class.forName(connectionString.getProperty("db.connection.driver"));
            conn = DriverManager.getConnection(dbURL, connectionString.getProperty("db.connection.user"),
                    connectionString.getProperty("db.connection.password"));

            Statement statement = conn.createStatement();
            String dbView = connectionString.getProperty("db.view");
            String queryString = "SELECT ID, ID_PERE, LIBELLE, DATA FROM " +dbView ;
            ResultSet rs = statement.executeQuery(queryString);
            HashMap<String, Level> h = new HashMap<String, Level>();
            Gson gson = new GsonBuilder().create();

            while (rs.next()) {
                String id = rs.getString(1);
                String idPere = rs.getString(2);
                String libelle = rs.getString(3);
                String data = rs.getString(4);

                if (h.get(id) == null)
                    h.put(id, new Level(id));

                if (h.get(idPere) == null)
                    h.put(idPere, new Level(idPere));

                Level n = h.get(id);
                Level p = h.get(idPere);

                if (p.getChildren() == null)
                    p.setChildren(new ArrayList<Level>());

                n.setDesc(libelle);
                LinkedHashMap<String, String> dataMap = new LinkedHashMap<String, String>();

                if (data != null) {
                    dataMap = (LinkedHashMap<String, String>) gson.fromJson("{" + data + "}", dataMap.getClass());
                }
                n.setData(dataMap);
                p.getChildren().add(n);
                n.setParent(idPere);
            }

              //  String a= "ROOT";
            //  String b = "PF1S0000000000";
            String dc = dataType.equals("rh") ? "PF"  : "ROOT";
                if(dc.equals("PF") || dc.equals("ROOT"))
                {

                    Level root = h.get(dc);
                    root.setParent("null");
                    JsonElement jsonElement = gson.toJsonTree(root);
                    json = jsonElement.toString();              
                    System.out.println(json);
                }           

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {

            try {

                if (conn != null && !conn.isClosed()) {
                    conn.close();
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }

        return json;
    }

0 个答案:

没有答案