MySQL
 Computer >> コンピューター >  >> プログラミング >> MySQL

Javaプログラムを使用してMySQLデータベースに画像を挿入するにはどうすればよいですか?


MySQLデータベースに画像を保持するには、通常、blobタイプが使用されます。したがって、次の説明を含むblobデータ型で作成されたテーブルがあることを確認してください。

+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| Name | varchar(255) | YES | | NULL | |
| image | blob | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+

MySQLに画像を挿入するには データベースについては、以下の手順に従ってください。

ステップ1:データベースに接続する

getConnection()を使用してデータベースに接続できます DriverManagerクラスのメソッド。

jdbc:mysql:// localhost / sampleDB であるMySQLURLを渡して、MySQLデータベースに接続します。 (sampleDBはデータベース名です)、getConnection()メソッドのパラメーターとしてのユーザー名とパスワード。

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

ステップ2:プリペアドステートメントを作成する

prepareStatement()を使用してPreparedStatementオブジェクトを作成します 接続の方法 インターフェース。このメソッドに、挿入クエリ(プレースホルダーを使用)をパラメーターとして渡します。

PreparedStatement pstmt = con.prepareStatement("INSERT INTO MyTable VALUES(?, ?)");

ステップ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:ステートメントを実行する

execute()を使用して、上記で作成したPreparedStatementオブジェクトを実行します。 PreparedStatementのメソッド インターフェイス。

import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class InsertImageToMySqlDB {
   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......");
      PreparedStatement pstmt = con.prepareStatement("INSERT INTO MyTable VALUES(?,?)");
      pstmt.setString(1, "sample image");
      //Inserting Blob type
      InputStream in = new FileInputStream("E:\\images\\cat.jpg");
      pstmt.setBlob(2, in);
      //Executing the statement
      pstmt.execute();
      System.out.println("Record inserted......");
   }
}

出力

Connection established......
Record inserted......

  1. JavaをMySQLに接続する方法は?

    JavaをMySQLに接続するための、Javaコードは次のとおりです- import java.sql.Connection; import java.sql.DriverManager; public class LostConnectionURLDemo {    public static void main(String[] args){       String JDBCURL="jdbc:mysql://localhost:3306/web?autoReconnect=true";     &

  2. MySQLデータベースにnull値を挿入するJavaアプリケーション?

    Javaでnull値を設定するには、ステートメントは次のとおりです- ps.setNull(yourIndex, Types.NULL); まずテーブルを作成しましょう- mysql> create table DemoTable1893    (    FirstName varchar(20)    ); Query OK, 0 rows affected (0.00 sec) Javaコードは次のとおりです- import java.sql.Connection; import java.sql.DriverManag