4

I have poor knowledge about programming. I need to save image in MySQL database. I have created a database table and there is a column to add image with longblob data type. I have code to a button to choose image from folder in PC then it load to a jlable. Now I need to insert this image to a database.

This is my code;

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    JFileChooser fc=new JFileChooser(); 
    fc.showOpenDialog(this); 
    File f=fc.getSelectedFile(); 
    String path=f.getAbsolutePath(); 
    jLabel1.setIcon(new ImageIcon(path)); 

    try{ 
        FileInputStream fin=new FileInputStream(f); 
        int len=(int)f.length(); Class.forName("com.mysql.jdbc.Driver"); 

        Connection con=DriverManager.getConnection("jdbc:my­sql://localhost/hss", "root", "bis123"); 
        PreparedStatement ps=con.prepareStatement("Insert into profile values(?)"); 

        ps.setBinaryStream(1, fin, len); 
        int status=ps.executeUpdate(); 

        if(status > 0) { 
            jLabel2.setText("Successfully inserted in DB"); 
        }else{ 
            jLabel2.setText("Image not inserted!"); 
        } 
    }catch(Exception e){
        System.out.println(e); 
    }
}

1 Answer 1

5

In MySQL when we use the blob type to store the data , it support only 5 kb image capacity.

CREATE TABLE image (

id varchar(45) DEFAULT NULL,

size int(11) DEFAULT NULL,

image longblob

);

created database table in above code

this is java code to insert image in database

import java.sql.*;
import java.io.*;
public class InsertImagesMysql{
public static void main(String[] args){
    System.out.println("Insert Image Example!");
    String driverName = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "test";
    String userName = "root";
    String password = "root";
    Connection con = null;
    try{
       Class.forName(driverName);
       con = DriverManager.getConnection(url+dbName,userName,password);
       Statement st = con.createStatement();
       File imgfile = new File("pic.jpg");

      FileInputStream fin = new FileInputStream(imgfile);

       PreparedStatement pre =
       con.prepareStatement("insert into Image values(?,?,?)");

       pre.setString(1,"test");
       pre.setInt(2,3);
       pre.setBinaryStream(3,(InputStream)fin,(int)imgfile.length());
       pre.executeUpdate();
       System.out.println("Successfully inserted the file into the database!");

       pre.close();
       con.close(); 
    }catch (Exception e1){
        System.out.println(e1.getMessage());
    }
}
    }

here is code to retrieve data from database

import java.io.*;
import java.sql.*;
public class RetriveImagesMysql{
public static void main(String[] args){
    System.out.println("Retrive Image Example!");
    String driverName = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "test";
    String userName = "root";
    String password = "root";
    Connection con = null;
    try{
        Class.forName(driverName);
        con = DriverManager.getConnection(url+dbName,userName,password);
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("select image from image");
        int i = 0;
        while (rs.next()) {
            InputStream in = rs.getBinaryStream(1);
            OutputStream f = new FileOutputStream(new File("test"+i+".jpg"));
            i++;
            int c = 0;
            while ((c = in.read()) > -1) {
                f.write(c);
            }
            f.close();
            in.close();
        }
    }catch(Exception ex){
        System.out.println(ex.getMessage());
    }
}
  }

here just assign fin to jbutton action event it will trigger automatically the run of the code

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Smash. I will try this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.