public void run() {\r
Map<String, Counters> map = new HashMap<String, Counters>();\r
long start = System.currentTimeMillis();\r
+\r
try {\r
DB db = new DB();\r
@SuppressWarnings("resource")\r
Connection conn = db.getConnection();\r
- PreparedStatement ps = conn.prepareStatement(SELECT_SQL);\r
- ps.setLong(1, from);\r
- ps.setLong(2, to);\r
- ResultSet rs = ps.executeQuery();\r
- while (rs.next()) {\r
- String date = rs.getString("DATE");\r
- int sub = rs.getInt("DELIVERY_SUBID");\r
- int res = rs.getInt("RESULT");\r
- int count = rs.getInt("COUNT");\r
- String key = date + "," + sub;\r
- Counters c = map.get(key);\r
- if (c == null) {\r
- c = new Counters(date, sub);\r
- map.put(key, c);\r
+ try(PreparedStatement ps = conn.prepareStatement(SELECT_SQL)) {\r
+ ps.setLong(1, from);\r
+ ps.setLong(2, to);\r
+ try(ResultSet rs = ps.executeQuery()) {\r
+ while (rs.next()) {\r
+ String date = rs.getString("DATE");\r
+ int sub = rs.getInt("DELIVERY_SUBID");\r
+ int res = rs.getInt("RESULT");\r
+ int count = rs.getInt("COUNT");\r
+ String key = date + "," + sub;\r
+ Counters c = map.get(key);\r
+ if (c == null) {\r
+ c = new Counters(date, sub);\r
+ map.put(key, c);\r
+ }\r
+ c.addCounts(res, count);\r
+ }\r
}\r
- c.addCounts(res, count);\r
}\r
- rs.close();\r
- ps.close();\r
\r
- ps = conn.prepareStatement(SELECT_SQL2);\r
- ps.setLong(1, from);\r
- ps.setLong(2, to);\r
- rs = ps.executeQuery();\r
- while (rs.next()) {\r
- String date = rs.getString("DATE");\r
- int sub = rs.getInt("DELIVERY_SUBID");\r
- int count = rs.getInt("COUNT");\r
- String key = date + "," + sub;\r
- Counters c = map.get(key);\r
- if (c == null) {\r
- c = new Counters(date, sub);\r
- map.put(key, c);\r
- }\r
- c.addDlxCount(count);\r
- }\r
- rs.close();\r
- ps.close();\r
+ try( PreparedStatement ps2 = conn.prepareStatement(SELECT_SQL2)) {\r
+ ps2.setLong(1, from);\r
+ ps2.setLong(2, to);\r
+ try(ResultSet rs2 = ps2.executeQuery()) {\r
+ while (rs2.next()) {\r
+ String date = rs2.getString("DATE");\r
+ int sub = rs2.getInt("DELIVERY_SUBID");\r
+ int count = rs2.getInt("COUNT");\r
+ String key = date + "," + sub;\r
+ Counters c = map.get(key);\r
+ if (c == null) {\r
+ c = new Counters(date, sub);\r
+ map.put(key, c);\r
+ }\r
+ c.addDlxCount(count);\r
+ }\r
+ }\r
+ }\r
\r
db.release(conn);\r
} catch (SQLException e) {\r
e.printStackTrace();\r
}\r
logger.debug("Query time: " + (System.currentTimeMillis() - start) + " ms");\r
- try {\r
- PrintWriter os = new PrintWriter(outfile);\r
+ try (PrintWriter os = new PrintWriter(outfile)){\r
os.println("date,subid,count100,count200,count300,count400,count500,countminus1,countdlx");\r
for (String key : new TreeSet<String>(map.keySet())) {\r
Counters c = map.get(key);\r
os.println(c.toString());\r
}\r
- os.close();\r
} catch (FileNotFoundException e) {\r
System.err.println("File cannot be written: " + outfile);\r
}\r