Insert & read image from database - Java

Q. Write a program to insert and read an image from the database table (Using BLOB).

Answer:

BLOB is nothing bug Binary Large Object. BLOB is used to store large amount of binary data into database like images, etc. Below example shows how to store images into database rows.

InsertImage.java

import java.sql.*;
import java.io.*;
class InsertImage
{
     static String DBURL = "jdbc:oracle:thin:@localhost:1521:XE";
     static String DBUSER = "local";
     static String DBPASS = "test";
     public static void main(String args[])
     {
          try
          {
               //Load the driver
               Class.forName("oracle.jdbc.driver.OracleDriver");
               //Cretae the connection object
               Connection con = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
               //Inserting the record in Image table
               String sql = "insert into Image values(?, ?, ?)";
               PreparedStatement ps = con.prepareStatement(sql);
               ps.setInt(1, 101);
               ps.setString(2, "Samsung");
               //read the image file
               FileInputStream fin=new FileInputStream("mob.jpg");  
               ps.setBinaryStream(2,fin,fin.available());  
               int i=ps.executeUpdate();  
               System.out.println(i+" records affected");  
               //close
               fin.close();
               ps.close();
               con.close();  

          }
          catch(ClassNotFoundException ex)
          {
               ex.printStackTrace();
          }
          catch(SQLException ex)
          {
               ex.printStackTrace();
          }
          catch(FileNotFoundException ex)
          {
               ex.printStackTrace();
          }
          catch(IOException ex)
          {
               ex.printStackTrace();
          }
     }
}


Output:

This program compile successfully, and then run it.

Now if we see the table the record is inserted into the table, but the image will be not shown.