Inserting Image in Database Table

In this section, you will learn to insert an image to the MySQL database table.

Ads

Tutorials   
Change Column Name in MySQL Java Mysql Connection Example JDBC Example with MySQL Jdbc Mysql Connection String Jdbc Mysql Connection Url Getting Column Names from a database table in Java Count Rows from a Database Table JDBC Example with MySQL Write Text File to Table Inserting Image in Database Table Insert Data in Table Using Stored Procedure Copy One Database Table to Another Copy Table in a MySQL Database Set Timestamp by using the Prepared Statement Set Time by using the Prepared Statement Set Date by using the Prepared Statement Prepared Statement Set Big Decimal Set byte, short and long data types by using the Prepared Statement Set Data Types by using Prepared Statement Using the Prepared Statement Twice Deleting Records using the Prepared Statement Count Records using the Prepared Statement Inserting Records using the Prepared Statement Update Records using Prepared Statement Select Records Using Prepared Statement Prepared Statement With Batch Update Statement Batch Update Prepared Statement Set Object Cross Join Tables in a Specific Database Join tables with the NATURAL RIGHT JOIN operation Join tables with the NATURAL LEFT JOIN operation Join tables in the specific database Delete a Column from a Database Table Delete a Specific Row from a Database Table Deleting All Rows from a Database Table Sum of Column in a Database Table Arrange a Column of Database Table Arrange a Column of Database Table Remove Unique Column in Database Table Make Unique Column in Database Table

Inserting Image in Database Table

     

In this section, you will learn to insert an image to the MySQL database table. 

Flow of the process :

This program helps the user to insert an image in the MySQL database table. To insert an image, you need to establish a connection with MySQL database. Lets' take a database hibernatetutorial, in our case. This database has a table (Image table). For inserting an image in table, we have used the SQL statement ?INSERT INTO Image values(?,?,?)? with the prepareStatement() method. Whenever an image is inserted into the database, it displays ?Inserting Successfully?. If it fails to insert an image then it throws an exception.

[Note: The table has a specified field (or data type) to store an image of type e.g. Blob]

Here is the Code of Program :

import java.sql.*;
import java.io.*;

public class insertImage{
  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 = "hibernatetutorial";
  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("images.jpg");
  FileInputStream fin = new FileInputStream(imgfile);
  PreparedStatement pre = con.prepareStatement("insert into Image values(?,?,?)");
  pre.setInt(1,5);
  pre.setString(2,"Durga");
  pre.setBinaryStream(3,fin,(int)imgfile.length());
  pre.executeUpdate();
  System.out.println("Inserting Successfully!");
  pre.close();
  con.close();  
  }
  catch (Exception e){
  System.out.println(e.getMessage());
  }
  }
} 

Download this code.

Output of this Program:

C:\Roseindia\vinod\jdbc4.0>javac insertImage.java

C:\Roseindia\vinod\jdbc4.0>java insertImage
Insert Image Example!
Inserting Successfully!

C:\Roseindia\vinod\jdbc4.0>

Table Name: Image

Durga.jpg

Advertisements

Share on Google+Share on Google+

Inserting Image in Database Table

Posted on: October 25, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Inserting Image in Database Table   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Swapnil And Lalit
April 2, 2011
Thanks

We were facing the huge problem for uploading and downloading image through jsp in mysql by using your code. we were successful to upload the image.

Ads

 

Ads