什么是 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.