Contact Us
    SearchSearch   RegisterRegister   Log inLog in 

store the image in database

store the image in database

Post new topic   Reply to topic     Forum Index -> Java,Struts framework, Velocity, JSF, Hibernate, Servlet, JSP, javaBeans
View previous topic :: View next topic  
Author Message

Joined: 28 Jun 2008
Posts: 154

PostPosted: Tue Sep 16, 2008 5:23 pm    Post subject: store the image in database Reply with quote


I want to save image in database through jdbc jsp. I am using mysql database. How can i insert image into database and after that how can i get retrieve that image into my jsp page.
Back to top
View user's profile Send private message
Site Admin

Joined: 20 Jul 2008
Posts: 77

PostPosted: Sat Oct 11, 2008 8:14 am    Post subject: Reply with quote

Insert image in database of mysql you need to do some extra work. It is not single line work. You have to use either oreilly Multipart request upload api or have to use (apache common file upload api + apache common io api). I have example of client side to upload image into database through jsp.
Two jsp need to do this work.

First jsp take input from client through browse file input type file option. This data is sent to next file in multipart request to process mean to insert image to mysql

You need three jar file in WEB-INF/lib folder


Mysql Database table

CREATE TABLE upload_image (
  `iImageID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `bImage` blob NOT NULL,
  PRIMARY KEY (`iImageID`)


<%@ page language="java"  errorPage="" %>
<title>Image insert into database</title>

<form name="frm" action="saveImage.jsp" enctype="multipart/form-data" method="post">
 <input type="file" name="uProperty" /> <br>
 <input type="submit" name="goUpload" value="Upload" />

<%@ page import="java.sql.*" %>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="*"%>
<%@ page import="java.util.*"%>
   Connection conn=null;
    conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jsp","root", "");

   PreparedStatement psImageInsertDatabase=null;
   byte[] b=null;
      String sqlImageInsertDatabase="insert into upload_image (bImage) values(?)";
      DiskFileItemFactory factory = new DiskFileItemFactory();

      ServletFileUpload sfu = new ServletFileUpload(factory);
      List items = sfu.parseRequest(request);

      Iterator iter = items.iterator();
      while (iter.hasNext()) {
         FileItem item = (FileItem);
         if (!item.isFormField()) {
              b = item.get();
   catch(Exception e)


Saved Image

If image is inserted successfully in database it will show saved image otherwise it will return back to previous screen imageUpload.jsp
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> Java,Struts framework, Velocity, JSF, Hibernate, Servlet, JSP, javaBeans All times are GMT
Page 1 of 1

Jump to:  
  Quick Post a Reply Topic
  Name (required)  
  Email Address (will not be published) (required)  
  Subject of topic (required)  

  More Links  
JSP Introduction
JSP Setup
Simple JSP Example
JSP Variable
Array Declaration in JSP
JSP Forms and User Input
Form Validation
JSP Cookies
JSP Session Object
Loop, content collection, iterator, conditional check in JSP
JSP Application Object
JSP Include
JSP Response Object
JSP Request Object
JSP Error Handling
JSP File Handling Object
JSP Action Tag
JSP Implicit Objects
JSP Declaration
JSP Directive
JDBC Introduction
JDBC Driver
JDBC Connection
JDBC Prepared/Statement
JDBC ResultSet
JDBC Close Connection object
JDBC Insert
JDBC Display Records
JDBC Update
JDBC Delete
JDBC Sorting
JDBC Query
JDBC Exception
JDBC Add Batch
JDBC Transaction/RollBack