从服务器接收到的未知初始字符集索引“255”

2022-09-01 11:07:42

在尝试创建休眠会话应用程序期间失败,并出现异常:

由以下原因引起:java.sql.SQLException:从服务器接收的未知初始字符集索引“255”。初始客户端字符集可以通过“characterEncoding”属性强制进行。at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)

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

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

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:

<?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> 

答案 1

一些进一步的调查表明,问题完全在于MySQL v.8.0中所做的更改:

字符集支持

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

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

collation_server和collation_database系统变量的默认值已从latin1_swedish_ci更改为utf8mb4_0900_ai_ci

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

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

答案 2

URL下面的用户,它适用于我。

url=jdbc:mysql://localhost:3306/hybrisdb?characterEncoding=latin1&useConfigs=maxPerformance

推荐