Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MySQL

JDBC Blob data type คืออะไร? วิธีเก็บและอ่านข้อมูล


BLOB (Binary Large Object) เก็บข้อมูล binary ขนาดใหญ่ สูงสุด 65,535 ตัวอักษร ใช้เก็บรูปภาพ ไฟล์ต่างๆ

แตกต่างจาก TEXT: BLOB case-sensitive ในการ sort/compare TEXT ไม่ case-sensitive ไม่ต้องระบุความยาว

เก็บ Blob ในฐานข้อมูล

ขั้นตอน 1: เชื่อมต่อ

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

ขั้นตอน 2: สร้าง PreparedStatement

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

ขั้นตอน 3: ตั้งค่า placeholder

pstmt.setString(1, "sample image");
InputStream in = new FileInputStream("E:\\images\\cat.jpg");
pstmt.setBlob(2, in);

ขั้นตอน 4: รันคำสั่ง

pstmt.execute();

ดึง Blob จากฐานข้อมูล

getBlob() คืน Blob object ใช้ getBytes() ได้ byte array:

while(rs.next()) {
    rs.getString("Name");
    rs.getString("Type");
    Blob blob = rs.getBlob("Logo");
    byte byteArray[] = blob.getBytes(1, (int)blob.length());
    FileOutputStream outPutStream = new FileOutputStream("path");
    outPutStream.write(byteArray);
}

ตัวอย่างครบ

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 {
        DriverManager.registerDriver(new com.mysql.jdbc.Driver());
        String mysqlUrl = "jdbc:mysql://localhost/sampleDB";
        Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
        System.out.println("Connection established......");
        Statement stmt = con.createStatement();
        stmt.execute("CREATE TABLE SampleTable( Name VARCHAR(255), Image BLOB)");
        System.out.println("Table Created");
        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();
        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