ホームページ  >  記事  >  データベース  >  JDBCを使用して画像をデータベースに挿入するにはどうすればよいですか?

JDBCを使用して画像をデータベースに挿入するにはどうすればよいですか?

WBOY
WBOY転載
2023-08-29 14:05:02744ブラウズ

如何使用 JDBC 将图像插入数据库?

PreparedStatementインターフェイスの setBinaryStream() メソッドは、パラメーター インデックスと InputStream オブジェクトを表す整数を受け取り、パラメーターを次のように設定します。指定されたInputStreamオブジェクト。非常に大きなバイナリ値を送信する必要がある場合は、このメソッドをいつでも使用できます。

SQL データベースは、画像などの大きなバイナリ データを格納できる Blob (Binary Large Object) と呼ばれるデータ型を提供します。

JDBC を使用した画像の保存

JDBC プログラムを使用して画像をデータベースに保存する必要がある場合は、次のように Blob データ型のテーブルを作成します。 、JDBC を使用してデータベースに接続し、

PreparedStatement

を準備します。上で作成したテーブルに値を挿入します。

CREATE TABLE Tutorial(Name VARCHAR(255), Type INT NOT NULL, Logo BLOB);
PreparedStatement インターフェイスの setter メソッドを使用して、プレースホルダーの値を設定します。 setBinaryStream( ) メソッドを使用して、Blob データ型の値を設定します。

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

次の例は、JDBC プログラムを使用して MySQL データベースにイメージを挿入する方法を示しています。ここでは、Blob データ型を含むテーブルを作成し、テーブル (Blob 型の

BinaryStream

オブジェクト) に値を挿入し、テーブルの内容を取得します。

FileInputStream fin = new FileInputStream("javafx_logo.jpg");
pstmt.setBinaryStream(3, fin);
出力

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://localhost/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();
      }
   }
}

注:

JDBC プログラムを使用して、.gif、.jpeg、または .png タイプの画像のみを保存および取得できます。

以上がJDBCを使用して画像をデータベースに挿入するにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はtutorialspoint.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。