根据 MySQL docs , Unsinged Bigint 的最大值 = 18446744073709551615
我在未签名的 Bigint 列中插入了一个值 9223372036854776900(远低于最大限制)。
没有显示错误。
当我尝试通过 JDBC 客户端以编程方式访问它时,出现异常:
com.mysql.jdbc.exceptions.jdbc4.MySQLDataException: '9223372036854776900' in column '10' is outside valid range for the datatype BIGINT. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:422) at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) at com.mysql.jdbc.Util.getInstance(Util.java:386) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1026) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927) at com.mysql.jdbc.ResultSetImpl.throwRangeException(ResultSetImpl.java:7964) at com.mysql.jdbc.ResultSetImpl.parseLongAsDouble(ResultSetImpl.java:7248) at com.mysql.jdbc.ResultSetImpl.getLong(ResultSetImpl.java:2946) at com.mysql.jdbc.ResultSetImpl.getLong(ResultSetImpl.java:2911)
MySQL版本:5.5.41-0ubuntu0.14.04.1
最佳答案
根据堆栈跟踪的建议,当我尝试使用 ResultSet#getLong
Long l = rs.getLong(1);
因为存储的值 9223372036854776900 大于 Java 中(带符号的)Long
的最大值:9223372036854775807。
但是,我能够使用 BigDecimal 成功检索该值
java.math.BigDecimal bd = rs.getBigDecimal(1);
或作为 BigInteger 使用
java.math.BigInteger bi = (java.math.BigInteger) rs.getObject(1);
关于java - 使用 JDBC 客户端获取时 MySQL 的 UNSIGNED BIGINT 出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38830257/