CLOB (Character Large Object) là kiểu dữ liệu SQL tích hợp dùng để lưu trữ lượng lớn dữ liệu văn bản (tối đa 2.147.483.647 ký tự). Interface java.sql.Clob của JDBC API đại diện cho kiểu CLOB. Vì đối tượng Clob trong JDBC được triển khai bằng SQL locator, nó giữ một con trỏ logic đến SQL CLOB (chứ không phải dữ liệu thực tế).
Cơ sở dữ liệu MySQL hỗ trợ kiểu này qua bốn biến: TINYTEXT, TEXT, MEDIUMTEXT, LONGTEXT.
Các bước chuyển đổi CLOB sang String
- Lấy giá trị Clob từ bảng dùng phương thức getClob() hoặc getCharacterStream() của PreparedStatement.
Reader r = clob.getCharacterStream();
- Đọc từng ký tự từ luồng ký tự và nối vào StringBuilder hoặc StringBuffer.
int ch; StringBuffer buffer = new StringBuffer(); while ((ch = r.read()) != -1) { buffer.append((char)ch); } - Hiển thị hoặc lưu chuỗi thu được.
System.out.println(buffer.toString());
Ví dụ hoàn chỉnh (JDBC MySQL)
Tạo bảng Technologies_data:
CREATE TABLE Technologies (Name VARCHAR(255), Type VARCHAR(255), Article LONGTEXT);
Cột thứ ba Article lưu dữ liệu kiểu CLOB.
Chương trình JDBC: Chèn 5 bản ghi (lưu nội dung file text vào cột Article), sau đó đọc và chuyển CLOB sang String hiển thị.
import java.io.FileReader;
import java.io.Reader;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class ClobToString {
public static void main(String args[]) throws Exception {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
String mysqlUrl = "jdbc:mysql://localhost/sampledatabase";
Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
System.out.println("Connection established......");
Statement stmt = con.createStatement();
String query = "INSERT INTO Technologies_data VALUES (?, ?, ?)";
PreparedStatement pstmt = con.prepareStatement(query);
// Insert 3 records
pstmt.setString(1, "JavaFX");
pstmt.setString(2, "Java Library");
FileReader reader = new FileReader("E:\\images\\javafx_contents.txt");
pstmt.setClob(3, reader);
pstmt.execute();
pstmt.setString(1, "CoffeeScript");
pstmt.setString(2, "Scripting Language");
reader = new FileReader("E:\\images\\coffeescript_contents.txt");
pstmt.setClob(3, reader);
pstmt.execute();
pstmt.setString(1, "Cassandra");
pstmt.setString(2, "NoSQL Database");
reader = new FileReader("E:\\images\\cassandra_contents.txt");
pstmt.setClob(3, reader);
pstmt.execute();
// Retrieve and convert CLOB to String
ResultSet rs = stmt.executeQuery("select * from Technologies_data");
System.out.println("Contents of the table are: ");
while(rs.next()) {
System.out.println("Article: " + rs.getString("Name"));
Clob clob = rs.getClob("Article");
Reader r = clob.getCharacterStream();
StringBuffer buffer = new StringBuffer();
int ch;
while ((ch = r.read()) != -1) {
buffer.append((char)ch);
}
System.out.println("Contents: " + buffer.toString());
System.out.println(" ");
}
}
}
Kết quả đầu ra
Connection established...... Contents of the table are: Article: JavaFX Contents: JavaFX is a Java library using which you can develop Rich Internet Applications... Article: CoffeeScript Contents: CoffeeScript is a lightweight language based on Ruby and Python... Article: Cassandra Contents: Apache Cassandra is a highly scalable, high-performance distributed database...