将 BigInteger 存储到 Mysql 中

发布于 2024-10-06 13:13:14 字数 178 浏览 0 评论 0原文

由于数学限制,我必须使用 BigInteger 类来表示值。

经过一些计算后,我想将结果(由 2x BigInteger 实例给出)存储到 Mysql 中...

存储此类对象的最佳数据类型是什么?

我正在考虑使用 Blob 来存储这些结果的二进制格式(128 位)?但我想避免不必要的类型转换。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我只土不豪 2024-10-13 13:13:14

我建议使用 Blob,然后使用 BigInteger(byte[] val) 构造函数从字节数组转换为 BigInteger,然后使用 BigInteger#toByteArray() 方法来转换另一个方式。

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.

药祭#氼 2024-10-13 13:13:14
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

参考

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

情域 2024-10-13 13:13:14

为了在 MySQL 中存储 bigInteger 值,我们可以存储为字符串:

  1. biginteger 转换为 string

    String s=bigint_value.toString();
    
  2. s 存储在 text 类型的表字段中;例如,如果我们将 s 存储在名为 big_values 且具有字段 big_value_as_string 的表中:

    创建表big_values(big_value_as_string 文本);
    

    该值现已存储。

要检索,我们必须:

  1. 从表中检索字符串值:

    String bg = rs.getString("big_value_as_string");
    
  2. 将字符串转换为 bigInteger 类型:

    BigInteger b = new BigInteger(bg);
    

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);
    
请爱~陌生人 2024-10-13 13:13:14

MySQL 有一个 BIGINT 数据类型,如下所示: http://dev .mysql.com/doc/refman/5.0/en/numeric-types.html

您可以尝试使用 BIGINT 来实现这一点,而不是转换为二进制格式,在我看来,这使得它变得更加复杂。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文