Save BigInteger in Mysql

Due to math limitations, I have to use the BigInteger class to represent values.

After some calculations, I would like to save the result (specified by 2x instances of BigInteger) in Mysql ...

What is the best data type for storing such an object?

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

+4
source share
4 answers

I would recommend using Blob and then the BigInteger(byte[] val) constructor BigInteger(byte[] val) to move from the byte array to BigInteger and to the BigInteger#toByteArray() method for another.

+5
source
 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 

Link

+6
source

To store bigInteger values ​​in MySQL, we can save them as strings:

  • Convert bigInteger to string :

     String s=bigint_value.toString(); 
  • Store s in a table field that is of type text ; for example, if we save s in a table called big_values with the big_value_as_string field:

     CREATE TABLE big_values (big_value_as_string text); 

    Now the value is saved.

To extract, you must:

  • Get string value from table:

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

     BigInteger b = new BigInteger(bg); 
+1
source

MySQL has a BIGINT data type, as shown in the image: http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html

You can try using BIGINT for this, rather than doing the conversion to binary formats, which makes it a lot more complicated in my opinion.

-1
source

All Articles