Computer >> Máy Tính >  >> Lập trình >> Java

Làm thế nào để chuyển đổi một loại CLOB sang Chuỗi trong Java?

CLOB là viết tắt của Character Large Object nói chung, SQL Clob là một kiểu dữ liệu tích hợp sẵn và được sử dụng để lưu trữ lượng lớn dữ liệu dạng văn bản. Sử dụng kiểu dữ liệu này, bạn có thể lưu trữ dữ liệu lên đến 2.147.483.647 ký tự.

Giao diện java.sql.Clob của API JDBC đại diện cho kiểu dữ liệu CLOB. Vì đối tượng Clob trong JDBC được triển khai bằng cách sử dụng bộ định vị SQL, nó giữ một con trỏ logic tới SQL CLOB (không phải dữ liệu).

MySQL cơ sở dữ liệu cung cấp hỗ trợ cho kiểu dữ liệu này bằng cách sử dụng bốn biến là TINYTEXT, TEXT, MEDIUMTEXT và, LONGTEXT.

Để chuyển đổi kiểu dữ liệu CLOB thành chuỗi

  • Lấy giá trị Clob từ một bảng bằng cách sử dụng getClob () hoặc getCharacterStream () phương pháp của PresparedStatement giao diện.
Reader r = clob.getCharacterStream();
  • Đọc từng ký tự một từ Luồng ký tự đã truy xuất và nối chúng vào StringBuilder hoặc StringBuffer .
int j = 0;
StringBuffer buffer = new StringBuffer();
int ch;
while ((ch = r.read())!=-1) {
   buffer.append(""+(char)ch);
}
System.out.println(buffer.toString());
j++;
  • Cuối cùng, hiển thị hoặc lưu trữ Chuỗi thu được.
System.out.println(buffer.toString());

Ví dụ

Hãy để chúng tôi tạo một bảng với tên tech_data trong cơ sở dữ liệu MySQL bằng cách sử dụng truy vấn sau -

CREATE TABLE Technologies (Name VARCHAR(255), Type VARCHAR(255), Article LONGTEXT);

Cột thứ ba của bảng Article lưu trữ dữ liệu kiểu CLOB.

Sau chương trình JDBC ban đầu chèn 5 bản ghi trong bảng tech_data lưu trữ tệp văn bản (nội dung của nó) vào cột bài viết (kiểu CLOB).

Sau đó, nó truy xuất các bản ghi của bảng và hiển thị tên và nội dung của bài báo. Ở đây, chúng tôi đang cố gắng chuyển đổi dữ liệu của CLOB đã truy xuất thành Chuỗi và hiển thị nó.

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 {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String mysqlUrl = "jdbc:mysql://localhost/sampledatabase";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Creating a Statement object
      Statement stmt = con.createStatement();
      //Inserting values
      String query = "INSERT INTO Technologies_data VALUES (?, ?, ?)";
      PreparedStatement pstmt = con.prepareStatement(query);
      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();
      //Retrieving the data
      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(" ");
      }
   }
}

Đầ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. By using Java technology, these applications have a browser penetration rate of 76%.
Article: CoffeeScript
Contents: CoffeeScript is a lightweight language based on Ruby and Python which transcompiles (compiles from one source language to another) into JavaScript. It provides better syntax avoiding the quirky parts of JavaScript, still retaining the flexibility and beauty of the language.
Article: Cassandra
Contents: Apache Cassandra is a highly scalable, high-performance distributed database designed to handle large amounts of data across many commodity servers,
providing high availability with no single point of failure. It is a type of NoSQL database. Let us first understand what a NoSQL database does.