jdbcTemplate查询数据库金额字段的精度问题:ResultSet中如何取值的问题
//数据表中Money字段是Number类型 number(8,2)
List query = this.getJdbcTemplate().query(sql,new Object[]{ aac001 } ,
new RowMapper(){
public Object mapRow(final ResultSet rs, final int i) throws SQLException {
//测试金额 System.out.println("rs.getDouble: "+rs.getDouble("money"));
System.out.println("rs.getObject: "+rs.getObject("money"));
System.out.println("rs.getString: "+rs.getString("money"));
System.out.println("rs.getFloat: "+rs.getFloat("money"));
System.out.println("rs.getBigDecimal: "+rs.getBigDecimal("money"));
//以下是数据表中Money的值,用Java查出并打印后的值
情况一:0.36
rs.getDouble: 0.36
rs.getObject: 0.36
rs.getString: .36(缺少了整数位的0)
rs.getFloat: 0.36
rs.getBigDecimal: 0.36
情况二:0.00
rs.getDouble: 0.0
rs.getObject: 0
rs.getString: 0
rs.getFloat: 0.0
rs.getBigDecimal: 0
情况三:20.03
rs.getDouble: 20.03
rs.getObject: 20.03
rs.getString: 20.03
rs.getFloat: 20.03
rs.getBigDecimal: 20.03
情况四:999999.03
rs.getDouble: 999999.03
rs.getObject: 999999.03
rs.getString: 999999.03
rs.getFloat: 999999.0(丢失了第2位小数)
rs.getBigDecimal: 999999.03
//============以下修改为number(16,6)
情况一:999999.030000
rs.getDouble: 999999.03
rs.getObject: 999999.03
rs.getString: 999999.03
rs.getFloat: 999999.0
rs.getBigDecimal: 999999.03
情况二:999999.030050
rs.getDouble: 999999.03005
rs.getObject: 999999.03005
rs.getString: 999999.03005
rs.getFloat: 999999.0
rs.getBigDecimal: 999999.03005
情况三:9999999.000000
rs.getDouble: 9999999.0
rs.getObject: 9999999
rs.getString: 9999999
rs.getFloat: 9999999.0
rs.getBigDecimal: 9999999
情况四:9999999999.030050 整数位10位 (可以看到getDouble显示为科学计数法)
rs.getDouble: 9.99999999903005E9
rs.getObject: 9999999999.03005
rs.getString: 9999999999.03005
rs.getFloat: 1.0E10
rs.getBigDecimal: 9999999999.03005
情况五:999999999.030001 整数位9位
rs.getDouble: 9.99999999030001E8
rs.getObject: 999999999.030001
rs.getString: 999999999.030001
rs.getFloat: 1.0E9
rs.getBigDecimal: 999999999.030001
情况六:99999999.030001 整数位8位
rs.getDouble: 9.9999999030001E7
rs.getObject: 99999999.030001
rs.getString: 99999999.030001
rs.getFloat: 1.0E8
rs.getBigDecimal: 99999999.030001
情况七:9999999.030001 整数位7位
rs.getDouble: 9999999.030001
rs.getObject: 9999999.030001
rs.getString: 9999999.030001
rs.getFloat: 9999999.0
rs.getBigDecimal: 9999999.030001
情况八:9999999.999999 整数位7位
rs.getDouble: 9999999.999999
rs.getObject: 9999999.999999
rs.getString: 9999999.999999
rs.getFloat: 1.0E7
rs.getBigDecimal: 9999999.999999
从上面的测试过程可以看出,
1.当数字的整数位为0时,
rs.geString()方法会丢失整数位的0.
2.当整数位>=7位数字时,
rs.getDouble()方法获得的数字将是科学计数法(与期望的不一样)
3.
rs.getFloat()方法会丢失小数位第2位
综合以上,在涉及到使用JdbcTemplate()方法查询数据表中number类型的代表金额的字段时,推荐使用
getBigDecimal()或getObject()方法。
-----end.