Save and Retrieve Image from MySql Database Using Java

Save and Retrieve Image from MySql Database Using Java

Here you will get an example for save and retrieve image from MySql database using Java.

Being developed we for the most part use envelopes for overseeing images. In any case, we

can store images legitimately in the database using BLOB (Binary Large Object) information type.

MySql has the following mass sorts:

TINYBLOB: 255 bytes

Mass: 64 KB

MEDIUMBLOB: 16 MB

LONGBLOB: 4 GB

Save and Retrieve Image from MySql Database Using Java

Contingent on prerequisite we can utilize any sort. It prescribes no to utilize database for putting away images with

enormous size since it will build the database size.

Underneath I have given an example of how to store and retrieve images from the database.

The portrayal of the table that I utilized is given underneath.

Save and Retrieve Image from MySql Database Using Java

Save and Retrieve Image from MySql Database Using Java

How to Save Image in Database

package com;
 
import java.sql.*;
import java.io.*;
 
public class DatabaseImageExample {
	public static void main(String args[]){
		try{
			Class.forName("com.mysql.jdbc.Driver");
			Connection con=DriverManager.getConnection("jdbc:mysql://localhost/demo","root","root");
			
			File file=new File("E:\\image.png");
			FileInputStream fis=new FileInputStream(file);
			
			PreparedStatement ps=con.prepareStatement("insert into image_table (name,image) values(?,?)"); 
			ps.setString(1,"image 1");
			ps.setBinaryStream(2,fis,(int)file.length());
			ps.executeUpdate();
 
			ps.close();
			fis.close();
			con.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}

Above example will take an image from area E:\image.png and save it into a database table

In reality, you can’t see the image legitimately in the table. You need to retrieve it from the database

and then save it to some area. Underneath example shows how you can do this.

How to Retrieve Image from Database

package com;
 
import java.io.*;
import java.sql.*;
 
public class DatabaseImageExample {
	public static void main(String args[]){
		try{
			Class.forName("com.mysql.jdbc.Driver");
			Connection con=DriverManager.getConnection("jdbc:mysql://localhost/demo","root","root");
			
			File file=new File("E:\\image1.png");
			FileOutputStream fos=new FileOutputStream(file);
			byte b[];
			Blob blob;
			
			PreparedStatement ps=con.prepareStatement("select * from image_table"); 
			ResultSet rs=ps.executeQuery();
			
			while(rs.next()){
				blob=rs.getBlob("image");
				b=blob.getBytes(1,(int)blob.length());
				fos.write(b);
			}
			
			ps.close();
			fos.close();
			con.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}

Above example will get image from database and save it at area E:\image1.png.

Remark beneath on the off chance that you confronting trouble to understand above code.

Happy Coding!! ???? ????

Leave a Comment

error: Alert: Content is protected!!