mysql bigint java_使用Java和JDBC将无符号的64位数插入BigInt MySQL列

当使用Java和JDBC尝试将CSV文件中的无符号64位整数插入到MySQL的BigInt(20)列时,会遇到问题,因为Java没有无符号长整型。解决方案是通过将数值转换为字符串,然后使用`setString`方法,让MySQL服务器正确解析。直接使用`setLong`会导致数据截断异常,而`setObject`配合`Types.BIGINT`也会失败。
摘要由CSDN通过智能技术生成

bd96500e110b49cbb3cd949968f18be7.png

I searched for answer to this question and only found vague answers. Here is the problem:

I have a sample table in MySQL:

mytable

time Timestamp

data BigInt(20) unsigned

Data field is chosen so that it can take in 2^64 max value: 18446744073709551616 (20 digits and hence BigInt(20)).

I have a csv file which contains unsigned 64-bit integers.

Now I am using Java and JDBC to insert this data and running into issues because Java does not have unsigned long. So I tried just passing string - but it fails.

st = conn.prepareStatement("INSERT INTO pawan_table (mytime, data) VALUES(?, ?)");

st.setTimestamp(1, new Timestamp(86400000+i*1000));

st.setString(2, "18446744073709551616");

st.execute();

I also tried BigInteger class in Java - but JDBC does not have a method which takes that type. I can only convert BigInteger to "long" which is signed and that is not what I want. For example:

BigInteger bi = new BigInteger(string_from_csv_file);

st.setLong(2, bi.longValue());

Interesting thing is that MySQL Workbench can insert this data - but it is written in c#! and I need to write my code in Java!

How do Java programmers insert unsigned 64-bit number in MySQL BigInt(20) column?

解决方案

So here is the solution:

If you do not use prepare statement and are just inserting using a string SQL statement, you have nothing to worry about because JDBC simply moves your statement to MySQL server and MySQL server can correctly parse the string to unsigned 64 number (BigInt-20).

If you use a prepare statement, then you are in trouble. If you do:

BigInteger bi = new BigInteger("18446744073709551615"); // max unsigned 64-bit number

statement.setObject(2, bi, Types.BIGINT);

statement.execute();

you will get an MySqlDataTruncation exception because JDBC wants to truncate this to signed long.

If you do:

BigInteger bi = new BigInteger(new Long(Long.MAX_VALUE).toString());

statement.setObject(2, bi, Types.BIGINT);

statement.execute();

This will work because the value is within Java's signed long.

So workaround which always works is:

BigInteger bi = new BigInteger("18446744073709551615"); // max unsigned 64-bit number

statement.setString(2, bi.toString());

statement.execute();

You end up passing the data as a string and let MySQL server parse it properly.

Someone should fix the JDBC driver because unsigned 64-bit numbers keep coming up in a lot of statistical applications.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值