在JDBC插入中将BigDecimal变量填充为随机数 [英] BigDecimal variable padded with random numbers in JDBC Insert

查看:324
本文介绍了在JDBC插入中将BigDecimal变量填充为随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑具有field Oracle类型NUMBER的SQL语句INSERT INTO table (field) VALUES (-0.11111111).

Consider SQL statement INSERT INTO table (field) VALUES (-0.11111111) with field Oracle type NUMBER.

当要插入的值是float或double类型时,您将在field中获得确切的值,即-0.11111111.

When the value to be inserted is of type float or double, you get the exact value in field, i.e. -0.11111111.

但是当要插入的值的类型为BigDecimal时,您将获得填充有随机数的值,即0.11111110999999999999990428634077943570446223.

But when the value to be inserted is of type BigDecimal, you get the value padded with random numbers, i.e. 0.1111111099999999990428634077943570446223.

为什么?

Java指出"BigDecimal是不可变的,任意精度的带符号十进制数字."

Java states that "BigDecimal is an immutable, arbitrary-precision signed decimal numbers."

代码是:

String sql = "INSERT INTO pat_statistics (file_key,test_number,rqw)"
    + " VALUES (3778, 100, " + new BigDecimal(-0.11111111) + ")";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, pwd);
Statement st = conn.createStatement();
int n = st.executeUpdate(sql);

数据库是Oracle.

The DB is Oracle.

推荐答案

对于文字值,应始终使用带字符串参数的BigDecimal构造函数.请参见 BigDecimal的文档( double)构造函数以获取详细信息.

For a literal value you should always use the BigDecimal constructor taking a string argument. See the documentation of the BigDecimal(double) constructor for details.

这篇关于在JDBC插入中将BigDecimal变量填充为随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆