public static ArrayList<StatisticalReports> retrieveStatistics(String username, String password) { ArrayList<StatisticalReports> allStatistics = new ArrayList<StatisticalReports>(); DBCreation sql = DBCreation.getInstance(); Connection conn; ResultSet res = null; PreparedStatement st; conn = sql.connect(); ArrayList<String> nodes = new ArrayList<String>(); int i; if (!checkMobileExistance(username, password)) { System.out.println("Invalid un or pw of android client"); return null; } try { st = conn.prepareStatement("select clientID from clients"); res = st.executeQuery(); while (res.next()) { nodes.add(res.getString("clientID")); } st.clearParameters(); for (i = 0; i < nodes.size(); i++) { StatisticalReports nodeStatistics = new StatisticalReports(); st = conn.prepareStatement("SELECT * FROM statistics WHERE nodeID = ? "); st.setString(1, nodes.get(i)); res = st.executeQuery(); ArrayList<StatisticsEntry> statistic = new ArrayList<StatisticsEntry>(); while (res.next()) { StatisticsEntry stat = new StatisticsEntry(); stat.setNodeID(res.getString("nodeID")); stat.setInterfaceName(res.getString("interfaceName")); // stat.setMaliciousPatternID(res.getInt("maliciousPatternID")); stat.setMaliciousPattern(getMaliciousByID(res.getInt("maliciousPatternID"))); stat.setInterfaceIP(res.getString("interfaceIP")); stat.setFrequency(res.getInt("frequency")); statistic.add(stat); } nodeStatistics.setStatisticalReportEntries(statistic); allStatistics.add(nodeStatistics); st.clearParameters(); } } catch (SQLException e) { e.printStackTrace(); // System.out.println("Can't get malicious patterns from db"); } finally { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } return allStatistics; }
/// for debugging /// public static void printRetrieveStatistics() { List<StatisticalReports> allReports = retrieveStatistics("admin", "1234"); if (allReports == null) return; for (int i = 0; i < allReports.size(); i++) { System.out.println("+++++++++++ CLIENT ++++++++++++"); StatisticalReports report = allReports.get(i); List<StatisticsEntry> entriesList = report.getStatisticalReportEntries(); for (int j = 0; j < entriesList.size(); j++) { StatisticsEntry entry = entriesList.get(j); System.out.println("\n------------------------------"); System.out.println("nodeID: " + entry.getNodeID()); System.out.println("interfaceName: " + entry.getInterfaceName()); System.out.println("ip: " + entry.getInterfaceIP()); System.out.println("pattern: " + entry.getMaliciousPattern()); System.out.println("freq: " + entry.getFrequency()); } } }