Insert images in database

Google Advertisements

Prev Tutorial Next Tutorial

Insert images in database

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();

Prev Tutorial Next Tutorial

Google Advertisements

Buy This Ad Space @$50 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

Cloud computing is the on demand availability of computer system resources, especially data storage and computing power, without direct active management by the user. Cloud Computing Tutorial

College Projects Related to Java, AWT, C Projects for College, C++ Projects for College, Android Projects. Download Java C C++ Projects

Download Projects

Adsense Advertisements

Buy This ads Space 8076671483

Buy This Ad Space @$120 per Month, Ad Size 300X600 Contact on: or 8076671483 Try this Keyword C++ Programs