Java连接MySQL遇BigInteger转Long类型转换异常求助
解决MySQL JDBC连接时的
BigInteger cannot be cast to Long错误 问题重现
我尝试将MySQL数据库连接到Java程序时出现了如下错误:
May 28, 2018 9:26:40 PM com.example.test main SEVERE: null java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919) at com.mysql.jdbc.ConnectionImpl.buildCollationMapping(ConnectionImpl.java:1062) at com.mysql.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:3556) at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2513) at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283) at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:822) at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47) 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:423) at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404) at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317) at java.sql.DriverManager.getConnection(DriverManager.java:664) at java.sql.DriverManager.getConnection(DriverManager.java:247) at com.example.test.main(test.java:13) Caused by: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long at com.mysql.jdbc.ConnectionImpl.buildCollationMapping(ConnectionImpl.java:1007) ... 15 more cast to java.lang.Long
使用的代码如下:
package com.example; import java.sql.*; import java.util.logging.Level; import java.util.logging.Logger; public class test { public static void main(String[] args) { try { Class.forName("com.mysql.jdbc.Driver"); Connection c; c = DriverManager.getConnection("jdbc:mysql://localhost:3306/company", "root", "root"); c.close(); } catch (ClassNotFoundException | SQLException ex) { Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex); } } }
解决方案
我之前也碰到过一模一样的问题,这本质是MySQL JDBC驱动版本和你的Java/MySQL服务器版本不兼容导致的。旧的mysql-connector-java(5.x系列)在搭配Java 8+或者MySQL 8.x使用时,会在构建排序规则映射的步骤里出现这个类型转换错误。下面是具体的解决步骤:
1. 升级JDBC驱动到兼容版本
如果你的MySQL服务器是8.x版本,或者Java版本是8及以上,直接升级到MySQL Connector/J 8.x版本就可以解决这个问题:
- 如果你用Maven管理依赖,修改
pom.xml:
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.33</version> <!-- 可以换成最新的稳定版 --> </dependency>
- 手动导入jar包的话,直接下载对应版本的jar替换掉旧的5.x版本即可。
另外注意,8.x版本的驱动类名已经更新,你代码里的Class.forName("com.mysql.jdbc.Driver")需要改成:
Class.forName("com.mysql.cj.jdbc.Driver");
2. 补充JDBC URL的必要参数
升级驱动后,记得在JDBC URL里添加上时区参数,不然可能会触发新的时区错误:
c = DriverManager.getConnection("jdbc:mysql://localhost:3306/company?useSSL=false&serverTimezone=UTC", "root", "root");
3. 版本匹配检查(可选)
如果非要用旧的5.x驱动,那得确保你的MySQL服务器是5.x版本,并且Java版本不超过Java 7,但显然升级驱动是更省心的方案,能避免后续更多兼容性问题。
你的代码逻辑本身没毛病,就是驱动版本的锅,按上面的步骤改完应该就能正常连接了。
内容的提问来源于stack exchange,提问作者Prathamesh Keni
相关产品推荐
相关产品推荐

