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 – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Introduction

When working with databases in Java, handling large text data is a common task. Besides, the Character Large Object (CLOB) type allows databases to store extensive text data. Moreover, converting between the CLOB and String objects is often necessary when reading from or writing to databases.

In this tutorial, we’ll explore how to perform this conversion efficiently in Java.

2. Converting CLOB to String

When converting a CLOB to a String, the approach depends on the size of the CLOB.

2.1. CLOB Size <= Integer.MAX_VALUE

If the size of the CLOB is less than or equal to Integer.MAX_VALUE, we can directly use the getSubString() method to convert the CLOB to a String. This method efficiently retrieves the entire CLOB content as a String in one go:

@Test
public void givenCLOB_whenSizeLessThanMaxValue_thenConvertToString() throws SQLException {
    Clob clob = new javax.sql.rowset.serial.SerialClob("This is a sample CLOB content.".toCharArray());
    long clobLength = clob.length();

    if (clobLength <= Integer.MAX_VALUE) {
        String clobAsString = clob.getSubString(1, (int) clobLength);
        assertEquals("This is a sample CLOB content.", clobAsString);
    }
}

In this approach, the getSubString() is used to fetch the entire CLOB content directly as a String, provided the CLOB‘s length is manageable within the limits of a String object.

2.2. CLOB Size > Integer.MAX_VALUE

When the size of the CLOB exceeds the Integer.MAX_VALUE, directly converting it into a String isn’t feasible due to the memory limitations of the String class. Instead, we need to process the CLOB data in chunks to avoid memory issues. Here is an example of how we can process the CLOB with chunks:

void convertLargeCLOBtoString() throws SQLException, IOException {
    Clob clob = // Assume we have a large clob size > Integer.MAX_VALUE

    try (Reader reader = clob.getCharacterStream()) {
        char[] buffer = new char[4096];
        int charsRead;

        while ((charsRead = reader.read(buffer)) != -1) {
            processChunk(buffer, charsRead); // Process each chunk as needed
        }
    }
}

We read the CLOB data using a Reader in manageable chunks (e.g., 4096 characters at a time). Instead of trying to store all the data in a single String, we process each chunk as it’s read. This could involve writing to a file, processing the data directly, or any other operation that suits our application’s needs.

This chunk-based approach ensures that even very large CLOBs can be handled efficiently without running into memory issues that would occur if we tried to fit all the data into a single String.

3. Converting String to CLOB

Let’s dive into the implementation of how to convert a String object into a Clob object:

@Test
public void givenString_whenConvertToCLOB_thenCorrect() throws SQLException {
    String sampleText = "This is a sample text to be stored as CLOB.";
    char[] charArray = sampleText.toCharArray();
    Clob clob = new javax.sql.rowset.serial.SerialClob(charArray);

    assertEquals(sampleText, clob.getSubString(1, (int) clob.length()));
}

Here, we define a String object named sampleText containing the text we want to store as a Clob object. Next, we convert the String into a character array (charArray) using the toCharArray() method. This step prepares our text for storage in the Clob object.

Afterward, we create a Clob object using its constructor, SerialClob(charArray), where charArray represents the character data to be stored.

4. Conclusion

In this tutorial, we’ve explored how to convert between Clob objects and String representations in Java, which is essential for managing large text data when interacting with databases.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
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.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

2 Comments
Oldest
Newest