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
juniormember



Joined: 28 Jun 2008
Posts: 154

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

Hi,

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
seniormember
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

Quote:
commons-fileupload-1.2.1.jar http://commons.apache.org/fileupload/index.html
commons-io-1.4.jar http://commons.apache.org/io/
mysql-connector-java-5.1.5-bin.jar http://www.mysql.com/


Mysql Database table

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


imageUpload.jsp
Code:

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

<body>
<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" />
</form>
</body>
</html>


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

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

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

      Iterator iter = items.iterator();
      
      while (iter.hasNext()) {
         FileItem item = (FileItem) iter.next();
         if (!item.isFormField()) {
              b = item.get();
          }
      }
      
      psImageInsertDatabase.setBytes(1,b);
      psImageInsertDatabase.executeUpdate();
   }
   catch(Exception e)
   {
     e.printStackTrace();
     response.sendRedirect("imageUpload.jsp");
   }

%>

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)  
     
   
     
   
     
  Advertisement  
     
 
     


  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