Storing images into the Oracle database

Storing images into the Oracle database

SQL offers BLOB(Binary Large Object) data type to store image files like .jpg or .gif into the database table. At the time of creating table, the data type of the image should be declared as ‘BLOB’
The table can created as:
create table imgtab(photo blob, no int);

Example
import java.sql.*;
import java.io.*;
public class test
{
public static void main(String[] args)
{
try{
Class.forName(“oracle.jdbc.driver.OracleDriver”);
Connection con=DriverManager.getConnection( “jdbc:oracle:thin:@localhost:1521:xe”,”system”,”system”);
PreparedStatement ps=con.prepareStatement(“insert into imgtab values(?,?)”);
ps.setString(1,”sonoo”);
FileInputStream fis=new FileInputStream(“d:\\java.jpg”);
ps.setBinaryStream(2,fis,fis.available());
int i=ps.executeUpdate();
System.out.println(i+” records affected”);
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *