在尝试创建 HibernateSession 应用程序时失败并出现异常:
原因:java.sql.SQLException:从服务器收到未知的初始字符集索引“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) )
我在 这里 找到了一种可能的解决方案,但不幸的是我无权访问数据库服务器,因此我无法更改其配置。 所以请注意这不是重复的,因为为数据库服务器更改提供了建议的解决方案,在我的情况下我没有这样的访问权限。
是否有机会在客户端解决此问题?您可以在下面找到我创建会话的 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>
原文由 user9927978 发布,翻译遵循 CC BY-SA 4.0 许可协议
一些进一步的调查表明问题正是在 MySQL v.8.0 中所做的更改:
所有这些更改都已在新版本的 mysql-connector-java 中处理,无需配置 MySQL。因此,从
5.1.6
更改为5.1.44
修复问题: