How To Store Image Into MySQL Using Java

In this section we will discuss about how to store an image into the database using Java and MySQL.

How To Store Image Into MySQL Using Java

In this section we will discuss about how to store an image into the database using Java and MySQL.

How To Store Image Into MySQL Using Java

How To Store Image Into MySQL Using Java

In this section we will discuss about how to store an image into the database using Java and MySQL.

This example explains you about all the steps that how to store image into MySQL database. To store an image into database there are four data types tinyblob, blob, mediumblob, and longblob that stores byte value. These data types are different on the basis of their size of storage.

Size of these data types are as follows :

tinyblob : Can store the maximum length of 2^8-1 characters.

blob : Can store the maximum length of 2^16-1 characters.

mediumblob : Can store the maximum length 2^24-1 characters.

longblob : Can store the maximum length of  2^32-1 characters.

To store the image into database using Java we will be required to convert the image file into InputStream.

Example

Here I am giving a simple example which will demonstrate you about how to store image into database using Java. We will use the MySQL database system to store the image. For achieving the solution of such problem we will first create a database table using MySQL. Then we will create a Java program to insert the image into database. In this program will write the code for connecting the database and then converted the file into InputStream and then we will write the SQL query for inserting the image into the database. We will use here the PreparedStatement to execute the "insert" query. The value which has to be stored will be set by the corresponding setXXXX() method of PreparedStatement such as setString() method for providing the string value, setBinaryStream() for providing the large binary value as InputStream object etc.

Source Code

StoreImageIntoDB.java

package image;

import java.sql.*;
import java.io.*;

public class StoreImageIntoDB {
	
	public static void main(String args[])
	{
		String className = "com.mysql.jdbc.Driver";
		String url = "jdbc:mysql://localhost/record";
		String user = "root";
		String password = "root";
		Connection con = null;
		PreparedStatement ps = null;
		InputStream is = null;
		try
		{
			Class.forName(className);
			con = DriverManager.getConnection(url, user, password);
			File image = new File("C:\\Documents and Settings\\bharat\\Desktop\\bipul\\logo.gif");
			System.out.println(image.getPath());
			is = new FileInputStream(image);
			ps = con.prepareStatement("insert into image(img_id, img_title, img_data, img_location, img_name)"
							+ "values(?,?,?,?,?)");
			ps.setInt(1, 1);
			ps.setString(2, "Rose India");
			ps.setBinaryStream(3, is,
					(int) (image.length()));
			ps.setString(4, image.getParent());
			ps.setString(5, image.getName());
			
			int i = ps.executeUpdate();
			if(i > 0)
			{
				System.out.println("Image Stored successfully");
			}
			ps.close();
			is.close();
		}
		catch(Exception e)
		{
			System.out.print(e);
		}
	}
}

Output:

When you will execute the above example and the image is stored successfully then the output will be as follows :

Download Source Code