什麼是 JDBC Blob 資料型別?如何儲存和讀取其中的資料?


BLOB 是二進位制大物件,可以容納可變數量的資料,最大長度為 65535 個字元。

這些用於儲存大量二進位制資料,例如影像或其他型別的檔案。定義為 TEXT 的欄位也儲存大量資料。兩者之間的區別在於,儲存資料的排序和比較在 BLOB 中區分大小寫,而在 TEXT 欄位中不區分大小寫。BLOB 或 TEXT 不指定長度。

將 Blob 儲存到資料庫

要使用 JDBC 程式將 Blob 資料型別儲存到資料庫中,請按照以下步驟操作

步驟 1:連線到資料庫

可以使用 **DriverManager** 類的 **getConnection()** 方法連線到資料庫。

透過將 MySQL URL (即 **jdbc:mysql:///sampleDB** (其中 sampleDB 是資料庫名稱))、使用者名稱和密碼作為引數傳遞給 getConnection() 方法來連線到 MySQL 資料庫。

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

步驟 2:建立一個預處理語句

使用 **Connection** 介面的 **prepareStatement()** 方法建立一個 PreparedStatement 物件。將插入查詢(帶佔位符)作為引數傳遞給此方法。

PreparedStatement pstmt = con.prepareStatement("INSERT INTO MyTableVALUES(?, ?)");

步驟 3:為佔位符設定值

使用 **PreparedStatement** 介面的 setter 方法為佔位符設定值。根據列的資料型別選擇方法。例如,如果列為 VARCHAR 型別,則使用 setString() 方法;如果列為 INT 型別,則可以使用 setInt() 方法。

如果是 Blob 型別,可以使用 setBinaryStream() 或 setBlob() 方法設定其值。將表示引數索引的整數變數和 InputStream 類物件作為引數傳遞給這些方法。

pstmt.setString(1, "sample image");
//Inserting Blob type
InputStream in = new FileInputStream("E:\images\cat.jpg");
pstmt.setBlob(2, in);

步驟 4:執行語句

使用 **PreparedStatement** 介面的 **execute()** 方法執行上面建立的 PreparedStatement 物件。

從資料庫中檢索 Blob

ResultSet 介面的 getBlob() 方法接受一個表示列索引的整數(或表示列名稱的字串值),並檢索指定列的值,並將其以 Blob 物件的形式返回。

while(rs.next()) {
   rs.getString("Name");
   rs.getString("Type");
   Blob blob = rs.getBlob("Logo");
}

**Blob** 介面的 **getBytes()** 方法檢索當前 **Blob** 物件的內容並作為位元組陣列返回。

使用 **getBlob()** 方法,您可以將 blob 的內容獲取到位元組陣列中,並使用 **FileOutputStream** 物件的 **write()** 方法建立影像。

byte byteArray[] = blob.getBytes(1,(int)blob.length());
FileOutputStream outPutStream = new FileOutputStream("path");
outPutStream.write(byteArray);

示例

下面的示例在 MySQL 資料庫中建立一個帶有 blob 資料型別的表,向其中插入影像,將其檢索回並存儲到本地檔案系統中。

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class BlobExample {
   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:///sampleDB";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Creating a table
      Statement stmt = con.createStatement();
      stmt.execute("CREATE TABLE SampleTable( Name VARCHAR(255), Image BLOB)");
      System.out.println("Table Created");
      //Inserting values
      String query = "INSERT INTO SampleTable(Name,image) VALUES (?, ?)";
      PreparedStatement pstmt = con.prepareStatement(query);
      pstmt.setString(1, "sample image");
      FileInputStream fin = new FileInputStream("E:\images\cat.jpg");
      pstmt.setBlob(2, fin);
      pstmt.execute();
      //Retrieving the data
      ResultSet rs = stmt.executeQuery("select * from SampleTable");
      int i = 1;
      System.out.println("Contents of the table are: ");
      while(rs.next()) {
         System.out.println(rs.getString("Name"));
         Blob blob = rs.getBlob("Image");
         byte byteArray[] = blob.getBytes(1,(int)blob.length());
         FileOutputStream outPutStream = new
         FileOutputStream("E:\images\blob_output"+i+".jpg");
         outPutStream.write(byteArray);
         System.out.println("E:\images\blob_output"+i+".jpg");
         System.out.println();
         i++;
      }
   }
}

輸出

Connection established......
Table Created
Contents of the table are:
sample image
E:\images\blob_output1.jpg

更新於:2019年7月30日

11K+ 次瀏覽

啟動您的 職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.