setBinaryStream() วิธีการของ PreparedStatement อินเทอร์เฟซยอมรับจำนวนเต็มที่แทนดัชนีของพารามิเตอร์และวัตถุ InputStream และตั้งค่าพารามิเตอร์เป็นวัตถุ InputStream ที่กำหนด เมื่อใดก็ตามที่คุณต้องการส่งค่าไบนารีจำนวนมาก คุณสามารถใช้วิธีนี้ได้
และฐานข้อมูล SQL มีประเภทข้อมูลชื่อ Blob (Binary Large Object) ซึ่งคุณสามารถจัดเก็บข้อมูลไบนารีขนาดใหญ่เช่นรูปภาพได้
การจัดเก็บรูปภาพโดยใช้ 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);
ตั้งค่าให้กับตัวยึดตำแหน่งโดยใช้เมธอด setter ของอินเทอร์เฟซ PreparedStatement และสำหรับการตั้งค่าประเภทข้อมูล Blob โดยใช้เมธอด setBinaryStream()
FileInputStream fin = new FileInputStream("javafx_logo.jpg");
pstmt.setBinaryStream(3, fin);
ตัวอย่าง
ต่อไปนี้เป็นตัวอย่างสาธิตวิธีการแทรกรูปภาพในฐานข้อมูล MySQL โดยใช้โปรแกรม JDBC ที่นี่ เรากำลังสร้างตารางโดยมีค่าแทรกประเภทข้อมูล Blob ลงในตาราง (BinaryStream วัตถุเป็นชนิดหยด) และดึงเนื้อหาของตาราง
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();
}
}
} ผลลัพธ์
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
หมายเหตุ: คุณสามารถจัดเก็บและเรียกค้นเฉพาะรูปภาพประเภท .gif หรือ .jpeg หรือ .png โดยใช้โปรแกรม JDBC