将JDBC resultSet转换为Java Collection / ArrayList

时间:2015-03-16 04:13:02

标签: java mysql arraylist

我正在尝试将resultSet转换为ArrayList。

正在执行以下JDBC查询,结果集如下所示。

SELECT DATE(date_auth) as date, count(stat_id) as typeSuccess, 
       device_type as typeName 
FROM auth_stat 
WHERE date_auth BETWEEN DATE_SUB(CURDATE(), INTERVAL 45 DAY) 
  AND CURDATE() AND auth_result='LOGGEDIN' 
GROUP BY date, typeName;


+------------+-------------+-------------------+
| date       | typeSuccess | typeName          |
+------------+-------------+-------------------+
| 2015-01-30 |         215 | Personal computer |
| 2015-01-30 |          21 | Smartphone        |
| 2015-01-30 |          63 | Tablet            |
| 2015-01-31 |         157 | Personal computer |
| 2015-01-31 |          29 | Tablet            |
| 2015-02-01 |         383 | Personal computer |
| 2015-02-01 |          61 | Smartphone        |
| 2015-02-01 |         150 | Tablet            |
| 2015-02-02 |         184 | Personal computer |
| 2015-02-02 |         125 | Smartphone        |
| 2015-02-02 |          29 | Tablet            |
| 2015-02-03 |          63 | Personal computer |
| 2015-02-03 |          50 | Smartphone        |
| 2015-02-03 |          21 | Tablet            |
| 2015-02-04 |           7 | Personal computer |
| 2015-02-04 |          31 | Smartphone        |
| 2015-02-04 |           1 | Tablet            |
| 2015-02-05 |          42 | Personal computer |
| 2015-02-05 |          37 | Smartphone        |
| 2015-02-05 |           1 | Tablet            |
| 2015-02-06 |         100 | Other             |
| 2015-02-06 |           2 | Personal computer |
| 2015-02-06 |          52 | Smartphone        |
| 2015-02-07 |          72 | Personal computer |
| 2015-02-07 |          36 | Smartphone        |
| 2015-02-07 |          25 | Tablet            |
| 2015-02-08 |         249 | Personal computer |
| 2015-02-08 |         115 | Smartphone        |
| 2015-02-08 |          96 | Tablet            |
| 2015-02-09 |         150 | Personal computer |
| 2015-02-09 |          44 | Smartphone        |
| 2015-02-09 |          61 | Tablet            |
| 2015-02-20 |           1 | Personal computer |
| 2015-02-25 |           4 | Personal computer |
| 2015-02-27 |          14 | Personal computer |
| 2015-02-28 |           6 | Personal computer |
| 2015-03-03 |           2 | Personal computer |
| 2015-03-04 |           9 | Personal computer |
| 2015-03-05 |           1 | Personal computer |
| 2015-03-06 |           1 | Personal computer |
+------------+-------------+-------------------+

我正在构建四个ArrayList来捕获日期,并为typeNames创建typeSuccess。

在日期的ArrayList中,我添加了所有日期ṣ。

在另外三个arrayList中,我为CorrespondingtypeNameṣ

添加了typeSuccess值

typeNames的三个ArrayList是samrtPhoneArrayList,tabletArrayList和pcArrayList。

对于以下记录,没有智能手机和平板电脑记录。当typeNames不存在这样的值时,我想向arrayList添加零。能告诉我如何在Java中实现这一点。 ?

| 2015-02-20 |           1 | Personal computer |
| 2015-02-25 |           4 | Personal computer |
| 2015-02-27 |          14 | Personal computer |
| 2015-02-28 |           6 | Personal computer |
| 2015-03-03 |           2 | Personal computer |
| 2015-03-04 |           9 | Personal computer |
| 2015-03-05 |           1 | Personal computer |
| 2015-03-06 |           1 | Personal computer |

这是我写的代码:

public SuccessDeviceType getDeviceType(String duration, String type,
        String result) {
    PreparedStatement ps = null;
    Connection connection = null;
    ResultSet resultSet = null;
    SuccessDeviceType successDeviceType = new SuccessDeviceType();
    Set<String> dateSet = new LinkedHashSet<>();
    try {
        LOGGER.info("Connecting to Database");
        String sql_query = null;
        if (duration.equals(DAILY)) {
            sql_query = DEV_TYPE_SUCC_DAILY;
        } else if (duration.equals(WEEKLY)) {
            sql_query = DEV_TYPE_SUCC_WEEKLY;
        } else if (duration.equals(MONTHLY)) {
            sql_query = DEV_TYPE_SUCC_MONTHLY;
        } else if (duration.equals(YEARLY)) {
            sql_query = DEV_TYPE_SUCC_YEARLY;
        }
        LOGGER.info(sql_query);
        connection = getConnection(connection);
        ps = connection.prepareStatement(DEV_TYPE_TOP3);
        ps.setString(1, result);
        resultSet = ps.executeQuery();
        LOGGER.info("Fetching the result Set for query one - Top 3  Device Types");
        int count = 0;
        while (resultSet.next()) {
            String deviceType = resultSet.getString("typeName");
            if (count == 0)
                successDeviceType.getDeviceList1().add(0, deviceType);
            else if (count == 1)
                successDeviceType.getDeviceList2().add(0, deviceType);
            else if (count == 2)
                successDeviceType.getDeviceList3().add(0, deviceType);
            count++;
        }
        ArrayList<String> deviceList1 = successDeviceType.getDeviceList1();
        int deviceList1Size = deviceList1.size();
        ArrayList<String> deviceList2 = successDeviceType.getDeviceList2();
        int deviceList2Size = deviceList2.size();
        ArrayList<String> deviceList3 = successDeviceType.getDeviceList3();
        int deviceList3Size = deviceList3.size();

        String device1 = deviceList1.get(0);

        String device2 = deviceList2.get(0);

        String device3 = deviceList3.get(0);
        ps = connection.prepareStatement(sql_query);
        ps.setString(1, result);
        resultSet = ps.executeQuery();
        LOGGER.info("Fetching the result Set for query two");
        while (resultSet.next()) {
            String date = resultSet.getString("date");
            StringBuilder sb = new StringBuilder();
            if (duration.equals(WEEKLY)) {
                sb.append(date);
            } else {
                if (duration.equals(DAILY)) {
                    date = convertDateFormat(date);
                }
                sb.append(date);
                if (duration.equals(MONTHLY)) {
                    sb.append("-");
                    sb.append(resultSet.getString("years"));
                }
            }
            Integer size1 = new Integer(deviceList1Size);
            Integer size2 = new Integer(deviceList2Size);
            Integer size3 = new Integer(deviceList3Size);
            Integer size = new Integer(dateSet.size());
            String fullDate = sb.toString();
            if (dateSet.contains(fullDate)) {

            } else {
                // Add zero if no count found for the device type
                /*if (!size.equals(size1 - 1)) {
                    deviceList1.add("0");
                } else if (!size.equals(size2 - 1)) {
                    deviceList2.add("0");
                } else if (!size.equals(size3 - 1)) {
                    deviceList3.add("0");
                }*/
            }
            dateSet.add(fullDate);
            String deviceName = resultSet.getString("typeName");
            String deviceCount = resultSet.getString("typeSuccess");
            if (deviceName.equals(device1)) {
                deviceList1.add(deviceCount);
            } else if (deviceName.equals(device2)) {
                deviceList2.add(deviceCount);
            } else if (deviceName.equals(device3)) {
                deviceList3.add(deviceCount);
            }
        }
        if (duration.equals(WEEKLY)) {
            for (String modifiedDate : dateSet) {
                int year = Integer.parseInt(modifiedDate.substring(0, 4));
                int date = Integer.parseInt(modifiedDate.substring(4));
                String range = getDateRange(year, date);
                successDeviceType.getDateList().add(range);
            }
        } else if (duration.equals(MONTHLY)){
            for (String modifiedDate : dateSet) {
                String [] dateArray = modifiedDate.split("-");
                String month = dateArray[0].substring(0, 3);
                successDeviceType.getDateList().add(month);
            }
        } else {
            successDeviceType.getDateList().addAll(dateSet);
        }

    } catch (ClassNotFoundException | ParseException | SQLException e) {
        LOGGER.log(Level.SEVERE, "Exception occurred in DBDaoImpl", e);
    }
    return successDeviceType;
}

0 个答案:

没有答案