本文介绍了从服务器收到未知的初始字符集索引“255"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试创建 HibernateSession 应用程序期间失败并出现异常:

During attempt to create HibernateSession application fails with exception:

Caused by: java.sql.SQLException: Unknown initial character set index从服务器收到255".可以强制初始客户端字符集通过characterEncoding"属性.在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055) 在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956) 在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)

我在这里找到了一种可能的解决方案,但不幸的是我无法访问数据库服务器,因此我无法更改其配置.因此请注意,这不是重复的,因为已为数据库服务器更改提供了建议的解决方案,而在我的情况下,我没有此类访问权限.

I found one possible solution here but unfortunately I do not have access to DB server thus I cannot change its configuration.So please pay attention that this is not a duplicate, because suggested solution was provided for the DB server changes and in my case I don't have such access.

是否有机会在客户端解决此问题?您可以在下面找到我创建会话的 pom.xml 文件和 java 代码.

Is it any chance to fix this issue on client side? Below you can find my pom.xml file and java code where I create session.

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            return new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void shutdown() {
        // Close caches and connection pools
        getSessionFactory().close();
    }
}

还有我的 pom.xml:

And my pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    ...
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.5.Final</version>
        </dependency>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.9.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

推荐答案

一些进一步的调查表明问题正是在 MySQL v.8.0 中所做的更改:

Some further investigation showed that the issue was exactly in changes which were done in MySQL v.8.0:

字符集支持

重要变化:默认字符集有从 latin1 更改为 utf8mb4.这些系统变量受到影响:

Important Change: The default character set has changed from latin1 to utf8mb4. These system variables are affected:

character_set_server 的默认值和character_set_database 系统变量已从 latin1 更改为utf8mb4.

The default value of the character_set_server and character_set_database system variables has changed from latin1 to utf8mb4.

collat​​ion_server 的默认值和collat​​ion_database 系统变量已从 latin1_swedish_ci 更改utf8mb4_0900_ai_ci.

The default value of the collation_server and collation_database system variables has changed from latin1_swedish_ci to utf8mb4_0900_ai_ci.

所有这些更改都已经在新版本的 mysql-connector-java 中进行了处理,无需配置您的 MySQL.因此从 5.1.6 更改为 5.1.44 解决问题:

All these change were already processed in new version of mysql-connector-java and there is no need to configure your MySQL. So change from 5.1.6 to 5.1.44 fix the issue:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.44</version>
</dependency>

这篇关于从服务器收到未知的初始字符集索引“255"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-29 19:16
查看更多