Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MySQL

JDBC Clob data type คืออะไร? วิธีเก็บและอ่านข้อมูล


CLOB (Character Large Object) ชนิดข้อมูล SQL เก็บข้อความขนาดใหญ่ จนถึง 2,147,483,647 ตัวอักษร

java.sql.Clob interface ใน JDBC API แทน CLOB ใช้ SQL locator ชี้ไปที่ CLOB (ไม่เก็บข้อมูลโดยตรง)

MySQL รองรับ 4 ระดับ:

  • TINYTEXT: สูงสุด 255 ตัวอักษร
  • TEXT: สูงสุด 65,535 ตัวอักษร
  • MEDIUMTEXT: สูงสุด 16,777,215 ตัวอักษร
  • LONGTEXT: สูงสุด 4,294,967,295 ตัวอักษร

เก็บ Clob ลงตาราง

ขั้นตอน 1: เชื่อมต่อ

String mysqlUrl = "jdbc:mysql://localhost/sampleDB";
Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");

ขั้นตอน 2: สร้าง PreparedStatement

PreparedStatement pstmt = con.prepareStatement("INSERT INTO Technologies(Name, Type, Article ) VALUES (?, ?, ?)");

ขั้นตอน 3: ตั้งค่า placeholder

pstmt.setString(1, "JavaFX");
pstmt.setString(2, "Java Library");
FileReader reader = new FileReader("E:\\images\\javafx.txt");
pstmt.setClob(3, reader);
pstmt.execute();

ขั้นตอน 4: รันคำสั่ง

pstmt.execute();

ดึง Clob จากฐานข้อมูล

getClob() คืน Clob object แล้วใช้ getCharacterStream() ได้ Reader object:

while(rs.next()) {
    System.out.println(rs.getString("Name"));
    System.out.println(rs.getString("Type"));
    Clob clob = rs.getClob("Article");
    Reader r = clob.getCharacterStream();
    // อ่านและเขียนไฟล์...
}

ตัวอย่างครบ

import java.io.FileReader;
import java.io.FileWriter;
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 ClobExample {
    public static void main(String args[]) throws Exception {
        DriverManager.registerDriver(new com.mysql.jdbc.Driver());
        String mysqlUrl = "jdbc:mysql://localhost/sampleDB";
        Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
        System.out.println("Connection established......");
        Statement stmt = con.createStatement();
        stmt.execute("CREATE TABLE Technologies( Name VARCHAR(255), Type VARCHAR(255), Article LONGTEXT)");
        System.out.println("Table Created......");
        String query = "INSERT INTO Technologies(Name, Type, Article ) VALUES (?, ?, ?)";
        PreparedStatement pstmt = con.prepareStatement(query);
        pstmt.setString(1, "JavaFX");
        pstmt.setString(2, "Java Library");
        FileReader reader = new FileReader("E:\\images\\javafx.txt");
        pstmt.setClob(3, reader);
        pstmt.execute();
        pstmt.setString(1, "CoffeeScript");
        pstmt.setString(2, "Scripting Language");
        reader = new FileReader("E:\\images\\coffeescript.txt");
        pstmt.setClob(3, reader);
        pstmt.execute();
        pstmt.setString(1, "Cassandra");
        pstmt.setString(2, "NoSQL Database");
        reader = new FileReader("E:\\images\\cassandra.txt");
        pstmt.setClob(3, reader);
        pstmt.execute();
        ResultSet rs = stmt.executeQuery("select * from Technologies");
        int j = 0;
        System.out.println("Contents of the table are: ");
        while(rs.next()) {
            System.out.println(rs.getString("Name"));
            Clob clob = rs.getClob("Article");
            Reader r = clob.getCharacterStream();
            String filePath = "E:\\Data\\clob_output" + j + ".txt";
            FileWriter writer = new FileWriter(filePath);
            int i;
            while ((i = r.read()) != -1) {
                writer.write(i);
            }
            writer.close();
            System.out.println(filePath);
            j++;
        }
    }
}

ผลลัพธ์

Connection established......
Table Created......
Contents of the table are:
JavaFX Java Library
E:\images\clob_output1.txt
CoffeeScript Scripting Language
E:\images\clob_output2.txt
Cassandra NoSQL Database
E:\images\clob_output3.txt