Store BigInteger into Mysql

后端 未结 4 621
予麋鹿
予麋鹿 2021-01-19 08:21

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 BigIntege

相关标签:
4条回答
  • 2021-01-19 08:29

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

    You can try to use the BIGINT for that rather then doing conversion to binary formats which makes it whole lot more complex to my opinion.

    0 讨论(0)
  • 2021-01-19 08:43

    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.

    0 讨论(0)
  • 2021-01-19 08:45
    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

    0 讨论(0)
  • 2021-01-19 08:45

    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);
      
    0 讨论(0)
提交回复
热议问题