❮ Previous Next ❯

JDBC-Store image in Oracle Database

It is possible that we can store images in the database by executing jdbc program with the help of PreparedStatement interface.
The setBinaryStream() method of PreparedStatement is used to set Binary information into the parameterIndex.
For example:
CREATE TABLE user  
(
id varchar2(100),
name varchar2(100),
image BLOB
);  
Let's write the program to store the image in the database. Here we are using e:\\a.jpg for the location of image. You can change it according to the image location.

JDBC Example to store image in the database

import java.sql.*;  
import java.io.*;  
public class InsertImageDemo {  
public static void main(String[] args) throws Exception{  
Class.forName("oracle.jdbc.driver.OracleDriver");  
Connection con=DriverManager.getConnection(  
"jdbc:oracle:thin:@localhost:1521:xe","system","oracle");  
              
PreparedStatement ps=con.prepareStatement("insert into user values(?,?,?)");  
ps.setString(1,101);  
ps.setString(2,"Tapuuu");  

  
FileInputStream fin=new FileInputStream("e:\\a.jpg");  
ps.setBinaryStream(2,fin,fin.available());  
int k=ps.executeUpdate();  
System.out.println(k+" row inserted");  
          
con.close();  
}
}  
Output
1 row inserted
If you see the table, record is stored in the database but image will not be shown. For this, you need to retrieve the image from the database.

❮ Previous Next ❯


Follow Us




  Tutorials

Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved