在尝试创建HibernateSession应用程序失败时出现异常:
引起:java.sql.SQLException:未知的初始字符集索引 ' 255'从服务器收到。可以强制初始客户端字符集 通过' characterEncoding'属性。在 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)at at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)at at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
我找到了一个可能的解决方案here,但遗憾的是我无法访问数据库服务器,因此我无法更改其配置。 所以请注意,这不是重复的,因为为数据库服务器更改提供了建议的解决方案,在我的情况下,我没有这样的访问权限。
是否有机会在客户端解决此问题?您可以在下面找到我创建会话的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>
答案 0 :(得分:28)
进一步的调查显示,问题恰恰是在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.6
更改为5.1.44
会解决问题:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
答案 1 :(得分:4)
URL下的用户,对我有用。
url=jdbc:mysql://localhost:3306/hybrisdb?characterEncoding=latin1&useConfigs=maxPerformance
答案 2 :(得分:3)
这对我有用!
<property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/empdemo?characterEncoding=utf8"></property>
答案 3 :(得分:1)
面临相同的问题。.所以我更新了连接器版本(8.0.18)并已解决。
答案 4 :(得分:1)
对我有帮助的是将其附加到我的URL:
useUnicode=true&character_set_server=utf8mb4&useLegacyDatetimeCode=false
这完全解决了问题。
答案 5 :(得分:0)
在与Java 8客户端程序连接时,我遇到了mysql版本8.0.11的相同问题。作为解决方案,我从mysql官方站点下载了最新的连接器。下面我提到了链接: https://dev.mysql.com/downloads/connector/j/
选择独立于操作系统的平台并下载zip归档文件。在构建路径中添加可用的jar(添加为外部jar)。
希望这可以解决您的问题:)
答案 6 :(得分:0)
您需要此连接器:Connector \ J 5.1驱动程序。
这是兼容的连接器
答案 7 :(得分:0)
在您的hibernate.xml中键入此属性
<property name="hibernate.connection.characterEncoding">utf8</property>
并将您的mysql连接器更新为
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
答案 8 :(得分:0)
以下更改对我有用
a_T \neq 0
为此连接
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
答案 9 :(得分:0)
它也对我有用
jdbc:mysql:// localhost:3306 / hybrisdb?characterEncoding = latin1&useConfigs = maxPerformance
答案 10 :(得分:0)
为解决此问题,请更改驱动程序类名称,如下所示 spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver 在mysql服务器8.0.16和mysql连接器8.0.16中,这对我来说工作正常
`spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.jpa.hibernate.ddl-auto=create
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect`
答案 11 :(得分:0)
使用以下网址:-
url = "jdbc:mysql://localhost:3306/dbName?characterEncoding=latin1&useConfigs=maxPerformance","root","password" ```
答案 12 :(得分:0)
这对我有用,你也可以试试:)
url ="jdbc:mysql://localhost:3306/dbname?characterEncoding=utf8"