Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将JDBC结果集转换为Java集合/ArrayList_Java_Mysql_Arraylist - Fatal编程技术网

将JDBC结果集转换为Java集合/ArrayList

将JDBC结果集转换为Java集合/ArrayList,java,mysql,arraylist,Java,Mysql,Arraylist,我正在尝试将结果集转换为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='

我正在尝试将结果集转换为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;

我正在构建四个ArrayList来捕获TypeName的日期和typeSuccess

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

在其他三个arrayList中,我为相应的typeName添加typeSuccess值ṣ

类型名的三个ArrayList是samrtPhoneArrayList、TableTarArrayList和pcArrayList

对于以下记录,没有智能手机和平板电脑记录。当类型名不存在此类值时,我想在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;
}
public SuccessDeviceType getDeviceType(字符串持续时间、字符串类型、,
字符串结果){
PreparedStatement ps=null;
连接=空;
ResultSet ResultSet=null;
SuccessDeviceType SuccessDeviceType=新建SuccessDeviceType();
Set dateSet=new LinkedHashSet();
试一试{
LOGGER.info(“连接到数据库”);
字符串sql\u query=null;
if(持续时间等于(每天)){
sql\u query=DEV\u TYPE\u such\u DAILY;
}else if(持续时间等于(每周)){
sql\u query=DEV\u TYPE\u such\u WEEKLY;
}else if(持续时间等于(每月)){
sql\u query=DEV\u TYPE\u such\u MONTHLY;
}否则,如果(持续时间等于(每年)){
sql\u query=DEV\u TYPE\u such\u每年一次;
}
LOGGER.info(sql\u查询);
连接=getConnection(连接);
ps=连接准备声明(开发类型TOP3);
ps.setString(1,结果);
resultSet=ps.executeQuery();
info(“获取查询结果集-前3种设备类型”);
整数计数=0;
while(resultSet.next()){
String deviceType=resultSet.getString(“类型名称”);
如果(计数=0)
successDeviceType.getDeviceList1().add(0,deviceType);
否则如果(计数=1)
successDeviceType.getDeviceList2().add(0,deviceType);
否则如果(计数=2)
successDeviceType.getDeviceList3().add(0,deviceType);
计数++;
}
ArrayList deviceList1=successDeviceType.getDeviceList1();
int deviceList1 size=deviceList1.size();
ArrayList deviceList2=successDeviceType.getDeviceList2();
int deviceList2Size=deviceList2.size();
ArrayList deviceList3=successDeviceType.getDeviceList3();
int deviceList3Size=deviceList3.size();
字符串device1=deviceList1.get(0);
字符串device2=deviceList2.get(0);
字符串device3=deviceList3.get(0);
ps=connection.prepareStatement(sql\u查询);
ps.setString(1,结果);
resultSet=ps.executeQuery();
info(“获取查询二的结果集”);
while(resultSet.next()){
字符串日期=resultSet.getString(“日期”);
StringBuilder sb=新的StringBuilder();
if(持续时间等于(每周)){
某人附上(日期);
}否则{
if(持续时间等于(每天)){
日期=转换日期格式(日期);
}
某人附上(日期);
if(持续时间等于(每月)){
某人加上(“—”);
sb.append(resultSet.getString(“年”);
}
}
整数大小1=新整数(deviceList1Size);
整数大小2=新整数(deviceList2Size);
整数大小3=新整数(deviceList3Size);
整数大小=新整数(dateSet.size());
字符串fullDate=sb.toString();
if(dateSet.contains(fullDate)){
}否则{
//如果未找到设备类型的计数,则添加零
/*如果(!size.等于(size1-1)){
设备列表1.添加(“0”);
}如果(!size.equals(size2-1)){
设备列表2.添加(“0”);
}如果(!size.等于(size3-1)){
设备列表3.添加(“0”);
}*/
}
dateSet.add(fullDate);
String deviceName=resultSet.getString(“类型名”);
String deviceCount=resultSet.getString(“typeSuccess”);
if(设备名称等于(设备1)){
DeviceList 1.添加(deviceCount);
}else if(deviceName.equals(device2)){
设备列表2.添加(设备计数);
}else if(设备名称等于(设备3)){
设备列表3.添加(设备计数);
}
}
if(持续时间等于(每周)){
for(字符串修改日期:日期集){
int year=Integer.parseInt(modifiedDate.substring(0,4));
int date=Integer.parseInt(modifiedDate.substring(4));
字符串范围=getDateRange(年份、日期);
successDeviceType.getDateList().add(范围);
}
}else if(持续时间等于(每月)){
for(字符串修改日期:日期集){
字符串[]dateArray=modifiedDate.split(“-”);
字符串month=dateArray[0]。子字符串(0,3);
successDeviceType.getDateList().add(月);
}
}否则{
successDeviceType.getDateList().addAll(日期集);
}
}catch(ClassNotFoundException | ParseException | SQLException e){
LOGGER.log(Level.severy,“DBDaoImpl中发生异常”,e);
}
返回successDeviceType;
}

让我们看看你用Java代码写了什么?Java就是OOP。创建一个包含所有这些字段的类
Device
,然后您可以使用
ArrayList
@Braj-为我所写的内容添加源代码..我建议您使用了错误的d
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;
}