jdbc.ConnectionImpl.buildCollat​​ionMapping抛出ClassCastException

时间:2017-05-03 02:35:55

标签: java mysql jdbc

  1. 我使用Tomcat v8.0.39,mysql8,jdk1.8,mybatis v3.2.8。

  2. 当我第一次获得与DB的连接时,类com.mysql.jdbc.ConnectionImpl 在mysql-connector-java-5.1.26.jar中抛出一个ClassCastException:java.math.BigInteger无法强制转换为java.lang.Long

  3. 我尝试制作断点来检查代码:

    private void buildCollationMapping() throws SQLException {
    
    HashMap<Integer, String> javaCharset = null;
    
    if (versionMeetsMinimum(4, 1, 0)) {
    
        TreeMap<Long, String> sortedCollationMap = null;
        HashMap<Integer, String> customCharset = null;
        HashMap<String, Integer> customMblen = null;
    
        if (getCacheServerConfiguration()) {
            synchronized (serverCollationByUrl) {
                sortedCollationMap = (TreeMap<Long, String>) serverCollationByUrl.get(getURL());
                javaCharset = (HashMap<Integer, String>) serverJavaCharsetByUrl.get(getURL());
                customCharset = (HashMap<Integer, String>) serverCustomCharsetByUrl.get(getURL());
                customMblen = (HashMap<String, Integer>) serverCustomMblenByUrl.get(getURL());
            }
        }
    
        java.sql.Statement stmt = null;
        java.sql.ResultSet results = null;
    
        try {
            if (sortedCollationMap == null) {
                sortedCollationMap = new TreeMap<Long, String>();
                javaCharset = new HashMap<Integer, String>();
                customCharset = new HashMap<Integer, String>();
                customMblen = new HashMap<String, Integer>();
    
                stmt = getMetadataSafeStatement();
    
                try {
                    results = stmt.executeQuery("SHOW COLLATION");
                    if (versionMeetsMinimum(5, 0, 0)) {
                        Util.resultSetToMap(sortedCollationMap, results, 3, 2);
                    } else {
                        while (results.next()) {
                            sortedCollationMap.put(results.getLong(3), results.getString(2));
                        }
                    }
                } catch (SQLException ex) {
                    if (ex.getErrorCode() != MysqlErrorNumbers.ER_MUST_CHANGE_PASSWORD || getDisconnectOnExpiredPasswords()) {
                        throw ex;
                    }
                }
    
                for (Iterator<Map.Entry<Long, String>> indexIter = sortedCollationMap.entrySet().iterator(); indexIter.hasNext();) {
                    Map.Entry<Long, String> indexEntry = indexIter.next();
    
                    int collationIndex = indexEntry.getKey().intValue();
    
  4. 当步骤到“int collat​​ionIndex = indexEntry.getKey()。intValue();”时,它抛出ClassCastException。

    有没有人见过,如何解决?感谢...

1 个答案:

答案 0 :(得分:0)

一个老问题,但是昨天我们处理了同样的问题。原来是由于我们使用MySQL v8,但我的sql连接器是v5。安装MySQL v5后,问题就消失了。

检查数据库和连接器版本以确保兼容性。