如何使用PreparedStatement创建列表?

时间:2014-03-04 21:13:54

标签: java hibernate jdbc

我有以下代码:

-------类------------

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();
        }
    }

---------方法-----------

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;
    }

我的查询从DB返回三行。如何从将包含查询的所有三行的类返回systemHealthAlertList。

2 个答案:

答案 0 :(得分:1)

在方法execute中,您应该在List<MonitorAlertInstance> systemHealthAlertList填充MonitorAlertInstance个实例。在MonitorAlertInstance循环中创建while的新实例,在其中检索数据:

//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);
}

除了这个问题,您应该在使用之前初始化List<MonitorAlertInstance> systemHealthAlertList变量:

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

答案 1 :(得分:0)

定义一个类/ bean来保存来自一个给定行的数据。遍历您的行,并为您拥有的每一行创建该类的一个实例。将这些实例添加到某个列表中。返回这3个实例的列表。