Java 从服务器收到未知的初始字符集索引“255”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50855622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unknown initial character set index '255' received from server
提问by
During attempt to create HibernateSession application fails with exception:
在尝试创建 HibernateSession 应用程序期间失败并出现异常:
Caused by: java.sql.SQLException: Unknown initial character set index '255' received from server. Initial client character set can be forced via the 'characterEncoding' property. 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)
引起: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) )
I found one possible solution herebut 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.
我在这里找到了一种可能的解决方案,但不幸的是我无法访问数据库服务器,因此我无法更改其配置。 所以请注意这不是重复的,因为为数据库服务器更改提供了建议的解决方案,在我的情况下我没有这样的访问权限。
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.
有没有机会在客户端解决这个问题?您可以在下面找到我创建会话的 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();
}
}
And my pom.xml:
还有我的 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>
采纳答案by
Some further investigation showed that the issue was exactly in changes which were done in MySQL v.8.0:
一些进一步的调查表明,问题正是在 MySQL v.8.0 中所做的更改:
Character Set Support
Important Change: The default character set has changed from latin1 to utf8mb4. These system variables are affected:
The default value of the character_set_server and character_set_database system variables has changed from latin1to utf8mb4.
The default value of the collation_server and collation_database system variables has changed from latin1_swedish_cito utf8mb4_0900_ai_ci.
字符集支持
重要更改:默认字符集已从 latin1 更改为 utf8mb4。这些系统变量受到影响:
character_set_server 和 character_set_database 系统变量的默认值已从latin1更改为 utf8mb4。
在collation_server的和collation_database查看系统变量的缺省值已经从latin1_swedish_ci到utf8mb4_0900_ai_ci。
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:
所有这些变化都已经在新版本的 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>
回答by Vivek Seth
I faced the same problem for mysql version 8.0.11 while connection with Java 8 client program. As a solution, I downloaded the latest connector from mysql official site.Below I have mentioned the link: https://dev.mysql.com/downloads/connector/j/
在与 Java 8 客户端程序连接时,我在 mysql 版本 8.0.11 中遇到了同样的问题。作为解决方案,我从mysql官方网站下载了最新的connector。下面我提到了链接:https://dev.mysql.com/downloads/connector/j/
Choose Platform independent as operating system and download the zip archive.Add the available jar in your build path(add as an external jar).
选择 Platform Independent 作为操作系统并下载 zip 存档。在您的构建路径中添加可用的 jar(添加为外部 jar)。
Hope this would solve your problem :)
希望这能解决你的问题:)
回答by Ankur
You need this connector : Connector\J 5.1 driver.
您需要此连接器:Connector\J 5.1 驱动程序。
This is the compatible connector
这是兼容的连接器
回答by Ashish Vyas
User below URL, It works for me.
URL下面的用户,它对我有用。
url=jdbc:mysql://localhost:3306/hybrisdb?characterEncoding=latin1&useConfigs=maxPerformance
回答by aly
type this property in your hibernate.xml
在您的 hibernate.xml 中键入此属性
<property name="hibernate.connection.characterEncoding">utf8</property>
and update your mysql connector to
并将您的 mysql 连接器更新为
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
回答by José Junior
This below change worked to me
以下更改对我有用
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
for this connection
对于这个连接
@Bean
@Profile("dev")
public DataSource dataSourceDev() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setUsername("root");
dataSource.setPassword("password");
dataSource.setUrl("jdbc:mysql://localhost:3306/<yourdatabasehere>");
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
return dataSource;
}
回答by Asif Raza
to solve this problem change driver class name as given below spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver this is working fine for me in mysql server 8.0.16 and mysql connector 8.0.16
要解决此问题,请更改驱动程序类名称,如下所示 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver 这对我来说在 mysql server 8.0.16 和 mysql connector 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`
回答by Sneha K.P
Was facing the same issue.. So I updated the connector version(8.0.18) and it was resolved.
面临同样的问题..所以我更新了连接器版本(8.0.18)并解决了。
回答by MangduYogii
This Works for me!
这对我有用!
<property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/empdemo?characterEncoding=utf8"></property>
回答by Gian
What helped for me was appending this to my URL:
对我有帮助的是将其附加到我的 URL:
useUnicode=true&character_set_server=utf8mb4&useLegacyDatetimeCode=false
This completely fixed the issue.
这完全解决了这个问题。