String sql2 = “SELECT as, bs, cs, ds, es, qs FROM b_table”; PreparedStatement stmt2 = conn.prepareStatement(sql2); ResultSet rs2 = stmt2.executeQuery(); BigDecimal as_current = BigDecimal.ZERO; BigDecimal bs_current = BigDecimal.ZERO; BigDecimal cs_current = BigDecimal.ZERO; BigDecimal ds_current = BigDecimal.ZERO; BigDecimal es_current = BigDecimal.ZERO; BigDecimal qs_current = BigDecimal.ZERO; if (rs2.next()) { as_current = rs2.getBigDecimal(“as”); bs_current = rs2.getBigDecimal(“bs”); cs_current = rs2.getBigDecimal(“cs”); ds_current = rs2.getBigDecimal(“ds”); es_current = rs2.getBigDecimal(“es”); qs_current = rs2.getBigDecimal(“qs”); } rs2.close(); stmt2.close();b表里的数据是小数,请问这段代码查出来为什么是null,已知字段类型是varchar,指出原因并改正
时间: 2024-02-27 14:51:19 浏览: 92
这段代码查询出来的结果为null是因为查询出来的数据类型是小数类型,而变量的初始值是字符串类型的,无法将小数类型赋值给字符串类型的变量。要改正这个问题,需要将变量的类型从字符串类型改为BigDecimal类型,代码如下所示:
```
String sql2 = “SELECT as, bs, cs, ds, es, qs FROM b_table”;
PreparedStatement stmt2 = conn.prepareStatement(sql2);
ResultSet rs2 = stmt2.executeQuery();
BigDecimal as_current = BigDecimal.ZERO;
BigDecimal bs_current = BigDecimal.ZERO;
BigDecimal cs_current = BigDecimal.ZERO;
BigDecimal ds_current = BigDecimal.ZERO;
BigDecimal es_current = BigDecimal.ZERO;
BigDecimal qs_current = BigDecimal.ZERO;
if (rs2.next()) {
as_current = rs2.getBigDecimal(“as”);
bs_current = rs2.getBigDecimal(“bs”);
cs_current = rs2.getBigDecimal(“cs”);
ds_current = rs2.getBigDecimal(“ds”);
es_current = rs2.getBigDecimal(“es”);
qs_current = rs2.getBigDecimal(“qs”);
}
rs2.close();
stmt2.close();
```
注意,需要将变量的初始值设置为BigDecimal类型的0,而不是字符串类型的0。
阅读全文