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 對象的內容并以字節數組形式返回。
使用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 數據類型?如何存儲和讀取其中的數據?的詳細內容,更多請關注www.92cms.cn其它相關文章!






