Here you will get an example for save and retrieve image from MySql database using Java.
In development we generally use folders for managing images. But we can store images directly in database using BLOB (Binary Large Object) data type.
MySql has following blob types:
TINYBLOB: 255 bytes
BLOB: 64 KB
MEDIUMBLOB: 16 MB
LONGBLOB: 4 GB
Depending upon requirement we can use any type. It is recommend no to use database for storing images with large size because it will increase the database size.
Below I have given an example of how to store and retrieve images from database. The description for table that I used is given below.
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 image from location E:\\image.png and save it into database table. Actually you can’t see the image directly in the table. You have to retrieve it from database and then save it to some location. Below 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 fetch image from database and save it at location E:\\image1.png.
Comment below if you facing difficulty to understand above code.
Happy Coding!! 🙂 🙂
Hi Neeraj,
I need how to download the image in database table and the image is stored in server location.
i am using spring and hibernate framework.
Sorry I have no idea about spring and hibernate.
hello..
what would i need to change to retrieve to a jlabel in swing form?
this is driving me crazy…
Thanks
Gator
After getting the image in byte from database add following lines.
Image img = Toolkit.getDefaultToolkit().createImage(image);
ImageIcon icon = new ImageIcon(img);
JLabel lPhoto = new JLabel();
lPhoto.setIcon(icon);
Here image is of byte type. I hope this will help.
I’ve tried the way you and I do not get the error. but I also don’t get the image from a database
“pos” + “length” arguments can not be larger than the BLOB’s length.
above is an SqlException coming when i tried to retrieve an image .
The maximum length of Blob type is 64kb, you are trying to retrieve an image which is greater than 64kb. Use bigger types like mediumblob or longblob.
what if i wanna to upload and insert and retrive
I want to download blob data for 5800K records and avg size of one blob is 2kb so total data would be 11 tb approx.
Thanks a lot broo
how to retrieve the image along with some data from database using only java
Am successful triving an image, but it’s coming with 0 kb, that course not to open, can you help me with that…
Nice work and more helpfull
How I can retrieve image from mysql database through its path not of blob datatype
how to upload the file and same file will be download in sql with java . i need a coding
how we can retrieve image from mysql database to print in jTable please guide me.
sir please help me I want to insert a passport size photo in netbeans jforms from user and their fingerprint also by using fingerprint scanner device so please help me …
plz give me idea and way to code this problem..and sir can you tell me that where I can learn all about these classes and how to use them form internet ..
will this code fetch all images /files from database ? How to achieve download all files ?
After retrieving image from database i am getting image but image size is 0 kb and getting error could not open .
what is command to display those images
how can i carry out a CRUD operation with an image field using DAO and MVC patterns with JSTL.
i’ve tried using your example but i run into problem when it comes to converting blob to string and vice versa.
hello
once the image is retrieved from database is it possible to use that image in tag in jsp…
Hi
Is there any equivalent class like java.sql.Blob for MEDIUMBLOB?
Can You Please do the same image saving thing without using PreparedStatement method.
image is not retriving from the databse. In jsp file it is image column showing null
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 58 in the jsp file: /user_view.jsp
The method print(boolean) in the type JspWriter is not applicable for the arguments (void)
55:
56: <a style="text-decoration: none;" href="status.jsp?mail=”/>
57:
58:
59:
60:
61:
How to use package com; in the code ..I can’t find it anywhere to download it’s .jar file .please help
Sir,Can we get code to save and retrieve image using database sqlite