From c3e6bf5bde573fe733f5b5860610d3df73630014 Mon Sep 17 00:00:00 2001 From: Amaresh Kumar Date: Wed, 29 Aug 2018 20:41:19 +0530 Subject: Fixed Sonar Blocker in StatisticsServlet Fixed 4 Blocker issues in StatisticsServlet.java https://sonar.onap.org/project/issues?id=org.onap.dmaap.datarouter%3Aparent&open= AWHFaQmXEU5FCDfKtnMB&resolved=false&severities=BLOCKER&types=BUG https://sonar.onap.org/project/issues?id=org.onap.dmaap.datarouter%3Aparent&open= AWHFaQmXEU5FCDfKtnL-&resolved=false&severities=BLOCKER&types=BUG https://sonar.onap.org/project/issues?id=org.onap.dmaap.datarouter%3Aparent&open= AWHFaQmXEU5FCDfKtnL_&resolved=false&severities=BLOCKER&types=BUG https://sonar.onap.org/project/issues?id=org.onap.dmaap.datarouter%3Aparent&open =AWHFaQmXEU5FCDfKtnMA&resolved=false&severities=BLOCKER&types=BUG Issue-ID: DMAAP-674 Change-Id: I3dbc50523e0fda002c0a26fa05b23814bf8563bc Signed-off-by: Amaresh Kumar --- .../datarouter/provisioning/StatisticsServlet.java | 91 ++++++++++++---------- 1 file changed, 48 insertions(+), 43 deletions(-) (limited to 'datarouter-prov/src/main/java') diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/StatisticsServlet.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/StatisticsServlet.java index 134a595b..e79d4705 100755 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/StatisticsServlet.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/StatisticsServlet.java @@ -293,13 +293,13 @@ public class StatisticsServlet extends BaseServlet { /** * getFeedIdsByGroupId - Getting FEEDID's by GROUP ID. * - * @throws SQL Query SQLException. + * @throws SQLException Query SQLException. */ public StringBuffer getFeedIdsByGroupId(int groupIds) throws SQLException { DB db = null; Connection conn = null; - PreparedStatement prepareStatement = null; + //PreparedStatement prepareStatement = null; ResultSet resultSet = null; String sqlGoupid = null; StringBuffer feedIds = new StringBuffer(); @@ -308,16 +308,16 @@ public class StatisticsServlet extends BaseServlet { db = new DB(); conn = db.getConnection(); sqlGoupid = " SELECT FEEDID from FEEDS WHERE GROUPID = ?"; - prepareStatement = conn.prepareStatement(sqlGoupid); - prepareStatement.setInt(1, groupIds); - resultSet = prepareStatement.executeQuery(); - while (resultSet.next()) { - feedIds.append(resultSet.getInt("FEEDID")); - feedIds.append(","); + try(PreparedStatement prepareStatement = conn.prepareStatement(sqlGoupid)) { + prepareStatement.setInt(1, groupIds); + resultSet = prepareStatement.executeQuery(); + while (resultSet.next()) { + feedIds.append(resultSet.getInt("FEEDID")); + feedIds.append(","); + } + feedIds.deleteCharAt(feedIds.length() - 1); + System.out.println("feedIds" + feedIds.toString()); } - feedIds.deleteCharAt(feedIds.length() - 1); - System.out.println("feedIds" + feedIds.toString()); - } catch (SQLException e) { e.printStackTrace(); } finally { @@ -326,13 +326,7 @@ public class StatisticsServlet extends BaseServlet { resultSet.close(); resultSet = null; } - - if (prepareStatement != null) { - prepareStatement.close(); - prepareStatement = null; - } - - if (conn != null) { + if (conn != null) { db.release(conn); } } catch (Exception e) { @@ -571,33 +565,44 @@ public class StatisticsServlet extends BaseServlet { intlogger.info("Error parsing time=" + s); return -1; } - - private ResultSet getRecordsForSQL(String sql) { - intlogger.debug(sql); - long start = System.currentTimeMillis(); - DB db = new DB(); - Connection conn = null; - ResultSet rs = null; - - try { - conn = db.getConnection(); - Statement stmt = conn.createStatement(); - PreparedStatement pst = conn.prepareStatement(sql); - rs = pst.executeQuery(); - //this.rsToJson(rs) - //rs.close(); - stmt.close(); - } catch (SQLException e) { - e.printStackTrace(); - } finally { - if (conn != null) { - db.release(conn); - } - } + intlogger.debug(sql); + long start = System.currentTimeMillis(); + DB db = new DB(); + Connection conn = null; + ResultSet rs = null; + Statement stmt = null; + PreparedStatement pst = null; + try { + conn = db.getConnection(); + stmt = conn.createStatement(); + pst = conn.prepareStatement(sql); + rs = pst.executeQuery(); + } catch (SQLException e) { + e.printStackTrace(); + } finally { + + try { + if (conn != null) { + db.release(conn); + } + if (stmt != null) { + stmt.close(); + } + if (pst != null) { + pst.close(); + } + if (rs != null) { + rs.close(); + } + + } catch (SQLException sqlException) { + intlogger.error("Exception in getting SQl Records",sqlException); + } - intlogger.debug("Time: " + (System.currentTimeMillis() - start) + " ms"); + intlogger.debug("Time: " + (System.currentTimeMillis() - start) + " ms"); - return rs; + return rs; + } } } -- cgit 1.2.3-korg