MySQL连接报错:java.math.BigInteger无法转换为java.lang.Long
java.math.BigInteger cannot be cast to java.lang.Long During MySQL Connection Hey there, let's dig into this connection error you're hitting—this isn't about your SQL queries, it's definitely a compatibility or configuration issue between your MySQL driver, server, and environment.
First, let's highlight the core context from your error stack trace (the key line tells us exactly where things break):
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.ConnectionImpl.buildCollationMapping(ConnectionImpl.java:1041) ...
The buildCollationMapping method failure means the driver can't parse collation metadata from the MySQL server due to a type mismatch, which almost always stems from version mismatches or misconfiguration.
Here are the most reliable fixes to try:
Align MySQL Driver and Server Versions
This is the #1 culprit. If you're using an oldcom.mysql.jdbc.Driver(5.x series) with a MySQL 8.0+ server, the driver can't handle the newer metadata formats the server returns.- Upgrade your driver to the 8.x series (e.g.,
mysql-connector-java:8.0.33), and update your driver class tocom.mysql.cj.jdbc.Driverinstead of the legacycom.mysql.jdbc.Driver. - If you're stuck on an older MySQL server (5.7 or below), ensure your driver version matches the server's major version (e.g., 5.7.x driver for a 5.7 server).
- Upgrade your driver to the 8.x series (e.g.,
Update Your JDBC Connection URL Parameters
Old connection URLs often lack required parameters for newer drivers/servers, causing metadata parsing failures. Add these critical parameters to your URL:jdbc:mysql://your-host:port/db-name?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=trueserverTimezoneis mandatory for 8.x drivers, andallowPublicKeyRetrievalresolves issues with newer MySQL authentication methods.Verify JDK Compatibility
MySQL 8.x drivers require JDK 8 or later. If you're running on JDK 7 or older, you'll hit type conversion issues because the driver uses APIs that returnBigIntegerwhere older JDKs expectLong. Upgrade to JDK 8+ if possible.Clean Up Dependency Conflicts
Ensure your project doesn't have multiple versions of the MySQL driver in the classpath. For Maven projects, runmvn dependency:treeto spot duplicate driver dependencies, then exclude any older versions pulled in transitively.
Start with the driver version alignment—it's the most likely fix for this specific error.
内容的提问来源于stack exchange,提问作者Howard Black

