You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MySQL连接报错:java.math.BigInteger无法转换为java.lang.Long

Fixing 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 old com.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 to com.mysql.cj.jdbc.Driver instead of the legacy com.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).
  • 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=true
    

    serverTimezone is mandatory for 8.x drivers, and allowPublicKeyRetrieval resolves 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 return BigInteger where older JDKs expect Long. 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, run mvn dependency:tree to 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:54:00