views:

173

answers:

2

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.BigDecimal

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

+3  A: 

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);
DaveJohnston
i tried but i m getting another exception.javax.servlet.ServletException: java.sql.SQLException: [Oracle][ODBC]Invalid precision value.root cause java.sql.SQLException: [Oracle][ODBC]Invalid precision value
Bipul
Possible that the type of the column you are inserting into doesn't accept BigDecimal?
DaveJohnston
A: 

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.

Thierry
i used varchar2(4000) and it is working.
Bipul
a `RAW(64)` would work as well.
Adam Musch