Store BigInteger into Mysql

10,857

Solution 1

I would recommend using a Blob and then the BigInteger(byte[] val) constructor to go from byte array to BigInteger, and the BigInteger#toByteArray() method for the other way.

Solution 2

MySQL Type      Java Type
----------------------------------------
CHAR            String
VARCHAR         String
LONGVARCHAR     String
NUMERIC         java.math.BigDecimal
DECIMAL         java.math.BigDecimal
BIT             boolean
TINYINT         byte
SMALLINT        short
INTEGER         int
BIGINT          long
REAL            float
FLOAT           double
DOUBLE          double
BINARY          byte []
VARBINARY       byte []
LONGVARBINARY   byte []
DATE            java.sql.Date
TIME            java.sql.Time
TIMESTAMP       java.sql.Tiimestamp

Reference

Solution 3

In order to store bigInteger values in MySQL, we can store as strings:

  1. Convert biginteger to string:

    String s=bigint_value.toString();
    
  2. Store s in table field which is of type text; e.g. if we store s in a table named big_values having field big_value_as_string:

    CREATE TABLE big_values (big_value_as_string text);
    

    The value is now stored.

To retrieve we must:

  1. Retrieve string value from table:

    String bg = rs.getString("big_value_as_string");
    
  2. Convert the string to bigInteger type:

    BigInteger b = new BigInteger(bg);
    
Share:
10,857

Related videos on Youtube

Kami
Author by

Kami

Computer Science Student at the Swiss Federal Institute of Technology Love to code in Scala, Ruby, C, C++, Objective-C, PHP, Python Knowns but not appreciated: Java, Shell, Perl Want to learn: Haskell, Erlang

Updated on May 11, 2022

Comments

  • Kami
    Kami almost 2 years

    Due to mathematica constraints I've to use the BigInteger class to represent values.

    After some calculations I would like to store the result (given by 2x BigInteger instances) into Mysql...

    What is the best datatype to store such object ?

    I was thinking about using a Blob to store the binary format of those results (128 bits) ? But I would like to avoid unnecessary type conversions.

  • Kami
    Kami over 13 years
    The BIGINT datatype from MySQL can't hold my value ! 2^128 > 18446744073709551615 (max BIGINT unsigned value) !
  • thotheolh
    thotheolh over 13 years
    This isn't really BigInteger anymore I guess... it's well more than the specific size of BigInteger.
  • b0fh
    b0fh over 13 years
    BigInteger has different meanings in different contexts. The question is tagged "java", where BigInteger means arbitrary precision.
  • j0k
    j0k over 11 years
    Lone link is considered a poor answer since it is meaningless by itself and target resource is not guaranteed to be alive in the future. It would be preferable to include the essential parts of the answer here, and provide the link for reference.
  • Jus12
    Jus12 over 9 years
    what about comparisons. I want to store and compare biginteger.
  • aioobe
    aioobe over 9 years
    As far as I know, mysql does not have support for arbitrarily large numbers.
  • tObi
    tObi over 8 years
    @thotheolh java's primitive long has the same range as mysql's bigint. java's biginteger is designed for values bigger than long and so for values bigger than mysql's bigint