eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

1. Overview

MySQL is one of the most common relational database systems. Consequently, integrating it with Java applications via the MySQL Connector/J JDBC driver is a fairly standard task. This enables working with the database directly from Java. Yet, even when handling pure text data, we might encounter errors such as java.sql.SQLException: Incorrect string value.

In this tutorial, we explain how to fix the Incorrect string value error in Java applications using MySQL. First, we explore what the exception means and why it occurs. Then, we examine configuration changes at different levels as a means to work around the problem. Finally, we provide other practical solutions such as MySQL schema updates, JDBC configuration, and server settings.

2. Understanding the Error

Before attempting to fix java.sql.SQLException: Incorrect string value, let’s understand how and when the error appears.

2.1. General Description

Initially, we can explore an example error text. To that end, let’s execute an INSERT or UPDATE operation with problematic characters:

java.sql.SQLException: Incorrect string value: '\x00\x00\x27\x4C' for column 'textcol'

In this case, we see that the textcol column can’t handle the string value \x00\x00\x27\x4C. In short, MySQL rejected the input. One of the main reasons for this is the fact that the column character set can’t represent the provided data.

2.2. Character Encoding Mismatch

So, the way that Java handles Unicode isn’t entirely compatible with the default or current MySQL character set.

Specifically, Java uses full UTF-8 encoding, where more than three bytes can represent a single character. On the other hand, the default utf8 character set in MySQL supports a maximum of 3 bytes per character. In this situation, if we try to store a 4-byte character, such as an emoji, within the database, we might encounter the exception in question.

For instance, let’s consider inserting a string containing an emoji into a table as a common action:

INSERT INTO messages (text) VALUES ('❌');

If the column uses utf8, MySQL rejects the value because of its inability to store 4-byte characters.

3. MySQL Character Set Configuration and Conversion

In short, the most reliable solution is often to configure MySQL, the database, and its components for full Unicode support.

3.1. Change utf8mb4 to utf8

MySQL provides the utf8mb4 character set, which supports all Unicode characters, including emojis and similar:

ALTER DATABASE mydb 
CHARACTER SET = utf8mb4 
COLLATE = utf8mb4_unicode_ci;

This command ensures the database itself can store 4-byte characters correctly.

3.2. Convert Tables

Existing tables may still use incompatible encodings, even after updating the database configuration:

ALTER TABLE xtab 
CONVERT TO CHARACTER SET utf8mb4 
COLLATE utf8mb4_unicode_ci;

Thus, we should update all tables with the new encoding.

3.3. Update Columns

In some cases, we might need to modify individual columns:

ALTER TABLE xtab 
MODIFY text_column TEXT 
CHARACTER SET utf8mb4;

To reiterate, the database, all tables, and each column must all use utf8mb4 exclusively.

4. JDBC Configuration Issues

Even after configuring MySQL correctly, we should set up the JDBC connection, so it also handles Unicode properly.

4.1. Configure Connection URL

The JDBC URL can include parameters to enforce Unicode behavior:

jdbc:mysql://localhost:3306/mydb
  ?useUnicode=true
  &characterEncoding=utf8

These options ensure that Java correctly encodes and transmits string data to MySQL.

4.2. Connector/J Behavior

Modern versions of Connector/J automatically negotiate character sets with the MySQL server. However, using older drivers or misconfigurations may still lead to encoding mismatches.

Therefore, explicitly setting encoding or upgrading the driver can help avoid inconsistencies.

5. Verifying MySQL Server Settings

In addition to schema and connection settings, server-level configuration plays a role in encoding behavior.

5.1. Check Character Set Variables

Let’s inspect the current MySQL character set configuration:

SHOW VARIABLES LIKE 'character_set%';

Ideally, the server, database, and connection should all use utf8mb4 to ensure compatibility.

5.2. Update MySQL Configuration

If necessary, we might need to update the MySQL configuration file to enforce the needed defaults:

[mysqld]
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci

After making changes, we should restart the MySQL service for them to take effect. At this point, the server should only employ utf8mb4 as its encoding.

6. Common Pitfalls

Even after applying fixes, certain common mistakes can still trigger the java.sql.SQLException: Incorrect string value error.

6.1. Partial Configuration

Updating only one component can result in inconsistent behavior.

For example, fixing the database but not the tables may still cause failures. Even single columns can result in problems. Thus, all components must conform to the same encoding.

This process can be tedious, but it is required, as even a single misaligned component can break the character set negotiation.

6.2. New Database Components

Any new database component with the default encoding utf8 can break character support.

Thus, we should only create new tables and columns with the utf8mb4 encoding.

6.3. Outdated JDBC Driver

Older JDBC drivers may not fully support modern MySQL encoding behavior:

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

That’s because the above Maven artifact (with its groupId and artifactId) is outdated and superseded:

<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <version>9.7.0</version>
</dependency>

Keeping dependencies up to date is one of the best ways to improve compatibility and stability. To find the latest artifacts, we can always go to MySQL Connector/J on the Maven Repository.

7. Summary

In this article, we explored how to diagnose and fix the java.sql.SQLException: Incorrect string value error in MySQL.

In summary, the problem is caused by character encoding mismatches, most commonly due to the limited utf8 implementation of MySQL. By switching to utf8mb4, aligning JDBC configuration, verifying server settings, and ensuring consistency across all layers, we can fully support Unicode and often eliminate this error.

Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook Jackson – NPI EA – 3 (cat = Jackson)
guest
0 Comments
Oldest
Newest
Inline Feedbacks
View all comments