How do I read CLOBs data from database?

package org.kodejava.jdbc;

import java.io.File;
import java.io.FileWriter;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.sql.*;

public class ClobReadDemo {
    private static final String URL = "jdbc:mysql://localhost/kodejava";
    private static final String USERNAME = "kodejava";
    private static final String PASSWORD = "s3cr*t";

    public static void main(String[] args) {
        try (Connection conn =
                     DriverManager.getConnection(URL, USERNAME, PASSWORD)) {

            String sql = "SELECT book_id, data FROM book_excerpt";
            PreparedStatement stmt = conn.prepareStatement(sql);

            ResultSet resultSet = stmt.executeQuery();
            while (resultSet.next()) {
                long bookId = resultSet.getLong("book_id");
                // Get the character stream of our CLOB file
                Reader reader = resultSet.getCharacterStream("data");

                File file = new File(bookId + ".txt");
                try (FileWriter writer = new FileWriter(file, StandardCharsets.UTF_8)) {
                    char[] buffer = new char[1];
                    while (reader.read(buffer) > 0) {
                        writer.write(buffer);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

The structure of book_excerpt table.

CREATE TABLE `book_excerpt`
(
    `id`          bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `book_id`     bigint(20) unsigned NOT NULL,
    `description` varchar(255)        NOT NULL,
    `data`        longtext,
    PRIMARY KEY (`id`),
    CONSTRAINT `book_excerpt_ibfk_1` FOREIGN KEY (`book_id`) REFERENCES `book` (`id`)
) ENGINE = InnoDB;

Maven Dependencies

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

Maven Central

How do I store CLOBs data into database?

package org.kodejava.jdbc;

import java.io.File;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class ClobInsertDemo {
    private static final String URL = "jdbc:mysql://localhost/kodejava";
    private static final String USERNAME = "kodejava";
    private static final String PASSWORD = "s3cr*t";

    public static void main(String[] args) {
        try (Connection conn =
                     DriverManager.getConnection(URL, USERNAME, PASSWORD)) {
            conn.setAutoCommit(false);

            String sql = "INSERT INTO book_excerpt " +
                         "(book_id, description, data) VALUES (?, ?, ?)";

            File data = new File("java-8-in-action.txt");
            try (PreparedStatement stmt = conn.prepareStatement(sql);
                 FileReader reader = new FileReader(data)) {

                stmt.setLong(1, 1L);
                stmt.setString(2, "Java 8 in Action");
                stmt.setCharacterStream(3, reader, (int) data.length());
                stmt.execute();

                conn.commit();
            } catch (Exception e) {
                conn.rollback();
                e.printStackTrace();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

The structure of book_excerpt table.

CREATE TABLE `book_excerpt`
(
    `id`          bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `book_id`     bigint(20) unsigned NOT NULL,
    `description` varchar(255)        NOT NULL,
    `data`        longtext,
    PRIMARY KEY (`id`),
    CONSTRAINT `book_excerpt_ibfk_1` FOREIGN KEY (`book_id`) REFERENCES `book` (`id`)
) ENGINE = InnoDB;

Maven Dependencies

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

Maven Central