Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java

จะแปลงประเภท CLOB เป็น String ใน Java ได้อย่างไร?


CLOB ย่อมาจาก Character Large Object โดยทั่วไป SQL Clob เป็นประเภทข้อมูลในตัวและใช้เพื่อเก็บข้อมูลข้อความจำนวนมาก เมื่อใช้ประเภทข้อมูลนี้ คุณจะจัดเก็บข้อมูลได้สูงสุด 2,147,483,647 อักขระ

อินเตอร์เฟส java.sql.Clob ของ JDBC API แสดงถึงประเภทข้อมูล CLOB เนื่องจากอ็อบเจ็กต์ Clob ใน JDBC ถูกใช้งานโดยใช้ตัวระบุตำแหน่ง SQL จึงถือตัวชี้แบบลอจิคัลไปยัง SQL CLOB (ไม่ใช่ข้อมูล)

MySQL ฐานข้อมูลรองรับข้อมูลประเภทนี้โดยใช้ตัวแปร 4 ตัว ได้แก่ TINYTEXT, TEXT, MEDIUMTEXT และ LONGTEXT

การแปลงประเภทข้อมูล CLOB เป็นสตริง

  • ดึงค่า Clob จากตารางโดยใช้ getClob() หรือ getCharacterStream() วิธีการของ PresparedStatement อินเทอร์เฟซ
Reader r = clob.getCharacterStream();
  • อ่านอักขระทีละตัวจากสตรีมอักขระที่ดึงมาและผนวกเข้ากับ StringBuilder หรือ 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++;
  • สุดท้าย แสดงหรือจัดเก็บสตริงที่ได้รับ
System.out.println(buffer.toString());

ตัวอย่าง

ให้เราสร้างตารางที่มีชื่อ technologies_data ในฐานข้อมูล MySQL โดยใช้แบบสอบถามต่อไปนี้ -

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

คอลัมน์ที่สามของบทความในตารางจะเก็บข้อมูลประเภท CLOB

การติดตามโปรแกรม JDBC เริ่มแรกแทรก 5 ระเบียนในตาราง technology_data ที่เก็บไฟล์ข้อความ (เนื้อหา) ลงในคอลัมน์บทความ (ประเภท CLOB)

จากนั้นจะดึงบันทึกของตารางและแสดงชื่อและเนื้อหาของบทความ ที่นี่ เรากำลังพยายามแปลงข้อมูลของ CLOB ที่ดึงออกมาเป็นสตริงและแสดงข้อมูลนั้น

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(" ");
      }
   }
}

ผลลัพธ์

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.