gpt4 book ai didi

java - 如何从满足某些条件的结果集中获取值并将其显示在表中

转载 作者:行者123 更新时间:2023-11-29 02:58:16 25 4
gpt4 key购买 nike

我有一个方法可以在我有最新时间的表中显示值。此方法的代码是:

public String[][] beamline_Status() {
int i = 0;
try

{
con = getConnection();
stmt = con.createStatement();

String sql = "SELECT TOP 1 c.logtime, a.BL1_data_SS_ST,a.BL2_data_SS_ST,a.BL3_data_SS_ST,a.BL4_data_SS_ST,a.BL5_data_SS_ST,a.BL6_data_SS_ST,a.BL7_data_SS_ST,a.BL8_data_SS_ST,a.BL9_data_SS_ST,a.BL10_data_SS_ST,a.BL11_data_SS_ST, a.BL12_data_SS_ST,a.BL13_data_SS_ST,a.BL14_data_SS_ST,a.BL15_data_SS_ST,a.BL16_data_SS_ST,a.BL17_data_SS_ST,a.BL18_data_SS_ST,a.BL19_data_SS_ST,a.BL20_data_SS_ST,a.BL21_data_SS_ST,a.BL22_data_SS_ST,a.BL23_data_SS_ST,a.BL24_data_SS_ST,a.BL25_data_SS_ST,a.BL26_data_SS_ST,a.BL27_data_SS_ST,b.st1_prmt_status_p45,c.beam_current,c.beam_energy from INDUS2_BLFE.dbo.main_BLFE_status a inner join INDUS2_MSIS.dbo.main_MSIS_status b on a.logtime=b.logtime inner join INDUS2_BDS.dbo.DCCT c on b.logtime=c.logtime ORDER BY c.logtime DESC ";

stmt.executeQuery(sql);
rs = stmt.getResultSet();

while (rs.next()) {

for (int j = 0; j < 31; j++) {
a[i][j] = rs.getString(j + 1);

}


}
} catch (Exception e) {
System.out.println("\nException in Bean " +e.getMessage());
} finally {
closeConnection(stmt, rs, con);
}

return a;



}

此方法返回正确的结果。

现在我有另一种方法,我只想从中检索存在 1 的那些列值和结果集中显示时间、beam_energy 和 beam_current 值的其他三个列,并将这些值显示在具有相应列名的表中.该方法的代码是:

public List < Integer > beam_CurrentStatus() throws SQLException {

try

{
con = getConnection();
stmt = con.createStatement();

String sql = "SELECT TOP 1 c.logtime, a.BL1_data_SS_ST,a.BL2_data_SS_ST,a.BL3_data_SS_ST,a.BL4_data_SS_ST,a.BL5_data_SS_ST,a.BL6_data_SS_ST,a.BL7_data_SS_ST,a.BL8_data_SS_ST,a.BL9_data_SS_ST,a.BL10_data_SS_ST,a.BL11_data_SS_ST, a.BL12_data_SS_ST,a.BL13_data_SS_ST,a.BL14_data_SS_ST,a.BL15_data_SS_ST,a.BL16_data_SS_ST,a.BL17_data_SS_ST,a.BL18_data_SS_ST,a.BL19_data_SS_ST,a.BL20_data_SS_ST,a.BL21_data_SS_ST,a.BL22_data_SS_ST,a.BL23_data_SS_ST,a.BL24_data_SS_ST,a.BL25_data_SS_ST,a.BL26_data_SS_ST,a.BL27_data_SS_ST,b.st1_prmt_status_p45,c.beam_current,c.beam_energy from INDUS2_BLFE.dbo.main_BLFE_status a inner join INDUS2_MSIS.dbo.main_MSIS_status b on a.logtime=b.logtime inner join INDUS2_BDS.dbo.DCCT c on b.logtime=c.logtime ORDER BY c.logtime DESC ";

stmt.executeQuery(sql);
rs = stmt.getResultSet();

while (rs.next()) {


for (int j = 2; j < 30; j++) {
if (rs.getInt(j) == 1)

al.add(rs.getInt(j));
}


}
} catch (Exception e) {
System.out.println("\nException in Bean " + e.getMessage());
} finally {
closeConnection(stmt, rs, con);
}


return al;


}

此方法仅返回 1,因为 1 出现在表中的次数为 ,输出为

[1, 1, 1, 1, 1, 1]

但我希望它以表格格式与列名称一起显示。如何做到这一点?

最佳答案

你可以返回一个 Map<String, Integer>得到StringInteger .而且,您可以使用 ResultSetMetaData 获取列标签。类似的东西,

public Map<String, Integer> beam_CurrentStatus() throws SQLException {
Map<String, Integer> map = new HashMap<>();
try {
con = getConnection();
stmt = con.createStatement();

String sql = "SELECT TOP 1 c.logtime, a.BL1_data_SS_ST,a.BL2_data_SS_ST,a.BL3_data_SS_ST,a.BL4_data_SS_ST,a.BL5_data_SS_ST,a.BL6_data_SS_ST,a.BL7_data_SS_ST,a.BL8_data_SS_ST,a.BL9_data_SS_ST,a.BL10_data_SS_ST,a.BL11_data_SS_ST, a.BL12_data_SS_ST,a.BL13_data_SS_ST,a.BL14_data_SS_ST,a.BL15_data_SS_ST,a.BL16_data_SS_ST,a.BL17_data_SS_ST,a.BL18_data_SS_ST,a.BL19_data_SS_ST,a.BL20_data_SS_ST,a.BL21_data_SS_ST,a.BL22_data_SS_ST,a.BL23_data_SS_ST,a.BL24_data_SS_ST,a.BL25_data_SS_ST,a.BL26_data_SS_ST,a.BL27_data_SS_ST,b.st1_prmt_status_p45,c.beam_current,c.beam_energy from INDUS2_BLFE.dbo.main_BLFE_status a inner join INDUS2_MSIS.dbo.main_MSIS_status b on a.logtime=b.logtime inner join INDUS2_BDS.dbo.DCCT c on b.logtime=c.logtime ORDER BY c.logtime DESC ";

stmt.executeQuery(sql);
rs = stmt.getResultSet();
ResultSetMetaData rsmd = rs.getMetaData();
while (rs.next()) {
for (int j = 2; j < 30; j++) {
if (rs.getInt(j) == 1) {
String name = rsmd.getColumnLabel(j);
map.put(name, rs.getInt(j));
}
}
}
} catch (Exception e) {
System.out.println("\nException in Bean " + e.getMessage());
} finally {
closeConnection(stmt, rs, con);
}
return map;
}

关于java - 如何从满足某些条件的结果集中获取值并将其显示在表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27683466/

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