Insert images in database

Google Advertisements

JDBC databasemetadata Retrieve Image JDBC

How to Insert Images in Database JDBC

you can not Insert a picture in database directly. but you can store binary data of picture file. To insert image in database we need a column of type BLOB (Binary Large Object).

insert image in databse

Important Points

  • In Jdbc only PreparedStatement support the binary data transfer between a Java application to database.
  • Jdbc supports only gif or jpeg or png type of images to insert or read from a database.
  • To set binary data into a parameter of PreparedStatement object, you need to call setBinaryStream().

Parameters of setBinaryStream()

  • parameter index
  • fileInputStream object
  • Size of the file

Find size of Image

To find the size of an image file we need File class object for that file.


File f=new File("c:/img001.gif");
int size=(int)f.length();

Note: In Java forward slash (/) is allowed but backward slash (\) is not allowed at the time of writing of path.

Example to Insert Image in database

import java.sql.*;
import java.util.*;
class PhotoInsert 
Connection con;
public void openCon()throws Exception
System.out.println("connection is opened");
public void insert()throws Exception
Scanner s=new Scanner(;
PreparedStatement pstmt=con.prepareStatement("insert into emp_info values(?,?,?)");
System.out.println("enter emp id");
int empid=s.nextInt();
System.out.println("enter emp name");
System.out.println("enter photo file path");

File f=new File("c:/pho001.gif");
int size=(int) f.length();
FileInputStream fis=new FileInputStream(f);
int i=pstmt.executeUpdate();
System.out.println(i+"row inserted");
public void closeCon()throws Exception

public static void main(String[] args)throws Exception 
 PhotoInsert p1= new PhotoInsert();

JDBC databasemetadata Retrieve Image JDBC

Google Advertisements

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: or 8076671483

Pure VPN Privide Lowest Price VPN Just @ $1.65. Per Month with Non Detected IP Lowest Price Non Detected IP VPN

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

Download Projects

Google Advertisements

Buy Websites 9999595223

Buy Fresh and Payment Receive Account with Website. Contact on: or 9999595223 Try this Keyword C++ Programs