在尝试创建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 )
我在这里找到了一个可能的解决方案,但不幸的是我没有访问DB服务器,因此我无法更改其配置. 所以请注意这不是重复,因为为DB服务器更改提供了建议的解决方案,在我的情况下我没有这样的访问权限.
是否有机会在客户端修复此问题?您可以在下面找到我创建会话的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();
}
}
Run Code Online (Sandbox Code Playgroud)
还有我的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>
Run Code Online (Sandbox Code Playgroud)
小智 35
一些进一步的调查表明,问题正是在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>
Run Code Online (Sandbox Code Playgroud)
小智 16
User below URL, It works for me.
url=jdbc:mysql://localhost:3306/hybrisdb?characterEncoding=latin1&useConfigs=maxPerformance
Run Code Online (Sandbox Code Playgroud)
小智 12
这对我有用,你也可以尝试一下:)
url="jdbc:mysql://localhost:3306/dbname?characterEncoding=utf8"
Run Code Online (Sandbox Code Playgroud)
这对我有用!
<property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/empdemo?characterEncoding=utf8"></property>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
31529 次 |
最近记录: |