how to insert image into mysql database using java Netbeans with source code

how to insert image into mysql database using java Netbeans with source code

This tutorial guide to insert image into mysql database using java project (JFrame). in next tutorial show you, how to display image using java code from MySQL database. Please note that I used LONG BLOB that can handle up to 2,048 KiB of data.

Do you need help to remotely set up my any project on your machine or customize any project with your requirement please contact

Follow these steps to insert image into mysql database

Step 01 Open New java project

Step 02 Create Database

Should create a database and a table to store the images. The image store field of the table should be LONG BLOB data type.

Step 03 creates a database connection with java project

Database Connections of insert image into mysql database project

package image;
import java.sql.Connection;
import java.sql.DriverManager;

public class DBConnect {   
     public static Connection connect()
     Connection con=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");                                con=DriverManager.getConnection("jdbc:mysql://localhost:3306/yourDatabaseNameHere?","DatabaseUserNameHere","DatabasePasswordHere");   
catch (Exception e) 
   return con;

Add “MySQL.jdbc.Driver” to the project library.

If you need more information to connect Netbeans with Mysql database please watch this video

Step 04 Add new JFrame to project

Add a label and two buttons to JFrame

Source code for insert image into mysql database

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import java.awt.Image;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Main extends javax.swing.JFrame {
   private ImageIcon format=null;
   String fname=null;
   int s=0;
   byte[] pimage=null;
   Connection conn=null;
   public Main() {
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

  // using this code you can brows image and image set to lable

        JFileChooser fchoser=new JFileChooser();
        File f=fchoser.getSelectedFile();
        ImageIcon micon=new ImageIcon(fname);        
        try {
            File image=new File(fname);
            FileInputStream fis=new FileInputStream(image);
            ByteArrayOutputStream baos=new ByteArrayOutputStream();
            byte[] buf=new byte[1024];
            for(int readnum; ( !=-1;)
            lableVeriableName.setIcon(resizeImage(fname, buf));
        } catch (Exception e) {

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
      // This code use to insert image to database
        String id=txtid.getText(); 
        try {
             String q= "INSERT INTO `img`(`id`, `image`) VALUES (?,?)";               
               PreparedStatement pst=conn.prepareStatement(q);                
                pst.setString(1, id);
                pst.setBytes(2, pimage);                
        } catch (Exception e) {
// This code use to resize image to fit lable
public ImageIcon resizeImage(String imagePath, byte[] pic){
        ImageIcon myImage=null;
        if(imagePath !=null)
        myImage=new ImageIcon(imagePath);
         myImage=new ImageIcon(pic);
        Image img=myImage.getImage();
        Image img2=img.getScaledInstance(lblimage.getHeight(),    lblimage.getWidth(),  Image.SCALE_SMOOTH);
        ImageIcon image=new ImageIcon(img2);
        return image;

Check more projects>>

Java Tutorials