JDBC- Insert/Store/save IMAGE in database by using PreparedStatement's setBinaryStream and executeUpdate methods, using BLOB data type - in java


In this tutorial we will learn how to Insert/Store/save IMAGE in database by using PreparedStatement's setBinaryStream and executeUpdate methods, using BLOB data type in java jdbc.


We must use BLOB data type for storing images, why not CLOB? Please read Difference between CLOB and CLOB data type in Oracle

What is BLOB data type in Oracle database >
BLOB stands for Binary Large Object.

BLOB stores values in bitstreams.

BLOB is used for storing binary data.

BLOB data type is appropriate for storing following >
  • image,
  • graphical,
  • voice and
  • some application specific data.


--Before executing java program execute these database scripts  >
create table TEST_IMG(ID number(4), IMAGE_COL blob);

And keep some image in c:/myImg.png (or you may change directory here and in below program)

--If table already exists then execute the DROP command >
drop table TEST_IMG;



Before execution of below program TEST_IMG table will look like this >

After execution of below program TEST_IMG table will look like this >

Must read : FILE - Storing in and retrieving out from database


Example/ Full Programs JDBC- Insert/Store/save image in database by using PreparedStatement's executeUpdate method, using BLOB data type in java
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class StoreImageExample {
   public static void main(String... arg) {
          Connection con = null;
          PreparedStatement prepStmt = null;
          FileInputStream fin = null;
          String imagePath="c:/myImg.png";
          try {
                 // registering Oracle driver class
                 Class.forName("oracle.jdbc.driver.OracleDriver");
                 // getting connection
                 con = DriverManager.getConnection(
                              "jdbc:oracle:thin:@localhost:1521:orcl",
                              "ankit", "Oracle123");
                 System.out.println("Connection established successfully!");
                
                
                
                 //create FileInputStream object
                 fin=new FileInputStream(imagePath);
                
                 //prepare insert query
                 prepStmt=con.prepareStatement("INSERT into TEST_IMG (ID, IMAGE_COL) "
                              + "values (1, ?)");
                 //Substitute first occurrence of ? with fileInputStream
                 prepStmt.setBinaryStream(1, fin);
                 //execute insert query
                 int numberOfRowsInserted = prepStmt.executeUpdate();
                 System.out.println("numberOfRowsInserted = " + numberOfRowsInserted);
                
                 System.out.println(imagePath+" > Image stored in database");
                
          } catch (Exception e) {
                 e.printStackTrace();
          }
          finally{
                 try {
                       if(fin!=null) fin.close(); //close file
                       if(prepStmt!=null) prepStmt.close(); //close PreparedStatement
                       if(con!=null) con.close(); // close connection
                 } catch (SQLException e) {
                       e.printStackTrace();
                 } catch (IOException e) {
                       e.printStackTrace();
                 }
          }
   }
}
/*OUTPUT
Connection established successfully!
numberOfRowsInserted = 1
c:/myImg.png > Image stored in database
*/



So in this tutorial we learned how to Insert/Store/save IMAGE in database by using PreparedStatement's setBinaryStream and executeUpdate methods, using BLOB data type in java jdbc.

RELATED LINKS>

JDBC tutorial program- Batch PreparedStatement example- Execute INSERT query(DML command) using PreparedStatement's addBatch() and executeBatch() methods in java


JDBC tutorial- Calling Oracle database STORED PROCEDURE and pass its IN parameter from java - CallableStatement example in java

JDBC tutorial program- Calling Oracle database STORED PROCEDURE- OUT parameter - CallableStatement example in java

JDBC tutorial- Calling Oracle database STORED PROCEDURE- IN OUT parameter - CallableStatement example in java


JDBC tutorial program- Calling Oracle database FUNCTION - CallableStatement example in java


No comments:

Post a Comment