首頁  >  文章  >  資料庫  >  什麼是 JDBC Blob 資料類型?如何儲存和讀取其中的資料?

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

PHPz
PHPz轉載
2023-09-14 18:57:10950瀏覽

什么是 JDBC Blob 数据类型?如何存储和读取其中的数据?

BLOB 是二進位大對象,可容納可變數量的數據,最大長度為 65535 個字元。

它們用於儲存大量二進位數據,例如圖像或其他類型的數據。文件。定義為 TEXT 的欄位也保存大量資料。兩者之間的差異在於,儲存資料的排序和比較在 BLOB 中區分大小寫,而在 TEXT 欄位中不區分大小寫。您沒有使用 BLOB 或 TEXT 指定長度。

將Blob 儲存到資料庫

要將Blob 資料類型儲存到資料庫,請使用JDBC 程式依照下列步驟操作

第1 步:連接到資料庫

您可以使用DriverManagergetConnection() 方法連接到資料庫

透過傳遞MySQL URL(jdbc:mysql://localhost /sampleDB)(其中exampleDB 是資料庫名稱)、使用者名稱和密碼作為getConnection() 方法的參數。

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

第二步驟:建立預編譯語句

使用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);

第四步:執行語句

使用PreparedStatement介面的execute()方法執行上述建立的PreparedStatement對象。

從資料庫中檢索blob

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

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

Blob 介面的 getBytes() 方法會擷取目前 Blob 物件的內容並以位元組陣列形式傳回。 p>

使用getBlob()方法,您可以將blob的內容取得到位元組數組中,並使用write()方法建立圖片FileOutputStream 物件。

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://localhost/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

以上是什麼是 JDBC Blob 資料類型?如何儲存和讀取其中的資料?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除