Course – LS – All
announcement - icon

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

>> CHECK OUT THE COURSE

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

In this approach, we’ll leverage standard Java I/O (Reader and Writer) operations to efficiently handle character data from a SQL Clob object. The Reader reads data from the Clob, which is then processed and written to a StringWriter for conversion to a String object.

Here’s how we can achieve this:

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

    String clobAsString;
    try (Reader reader = clob.getCharacterStream();
         StringWriter w = new StringWriter()) {
        char[] buffer = new char[4096];
        int charsRead;
        while ((charsRead = reader.read(buffer)) != -1) {
            w.write(buffer, 0, charsRead);
        }
        clobAsString = w.toString();
    }

    assertEquals("This is a sample CLOB content.", clobAsString);
}

Here, we first create a Clob object with sample content using SerialClob. Next, we obtain a Reader from the Clob using the getCharacterStream() method, which allows us to read the character data from the Clob. We use a StringWriter named w to capture the character data read from the Reader.

Inside the try block with resources, we define a buffer (char[] buffer) to read characters from the Reader. We then read characters from the Reader into the buffer and write them to the StringWriter using the write() method.

After reading all characters from the Clob into the StringWriter, we convert the content of the StringWriter to a String object using the toString() method, which gives us the content of the Clob as a String object. Finally, we use the assertEquals() method to verify that the colbAsString matches the expected content of the original Clob object.

3. Converting CLOB to String

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.

As always, the complete code samples for this article can be found over on GitHub.

Course – LS – All
announcement - icon

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

>> CHECK OUT THE COURSE

res – REST with Spring (eBook) (everywhere)
Subscribe
Notify of
guest
2 Comments
Oldest
Newest
Inline Feedbacks
View all comments