技术文章和资源

技术文章(时间排序)

热门类别

Python PHP MySQL JDBC Linux

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

jdbcjava 8mysqlmysqli database

BLOB 是二进制大对象,可以容纳可变数量的数据,最大长度为 65535 个字符。

它们用于存储大量二进制数据,例如图像或其他类型的文件。定义为 TEXT 的字段也可以容纳大量数据。两者之间的区别在于,对存储数据的排序和比较在 BLOB 上区分大小写,而在 TEXT 字段中不区分大小写。您没有为 BLOB 或 TEXT 指定长度。

将 blob 存储到数据库中

要将 Blob 数据类型存储到数据库中,请使用 JDBC 程序按照以下步骤操作

步骤 1:连接到数据库

您可以使用 DriverManager 类的 getConnection() 方法连接到数据库。

通过将 MySQL URL 即 jdbc:mysql://localhost/sampleDB(其中 sampleDB 是数据库名称)、用户名和密码作为参数传递给 getConnection() 方法,连接到 MySQL 数据库。

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

步骤 2:创建 Prepared 语句

使用 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://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

相关文章