Question

I have following query:

SELECT MAX(MONITOR_ALERT_INSTANCE_ID) AS MONITOR_ALERT_INSTANCE_ID,
  description,
  MAX(CO_MOD_ASSET_ID) as CO_MOD_ASSET_ID,
  COUNT(MONITOR_ALERT_INSTANCE_ID) AS COUNT
FROM monitor_alert_instance
WHERE description IN
  (SELECT description
  FROM monitor_alert_instance
  WHERE co_mod_asset_id = 123
  )
GROUP BY description;

and the following class set values for each properties of the query:

public void execute(Connection connection) throws SQLException {        
                PreparedStatement ps = connection.prepareStatement(queryString);
        //      ps.setLong(1, (long) 4);

                int index = 1;
                ResultSet rs = ps.executeQuery();


                int columnCount = rs.getMetaData().getColumnCount();
                systemHealthAlertList = new ArrayList<MonitorAlertInstance>();

                while(rs.next())
                {   
                     MonitorAlertInstance monitor = new MonitorAlertInstance();
                     MonitorAlert mon = new MonitorAlert();
                     CompanyModuleAsset cma = new CompanyModuleAsset();
                     MonitorAlertDAO maDAO = new MonitorAlertDAO(getSession());
                     CompanyModuleAssetDAO cmaDAO = new CompanyModuleAssetDAO(getSession());


                        monitor.setMonitorAlertInstanceId(rs.getLong(1));
                        monitor.setDescription(rs.getString(2));
                         mon.setMonitorAlertId(rs.getLong(3));

                         Date d = monitor.getCreatedDate();
                         Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                         DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                         String s = formatter.format(rs.getDate(4));
                         Date date = null;
                        try {
                            date = format.parse(s);
                        } catch (ParseException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                         monitor.setCreatedDate(date);
                         System.out.println("Date is : " + date);

                         monitor.setStatus(rs.getString(5));
                         cma.setCoModAssetId(rs.getLong(6));


                        Long monitorAlertId = mon.getMonitorAlertId();
                        Long coModAssetId= cma.getCoModAssetId();

                        MonitorAlert ma =   maDAO.findById(monitorAlertId);
                        monitor.setMonitorAlert(ma);    

                        CompanyModuleAsset cm = cmaDAO.findById(coModAssetId);
                        monitor.setCompanyModuleAsset(cm);

                        systemHealthAlertList.add(monitor);


                     }      


                rs.close();
                ps.close();
            }
        }

My question is that, in the query "count" is a derived variable. How can I add the result of the count in the systemHealthAlertList. SO that systemHealthAlertList contains all the properties including the count.

Thanks

Was it helpful?

Solution

You'll have to modify MonitorAlertInstance. Add another field to it:

class MonitorAlertInstance {
    private int count;

    public void setCount(int count) {
        this.count = count;
    }

    public int getCount() {
        return count;
    }
}

Then just set it like you set the other fields:

monitor.setCount(rs.getInt(4));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top