【问题标题】:How to create a list using PreparedStatement?如何使用 PreparedStatement 创建列表?
【发布时间】:2014-03-04 21:13:53
【问题描述】:

我有以下代码:

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

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

我的查询从数据库返回三行。如何从将包含所有三行查询的类中返回 systemHealthAlertList。

【问题讨论】:

    标签: java hibernate jdbc


    【解决方案1】:

    在方法execute 中,您应该用MonitorAlertInstance 的实例填充您的List&lt;MonitorAlertInstance&gt; systemHealthAlertList。在您检索数据的while 循环内创建MonitorAlertInstance 的新实例:

    //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&lt;MonitorAlertInstance&gt; systemHealthAlertList变量:

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

    【讨论】:

    • 是否需要添加 MonitorAlertInstance 类中的所有字段?
    • @Novis 填写课程中的必要字段。如果这意味着填写每个字段,那么是的,您应该添加所有字段。
    • 填写必要的字段是什么意思?我在 MonitorALertInstance 类中有 13 个字段。
    • @Novis 我的意思是,如果您只从数据库中检索两个字段,那么您将只填写类中的 2 个字段。如果您从数据库中检索 13 个字段,那么您将必须填写班级中的 13 个字段。
    • 我从 DB 中得到了两个值,并在 while 循环中设置了这两个字段。但它在以下行中出现空指针异常:systemHealthAlertList.add(monitor);有什么想法吗?
    【解决方案2】:

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

    【讨论】:

    • 好的,那么 OP 只需要填充实例并返回包含它们的列表。
    猜你喜欢
    • 2016-02-10
    • 2016-09-25
    • 1970-01-01
    • 1970-01-01
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多