如何使用JDBC向表中BLOB和CLOB型別列寫入資料?


CLOB代表字元大物件 (Character Large Object)。通常,SQL CLOB是一種內建資料型別,用於儲存大量文字資料。使用此資料型別,您可以儲存多達2,147,483,647個字元的資料。MySQL資料庫支援CLOB資料型別**TINYTEXT、TEXT、MEDIUMTEXT、LONGTEXT**。

JDBC API的**java.sql.Clob**介面表示CLOB資料型別。由於JDBC中的Clob物件是使用SQL定位器實現的,因此它儲存指向SQL CLOB的邏輯指標(而不是資料本身)。

將資料插入CLOB型別列

您可以使用**PreparedStatement**介面的**setCharacterStream()**或setClob()方法插入CLOB型別值。這些方法接受兩個引數:

  • 一個整數,表示繫結變數的索引(代表儲存CLOB型別資料的列)。
  • 一個Reader物件,包含要儲存到表中的資料檔案。
FileReader fileReader = new FileReader("javafx_contents.txt");
pstmt.setClob(1, fileReader);
or,
pstmt.setCharacterStream(1, fileReader);

**BLOB**是**b**inary **l**arge **o**bject的縮寫,它可以容納可變數量的資料,最大長度為65535個字元。

這些用於儲存大量二進位制資料,例如影像或其他型別的檔案。MySQL使用TINYBLOB、BLOB、MEDIUMBLOB和LONGBLOB支援BLOB資料型別。

JDBC API的**java.sql.Blob**介面表示BLOB資料型別。由於JDBC中的Blob物件是使用SQL定位器實現的,因此它儲存指向SQL BLOB的邏輯指標(而不是資料本身)。

將資料插入Blob資料型別列

您可以使用**PreparedStatement**介面的**setBinaryStream()**或setBlob()方法將BLOB資料插入表中。這些方法接受兩個引數:

  • 一個整數,表示繫結變數的索引(代表儲存BLOB型別資料的列)。
  • 一個InputStream物件,包含要儲存到表中的二進位制資料(影像)檔案。
InputStream inputStream = new FileInputStream("javafx_logo.jpg");
pstmt.setBlob(1, inputStream);
or,
pstmt.setBinaryStream(1, inputStream);

下面的JDBC程式建立與MySQL的連線,並將BLOB和CLOB值插入到articles_data表中。

示例

import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class InsertingDataToBlobandClob {
   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:///sampledatabase";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Inserting values
      String query = "INSERT INTO articles_data(Name, Article, Logo) VALUES (?, ?, ?)";
      PreparedStatement pstmt = con.prepareStatement(query);
      pstmt.setString(1, "JavaFX");
      FileReader fileReader = new FileReader("E:\images\javafx_contents.txt");
      pstmt.setClob(2, fileReader);
      InputStream inputStream = new FileInputStream("E:\images\javafx_logo.jpg");
      pstmt.setBlob(3, inputStream);
      pstmt.execute();
      pstmt.setString(1, "CoffeeScript");
      fileReader = new FileReader("E:\images\coffeescript_contents.txt");
      pstmt.setClob(2, fileReader);
      inputStream = new FileInputStream("E:\images\coffeescript_logo.jpg");
      pstmt.setBlob(3, inputStream);
      pstmt.execute();
      pstmt.setString(1, "JavaFX");
      fileReader = new FileReader("E:\images\cassandra_contents.txt");
      pstmt.setClob(2, fileReader);
      inputStream = new FileInputStream("E:\images\cassandra_logo.jpg");
      pstmt.setBlob(3, inputStream);
      pstmt.execute();
      System.out.println("Records inserted......");
   }
}

輸出

Records inserted......

驗證

如果您使用MySQL Workbench驗證articles_data表的內容,您可以看到插入的影像和文字值,如下所示:

更新於:2019年7月30日

2K+ 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告