Question

I have following codes:

-------class------------

private class SystemHealthAlert implements Work {
        List<MonitorAlertInstance>  systemHealthAlertList; 
        private String queryString;
    //  private java.util.Date startDate;
    //  private java.util.Date endDate;

        @Override
        public void execute(Connection connection) throws SQLException {        
            PreparedStatement ps = connection.prepareStatement(queryString);

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


            int columnCount = rs.getMetaData().getColumnCount();
            while(rs.next())
            {   
                //String[] row = new String[columnCount];
                //results.set(index, element);
                //for (int i=0; i <columnCount ; i++)
              //  {
              //     row[i] = rs.getString(i + 1);
             //   }
                systemHealthAlertList.add(row);
            }       


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

---------Method-----------

public List<MonitorAlertInstance> getSystemHealthAlert(Long selectedSensorId) {
        List<MonitorAlertInstance>  systemHealthAlertList; 

        try {
            // Add SELECT with a nested select to get the 1st row
            String queryString = "select min(MONITOR_ALERT_INSTANCE_ID) as MONITOR_ALERT_INSTANCE_ID, description" +
                    "                      from ems.monitor_alert_instance  " +
                    "                          where description in (select description from monitor_alert_instance" +
                    "                            where co_mod_asset_id = " + selectedSensorId +
                    "                                               )" +
                    "                       group by description";

            SystemHealthAlert work = new SystemHealthAlert();
        //  work.coModAssetId = coModAssetId;
            work.queryString = queryString;

            getSession().doWork(work);

            systemHealthAlertList = work.systemHealthAlertList;

        } catch (RuntimeException re) {
        //  log.error("getMostRecentObservationId() failed", re);
            throw re;
        }
        //log.info("End");
        return systemHealthAlertList;
    }

My query returns three rows from DB. How can I return systemHealthAlertList from the class that will have all the three rows of the query.

Was it helpful?

Solution

In method execute, you should fill your List<MonitorAlertInstance> systemHealthAlertList with instances of MonitorAlertInstance. Create a new instance of MonitorAlertInstance inside the while loop where you retrieve the data:

//You don't need this line, remove it
//int columnCount = rs.getMetaData().getColumnCount();
while(rs.next()) {
    //create a new instance of MonitorAlertInstance per ResultSet row
    MonitorAlertInstance monitor = new MonitorAlertInstance();
    //set the fields from the ResultSet in your MonitorAlertInstance fields
    //since I don't know the fields of this class, I would use field1 and field2 as examples
    monitor.setField1(rs.getInt(1));
    monitor.setField2(rs.getString(2));
    //and on...
    systemHealthAlertList.add(monitor);
}

Apart from this problem, you should initialize your List<MonitorAlertInstance> systemHealthAlertList variable before use it:

systemHealthAlertList = new ArrayList<MonitorAlertInstance>();
while(rs.next()) {
    //content from previous code...
}

OTHER TIPS

Define a class/bean to hold the data from one given row. Loop through your rows, and create one instance of that class for each row you have. Add these instances to some List. Return the List of these 3 instances.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top