Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Inserting Image in a database Table 
 

Consider a case where we want that along with the name of the person and its information, his image should also come with all these things.

 

Inserting Image in MySql Database Table

                         

Consider a case where we want that along with the name of the person and its information, his image should also come with all these things. After going through this tutorial you can better understand the concept of inserting a image in the database table, so go through this example properly.

To get the program working we need to use a doGet() method to write our business logic as it is server side programming so all the processing will be done by the container.  First of all make a class named JdbcInsertImage, the name of the class should be such that the person can understand what the program is going to do. This class must extend the HttpServlet class which is an abstract method.  Now inside the doGet() method call the method getWriter() of the class PrintWriter. To insert a image from our java program we need to make a connection between our java class and the MySql database which we are using.  After the connection establishment we will pass a insertion query for the image in the prepareStatement() method of the Connection object which returns the PreparedStatement object. Note that the data type for the image we have used is mediumblob. It is case sensitive.

As we have to insert an image file in our database so there is a need to use a File class of the java.io package. In the constructor of the File class pass the path of the file. To read the image file we will use FileInputStream class. To set the image in the database use the method setBinaryStream() of the PreparedStatement interface. If the image will be inserted in the database you will get the message "image has been inserted" otherwise "image is not inserted".

The code of the program is given below:

 

import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class JdbcInsertImage extends  HttpServlet{
public void doGet(HttpServletRequest request, 
HttpServletResponse response) throws ServletException, IOException
{
PrintWriter pw = response.getWriter();
String connectionURL = "jdbc:mysql://localhost:3306/roseindia";
java.sql.Connection connection=null;
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, 
"root", "root");
PreparedStatement pst = connection.prepareStatement
("insert into image values(?,?)");
File file = new File("C:/apache-tomcat-5.5.20/webapps
/mywork/grad_sm.gif");
FileInputStream fis = new FileInputStream(file);
pst.setBinaryStream(1,fis,fis.available());
pst.setString(2, "Tim");
int i = pst.executeUpdate();
if(i!=0)
{
pw.println("image has been inserted");
}
else
{
pw.println("image is not inserted");
}	
}
catch (Exception e)
{
System.out.println(e);
}
}
}

The output of the program is given below:

 

 

 

Download this example.

 

                         

» View all related tutorials
Related Tags: c ide orm table data form io make type column field name columns change this id nsis war tab row

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

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

i tried that code ..it is working for small size of image only.when we are trying for larger size image
it is showing this error..Packet for query is too large (2509425 > 1048576). You can change this value on the server by setting the max_allowed_packet' variable....i am new to this how i can change the size....i am eagerly waiting for your reply..

Posted by kishore on Tuesday, 07.29.08 @ 15:35pm | #69772

pls help me to retrieve image from database..

Posted by ravi on Wednesday, 11.21.07 @ 13:22pm | #38145

how retieve images from database through java

Posted by aswin on Saturday, 10.13.07 @ 11:51am | #33470

pls tell me how to insert and select image in midlet using j2me to mysql

Posted by mala on Saturday, 07.28.07 @ 21:20pm | #22120

How to Insert and Retrieve multiple images in to the database .u are given only for inserting one image in to the database.could u pl tell me the process of inserting&Retrieving multiple images.

Posted by vishnuvardhan reddy siddareddy on Wednesday, 03.28.07 @ 07:45am | #12821

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
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.