Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Inserting Image in Database Table
In this section, you will learn to insert an image to the MySQL database table.
 
Search Tutorials:
 
Software Solutions and Services
 

 
Google Custom Search:
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation

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

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

11 comments so far (
post your own) View All Comments Latest 10 Comments:

THANK YOU SO MUCH

Posted by ankur on Tuesday, 12.2.08 @ 00:06am | #82265

No matter how much hard time I have, once I arrive to roseIndia, I get my solution. Not only that, I also have a better understanding.
THANKS ALOT!

Posted by a on Thursday, 09.18.08 @ 03:17am | #80180

It is very good ..

How to use MySql and how to work..

I want to know how to retrive the store image form MySql DataBase.

Posted by subrahmanyam on Tuesday, 05.13.08 @ 17:59pm | #59721

i have a db of images. how can i reterieve images from database using EL(Expression Language)

Posted by Ravi on Saturday, 12.29.07 @ 14:04pm | #44125

Hi, I have never seen a site like this that is realy helpful for the new JAVA Developers ..

Plz keep the Good Work Continue .....

Thnx ....

Posted by Adnan on Friday, 12.28.07 @ 16:57pm | #44053

Hi
Please could anyone tell me is there a tutorial or give me instructions how to insert files in Blob using spring2+ spring + hibernate
Thank you very much

Posted by Lidija on Thursday, 12.27.07 @ 20:17pm | #43975

Please could you tell me how to upload files in DB using Struts2 + Spring?
Thank you very much

Posted by Lidija on Thursday, 12.27.07 @ 19:50pm | #43972


hi,
please tell me where should i place the
imageFile? May i use this code to insert image
into Sql Servet or it's MySql Specific?

Posted by Kalpana on Friday, 12.14.07 @ 15:19pm | #42243

please give us a db code for a table image.
thanks.

Type

Posted by typemishmash on Thursday, 12.6.07 @ 18:49pm | #41452

Hi, give a sql code on table Image.
thanks.

Posted by No name on Tuesday, 12.4.07 @ 21:06pm | #41286

Training Courses
Tell A Friend
Your Friend Name
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.