什麼是 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中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

本文討論了使用MySQL的Alter Table語句修改表,包括添加/刪除列,重命名表/列以及更改列數據類型。

InnoDB的全文搜索功能非常强大,能够显著提高数据库查询效率和处理大量文本数据的能力。1)InnoDB通过倒排索引实现全文搜索,支持基本和高级搜索查询。2)使用MATCH和AGAINST关键字进行搜索,支持布尔模式和短语搜索。3)优化方法包括使用分词技术、定期重建索引和调整缓存大小,以提升性能和准确性。

文章討論了為MySQL配置SSL/TLS加密,包括證書生成和驗證。主要問題是使用自簽名證書的安全含義。[角色計數:159]

文章討論了流行的MySQL GUI工具,例如MySQL Workbench和PhpMyAdmin,比較了它們對初學者和高級用戶的功能和適合性。[159個字符]

本文討論了使用Drop Table語句在MySQL中放下表,並強調了預防措施和風險。它強調,沒有備份,該動作是不可逆轉的,詳細介紹了恢復方法和潛在的生產環境危害。

聚集索引和非聚集索引的區別在於:1.聚集索引將數據行存儲在索引結構中,適合按主鍵查詢和範圍查詢。 2.非聚集索引存儲索引鍵值和數據行的指針,適用於非主鍵列查詢。

本文討論了在PostgreSQL,MySQL和MongoDB等各個數據庫中的JSON列上創建索引,以增強查詢性能。它解釋了索引特定的JSON路徑的語法和好處,並列出了支持的數據庫系統。
