Phương thức setBinaryStream() của interface PreparedStatement chấp nhận nguyên đại diện cho chỉ số tham số và đối tượng InputStream, thiết lập tham số thành InputStream đó. Dùng khi gửi giá trị binary rất lớn.
Database SQL cung cấp kiểu dữ liệu Blob (Binary Large Object) để lưu dữ liệu binary lớn như ảnh.
Lưu ảnh bằng JDBC
Tạo bảng với kiểu Blob:
CREATE TABLE Tutorial(Name VARCHAR(255), Type INT NOT NULL, Logo BLOB);
Kết nối database, chuẩn bị PreparedStatement chèn giá trị:
String query = "INSERT INTO Tutorial(Name, Type, Logo) VALUES (?, ?, ?)"; PreparedStatement pstmt = con.prepareStatement(query);
Thiết lập giá trị cho placeholder bằng setter methods, với Blob dùng setBinaryStream():
FileInputStream fin = new FileInputStream("javafx_logo.jpg");
pstmt.setBinaryStream(3, fin);
Ví dụ
Ví dụ chèn ảnh vào MySQL bằng JDBC. Tạo bảng có blob, chèn giá trị (BinaryStream object cho blob), truy xuất nội dung:
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 {
// Đăng ký Driver
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
// Lấy kết nối
String mysqlUrl = "jdbc:mysql://localhost/sampleDB";
Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
System.out.println("Kết nối thành công......");
// Tạo Statement
Statement stmt = con.createStatement();
// Tạo bảng
String createTable = "CREATE TABLE Tutorial( "
+ "Name VARCHAR(255), "
+ "Type VARCHAR(50), "
+ "Logo BLOB)";
stmt.execute(createTable);
// Chèn giá trị
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("Dữ liệu đã chèn");
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();
}
}
}
Kết quả
Kết nối thành công...... Dữ liệu đã chèn 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
Lưu ý: Chỉ lưu và truy xuất được ảnh .gif, .jpeg, .png bằng JDBC.