如何使用 JDBC 將影像插入資料庫?


**PreparedStatement** 介面的 **setBinaryStream()** 方法接受一個表示引數索引的整數和一個 InputStream 物件,並將引數設定為給定的 InputStream 物件。當需要傳送非常大的二進位制值時,可以使用此方法。

SQL 資料庫提供了一種名為 Blob(二進位制大型物件)的資料型別,您可以在其中儲存諸如影像之類的較大二進位制資料。

使用 JDBC 儲存影像

如果需要使用 JDBC 程式將影像儲存到資料庫中,請建立一個具有 Blob 資料型別的表,如下所示:

CREATE TABLE Tutorial(Name VARCHAR(255), Type INT NOT NULL, Logo BLOB);

現在,使用 JDBC 連線到資料庫並準備一個 **PreparedStatement** 來將值插入到上面建立的表中。

String query = "INSERT INTO Tutorial(Name, Type, Logo) VALUES (?, ?, ?)";
PreparedStatement pstmt = con.prepareStatement(query);

使用 PreparedStatement 介面的 setter 方法為佔位符設定值,並使用 setBinaryStream() 方法為 Blob 資料型別設定值。

FileInputStream fin = new FileInputStream("javafx_logo.jpg");

pstmt.setBinaryStream(3, fin);

示例

以下示例演示如何使用 JDBC 程式將影像插入 MySQL 資料庫。在這裡,我們建立一個具有 blob 資料型別的表,將值插入表中(將 **BinaryStream** 物件插入到 blob 型別中),並檢索表的內容。

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class InsertingImageToDatabase {
   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 the Statement
      Statement stmt = con.createStatement();
      //Executing the statement
      String createTable = "CREATE TABLE Tutorial( "
         + "Name VARCHAR(255), "
         + "Type VARCHAR(50), "
         + "Logo BLOB)";
      stmt.execute(createTable);
      //Inserting values
      String query = "INSERT INTO Tutorial(Name, Type, Logo) VALUES (?, ?, ?)";
      PreparedStatement pstmt = con.prepareStatement(query);
      pstmt.setString(1, "JavaFX");
      pstmt.setString(2, "Java_library");
      FileInputStream fin = new FileInputStream("E:\images\javafx_logo.jpg");
      pstmt.setBinaryStream(3, fin);
      pstmt.execute();

      pstmt.setString(1, "CoffeeScript");
      pstmt.setString(2, "scripting Language");
      fin = new FileInputStream("E:\images\coffeescript_logo.jpg");
      pstmt.setBinaryStream(3, fin);
      pstmt.execute();
      pstmt.setString(1, "Cassandra");
      pstmt.setString(2, "NoSQL database");
      fin = new FileInputStream("E:\images\cassandra_logo.jpg");
      pstmt.setBinaryStream(3, fin);
      pstmt.execute();
      System.out.println("Data inserted");
      ResultSet rs = stmt.executeQuery("Select *from Tutorial");
      while(rs.next()) {
         System.out.print("Name: "+rs.getString("Name")+", ");
         System.out.print("Tutorial Type: "+rs.getString("Type")+", ");
         System.out.print("Logo: "+rs.getBlob("Logo"));
         System.out.println();
      }
   }
}

輸出

Connection established......
Data inserted
Name: JavaFX, Tutorial Type: Java_library, Logo: com.mysql.jdbc.Blob@7dc5e7b4
Name: CoffeeScript, Tutorial Type: scripting Language, Logo:
com.mysql.jdbc.Blob@1ee0005
Name: Cassandra, Tutorial Type: NoSQL database, Logo: com.mysql.jdbc.Blob@75a1cd57

**注意:** 使用 JDBC 程式只能儲存和檢索 .gif、.jpeg 或 .png 型別的影像。

更新於:2019年7月30日

4K+ 次瀏覽

啟動您的 職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.