开发者

how to store BigInteger values in oracle database

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

I m trying like this:

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

I converted BigInteger to BigDecimal like this:

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.BigD开发者_如何转开发ecimal

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


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.

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

BigDecimal d = new BigDecimal(b);


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)

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


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


you can try this way:

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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜