How do I store BLOBs data into database?

Database BLOBs (Binary Large Objects) can be used to stored any data, it could be image, audio or video file for instance. This example shows you how we use JDBC library to store image in our database. To send the binary information to the database we can call the PreparedStatement.setBinaryStream() method and pass the appropriate input stream and it size.

package org.kodejava.example.sql;

import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class BlobDemo {
    private static String url = "jdbc:oracle:thin:@localhost:1521:xe";
    private static String username = "kodejava";
    private static String password = "welcome";

    public static void main(String[] args) throws Exception {
        Connection conn = null;
        FileInputStream fis = null;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            conn = DriverManager.getConnection(url, username, password);
            conn.setAutoCommit(false);

            String sql = "INSERT INTO pictures (name, description, image) VALUES (?, ?, ?)";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1, "java.gif");
            stmt.setString(2, "Java Official Logo");

            File image = new File("D:\Projects\kodejava\web\images\java.gif");
            fis = new FileInputStream(image);
            stmt.setBinaryStream(3, fis, (int) image.length());
            stmt.execute();

            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                fis.close();
            }
            if (conn != null && !conn.isClosed()) {
                conn.close();
            }
        }
    }
}

Wayan

Programmer, runner, recreational diver, live in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, Hibernate / JPA.

1 Comments

Leave a Reply