gpt4 book ai didi

java - 如何将sql查询的派生变量添加到java中的列表中

转载 作者:行者123 更新时间:2023-12-01 13:18:59 25 4
gpt4 key购买 nike

我有以下查询:

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;

以及以下类为查询的每个属性设置值:

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

我的问题是,在查询中“count”是一个派生变量。如何将计数结果添加到 systemHealthAlertList 中。因此 systemHealthAlertList 包含所有属性,包括计数。

谢谢

最佳答案

您必须修改MonitorAlertInstance。添加另一个字段:

class MonitorAlertInstance {
private int count;

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

public int getCount() {
return count;
}
}

然后像设置其他字段一样设置它:

monitor.setCount(rs.getInt(4));

关于java - 如何将sql查询的派生变量添加到java中的列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22203958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com