Illustration to store picture in Oracle database

You can store pictures in the database in java by the help of PreparedStatement interface. 

The setBinaryStream() system for PreparedStatement is utilized to set Binary data into the parameterIndex. 

Mark of setBinaryStream system 

The sentence structure of setBinaryStream() strategy is given underneath: 

1) open void setBinaryStream(int paramIndex,InputStream stream) 

tosses SQLException 

2) open void setBinaryStream(int paramIndex,InputStream stream,long length) 

tosses SQLException 

For putting away picture into the database, BLOB (Binary Large Object) datatype is utilized as a part of the table. For instance: 

Make TABLE "IMGTABLE" 

( "NAME" VARCHAR2(4000), 

"Photograph" BLOB 

) 

/ 

How about we compose the jdbc code to store the picture in the database. Here we are utilizing d:\\d.jpg for the area of picture. You can transform it as indicated by the picture area. 

import java.sql.*; 

import java.io.*; 

open class InsertImage { 

open static void main(String[] args) { 

try{ 

Class.forName("oracle.jdbc.driver.OracleDriver"); 

Association con=DriverManager.getConnection( 

"jdbc:oracle:thin:@localhost:1521:xe","system","oracle"); 

PreparedStatement ps=con.prepareStatement("insert into imgtable values(?,?)"); 

ps.setString(1,"sonoo"); 

FileInputStream fin=new FileInputStream("d:\\g.jpg"); 

ps.setBinaryStream(2,fin,fin.available()); 

int i=ps.executeUpdate(); 

System.out.println(i+" records influenced"); 

con.close(); 

}catch (Exception e) {e.printStackTrace();} 

} 

} 

On the off chance that you see the table, record is put away in the database however picture won't be appeared. To do as such, you have to recover the picture from the database which we are covering in the following page.


hire-me