gpt4 book ai didi

java - Oracle 查询返回为 Hasmap

转载 作者:行者123 更新时间:2023-11-30 05:57:57 26 4
gpt4 key购买 nike

我正在对 Oracle 数据库进行此查询 SELECT T109.DISTRITO FROM T109 T109 ,其中 DISTRITO 列有很多数字。我想获取所有这些数字并将它们放入端点中,因此当他们使用它时以 Hashmap 或 Hasmap 等格式显示此信息......但始终作为 Hashmap。我该怎么做?请忽略西类牙语单词,我知道我们必须始终用英语编码,但这是继承的代码。这是我尝试过的。

代码

private final String CONSULTA_SOLO_DISTRITOS =
" SELECT T109.DISTRITO \n"
+ " FROM T109 T109 \n";


public HashMap<String, String> getOnlyDistritoTarificacion(String numero) {

List<Map<String, Object>> filas_distritos = null;

HashMap<String, String> distrito = new HashMap();

filas_distritos = jdbcTemplate.queryForList(CONSULTA_SOLO_DISTRITOS, new Object[]{numero, numero});

if ((filas_distritos != null) && (!filas_distritos.isEmpty()) || (filas_distritos.size() != 0)) {

Map<String, Object> row = filas_distritos.get(0);

distrito.put("distrito", (String) row.get("DISTRITO"));

distrito.put("mensaje", "El n&uacute;mero consultado (" + numero + ") ya pertenece a un Distrito de Tarificaci&oacute;n.");

} else {

distrito.put("mensaje", "El n&uacute;mero consultado (" + numero + ") no pertenece a un Distrito de Tarificaci&oacute;n.");

}

return distrito;

}

最佳答案

我不确定我是否正确,所以如果这是错误的,请原谅我,我只是尽力提供帮助。

我认为您的代码的问题在于您继续为来自查询结果集的每个数字设置相同的 map 键distrito

相反,您应该在 List 对象中收集您的值,当您拥有所有这些值时,将值设置到您的 Map 中。

public HashMap<String, String> getOnlyDistritoTarificacion(String numero) {

List<String> distritos = new ArrayList();

HashMap<String, String> distrito = new HashMap();

List<Map<String, Object>> filas_distritos = jdbcTemplate.queryForList(CONSULTA_SOLO_DISTRITOS, new Object[]{numero, numero});

if ((filas_distritos != null) && (!filas_distritos.isEmpty()) || (filas_distritos.size() != 0)) {

Map<String, Object> row = filas_distritos.get(0);

distritos.add((String) row.get("DISTRITO"));
distritos.put("distrito", (String) row.get("DISTRITO"));

}

distrito.put("distrito",
distritos.stream()
.collect(Collectors.joining(","));

return distrito;
}

因此,正如您所看到的,您只需在收集所有值后添加一次值即可。我使用 "," 来连接,但您可以使用您需要的其他字符或字符串。

此外,您不需要在每次迭代中添加“mensaje”的值,只需检查需要设置的值即可:

boolean numberConsulted = true;

if ((filas_distritos != null) && (!filas_distritos.isEmpty()) || (filas_distritos.size() != 0)) {
// .... code ...
} else {
numberConsulted = false;
}


if (numberConsulted) {
distrito.put("mensaje", "El n&uacute;mero consultado (" + numero + ") ya pertenece a un Distrito de Tarificaci&oacute;n.");
} else {
distrito.put("mensaje", "El n&uacute;mero consultado (" + numero + ") no pertenece a un Distrito de Tarificaci&oacute;n.");
}

我不懂西类牙语,所以对变量名称感到抱歉,我希望含义是清楚的,无论如何,请询问。

在之前的代码中,您可能会得到错误的结果。

关于java - Oracle 查询返回为 Hasmap<String, String>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52834259/

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