'Unknown initial character set index '255' received from server

During attempt to create HibernateSession application fails with exception:

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)

I found one possible solution here but 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.

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:

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


Solution 1:[1]

Some further investigation showed that the issue was exactly in changes which were done in 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 latin1 to utf8mb4.

The default value of the collation_server and collation_database system variables has changed from latin1_swedish_ci to 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:

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

Solution 2:[2]

User below URL, It works for me.

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

Solution 3:[3]

This Works for me!

 <property name="JDBC.ConnectionURL"  value="jdbc:mysql://localhost:3306/empdemo?characterEncoding=utf8"></property>

Solution 4:[4]

This works for me and you can try this too :)

url="jdbc:mysql://localhost:3306/dbname?characterEncoding=utf8"

Solution 5:[5]

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;
}

Solution 6:[6]

Was facing the same issue.. So I updated the connector version(8.0.18) and it was resolved.

Solution 7:[7]

What helped for me was appending this to my URL:

useUnicode=true&character_set_server=utf8mb4&useLegacyDatetimeCode=false

This completely fixed the issue.

Solution 8:[8]

use below url:-

url = "jdbc:mysql://localhost:3306/dbName?characterEncoding=latin1&useConfigs=maxPerformance","root","password" ```

Solution 9:[9]

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/

Choose Platform independent as operating system and download the zip archive.Add the available jar in your build path(add as an external jar).

Hope this would solve your problem :)

Solution 10:[10]

You need this connector : Connector\J 5.1 driver.

This is the compatible connector

Solution 11:[11]

type this property in your hibernate.xml

<property name="hibernate.connection.characterEncoding">utf8</property>   

and update your mysql connector to

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

Solution 12:[12]

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.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` 

Solution 13:[13]

I think the Initial client character set should be forced via the 'characterEncoding' property.
so this worked for me:

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