oracle 如何在oracle数据库中存储BigInteger值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3052772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 20:50:00  来源:igfitidea点击:

how to store BigInteger values in oracle database

javaoraclejdbcoracle10g

提问by condinya

I have connected Java program to Oracle database using JDBC. I want to store BigIntegervalues(512 bits) in the database. What should be the type of the column?

我已经使用 JDBC 将 Java 程序连接到 Oracle 数据库。我想BigInteger在数据库中存储值(512 位)。列的类型应该是什么?

I m trying like this:

我正在尝试这样:

I have taken a column of number type in the database.

我在数据库中取了一列数字类型。

I converted BigIntegerto BigDecimallike this:

我转换BigIntegerBigDecimal这样:

BigInteger b=new BigInteger("5779857570957802579079");
Number n =b;
BigDecimal d=(BigDecimal)n;

PreparedStatement pstmt=con.prepareStatemant("insert into database values(?,?)");
pstmt.setString(1,"john");
pstmt.setBigDecimal(2,d);

I am getting the following exception:

我收到以下异常:

javax.servlet.ServletException: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.math.BigDecimal
root cause 

java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.math.BigDecimal

Is there anything wrong in this code snippet? If there is, please suggest other methods.

这个代码片段有什么问题吗?如果有,请提出其他方法。

采纳答案by Thierry

I'm not answering directly your question, but i only see one oracle datatype that can store a 512 bits number : varchar2(156) (156 = abs(log(2^512))+2)

我没有直接回答你的问题,但我只看到一种可以存储 512 位数字的 oracle 数据类型:varchar2(156) (156 = abs(log(2^512))+2)

So i would rather convert the biginteger to a string rather than a bigdecimal.

所以我宁愿将 biginteger 转换为字符串而不是 bigdecimal。

回答by DaveJohnston

Both BigInteger and BigDecimal extend java.lang.Number, however this does not mean that you can cast from BigInteger up to Number then down to BigDecimal.

BigInteger 和 BigDecimal 都扩展了 java.lang.Number,但这并不意味着您可以从 BigInteger 向上转换为 Number,然后再向下转换为 BigDecimal。

There is a constructor in BigDecimal that takes a BigInteger, so try:

BigDecimal 中有一个接受 BigInteger 的构造函数,因此请尝试:

BigDecimal d = new BigDecimal(b);

回答by Martin

You can use a decimal/numeric value depending on your db limits.

您可以根据您的数据库限制使用十进制/数字值。

回答by Marcio Duarte

you can try this way:

你可以试试这样:

 oracle.sql.NUMBER numberValue = new oracle.sql.NUMBER(bigIntegerValue);
 cs.setObject(id, numberValue, OracleTypes.NUMBER);

where bigIntegerValueis an instance of java.math.BigInteger, it works for me

bigIntegerValue的实例在哪里java.math.BigInteger,它对我有用