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

PHPz
PHPz 转载
2023-09-14 18:57:10 162浏览

什么是 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,如有侵犯,请联系admin@php.cn删除