Monday , September 25 2017
Home / jdbc / Insert an Image using JDBC in Mysql DB

Insert an Image using JDBC in Mysql DB

In this tutorial, we are going to learn about how to insert an image using JDBC in mysql database. As we already discussed in the previous example on PreparedStatement in JDBC, one of the main advantage of PreparedStatement is, it can handle the large objects (images, videos and etc..).

For this example, we are going to use MySql database for inserting an image. In Mysql, if we want to store the binary data, we have to define the column with BLOB (Binary Large Object) type.

Insert an Image using JDBC :

Some important points about to handle the large objects in JDBC.

  • If want to insert an image using JDBC into database, or read an image from database then we need to use PreparedStatement of JDBC
  • In database, the image will not be stored directly. The bytes of an image (binary data) will be stored.
  • To store the image in data base, we should declare the column type as BLOB.
  • BLOB type of column can store the data up-to a maximum of 4 GB.
  • To set the binary data (bytes) of an image to sql command, we need to use the setBinaryStream() method on PrepareStatement object.

Here is the complete example to insert an image using JDBC into database.

Insert an Image using JDBC Example :

Create a table like below :

CREATE TABLE employee (
id int(10) DEFAULT NULL,
name varchar(100) DEFAULT NULL,
salary varchar(100) DEFAULT NULL,
photo blob
);

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class JDBC_LargeObjects_Example {

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub

        Connection connection = null;
        PreparedStatement pstatement = null;
        Scanner scanner = null;
        String img = "C:/Users/cgoka/Desktop/sample.jpg";

        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            connection = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/jdbc", "root", "123456");
            pstatement = connection
                    .prepareStatement("insert into employee values(?,?,?,?)");
            pstatement.setInt(1, 101);
            pstatement.setString(2, "Chandra Shekhar");
            pstatement.setString(3, "3000");
            File file = new File(img);
            FileInputStream stream = new FileInputStream(file);
            pstatement.setBinaryStream(4, stream);

            pstatement.executeUpdate();
            System.out.println("Image Inserted");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

On the above example, we are using the FileInputStream to read the image from the local computer. And passing the FileInputStream to PrepareStatement object using setBinaryStream() method.

To run the above code in your system, you need to give your local image path to img variable.

Here is an example to read an image from db using jdbc.

Happy Learning 🙂

About chandrashekhar

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Recommended

JDBC Updatable ResultSet Example

Whenever we create a ResultSet object which never allows us to update the database through …

Leave a Reply

Your email address will not be published. Required fields are marked *