Home Jsp Retrieve image from mysql database through jsp

Related Tutorials


 
 

Share on Google+Share on Google+

Retrieve image from mysql database through jsp

Advertisement
In this section we will see how to retrieve image from mysql database through jsp code.

Retrieve image from mysql database through jsp

     

In this section we will see how to retrieve image from mysql database through jsp code. First create a database of structure given below and save images. Here we will use table 'save_image' of database 'mahendra'.

Structure of table 'save_image'

First create database named 'mahendra' by query given below....

 

 

 

CREATE TABLE save_image (             
              id int(5) NOT NULL auto_increment,  
              name varchar(25) default NULL,      
              city varchar(20) default NULL,      
              image blob,                         
              Phone varchar(15) default NULL,     
              PRIMARY KEY  (`id`)                   
            );

Create application directory named "user" in the tomcat-6.0.16/webapps. Before running this java code you need mysql connector jar file in the Tomcat-6.0.16/webapps/user/WEB-INF/lib.

mysql> create database mahendra;

Note : In the jsp code given below, image will be retrieved from database on the basis of  'id' field of the table. So code will retrieve image of specified 'id' value. In this example we will retrieve image with 'id' value 11. So before running this jsp code first check whether image for the specified 'id' value is present.

retrieve_image.jsp

<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %> 

<% // declare a connection by using Connection interface Connection connection = null;
/* Create string of connection url within specified format with machine 
name, port number and database name. Here machine name id localhost 
and database name is mahendra. */
String connectionURL = "jdbc:mysql://localhost:3306/mahendra";
/*declare a resultSet that works as a table resulted by execute a specified 
sql query. */
ResultSet rs = null;
// Declare statement.
PreparedStatement psmnt = null;
// declare InputStream object to store binary stream of given image.
InputStream sImage;
try {
// Load JDBC driver "com.mysql.jdbc.Driver"
Class.forName("com.mysql.jdbc.Driver").newInstance();
/* Create a connection by using getConnection() method that takes 
parameters of string type connection url, user name and password to 
connect to database. */
connection = DriverManager.getConnection(connectionURL, "root", "root");
/* prepareStatement() is used for create statement object that is 
used for sending sql statements to the specified database. */
psmnt = connection.prepareStatement("SELECT image FROM save_image WHERE id = ?");
psmnt.setString(1, "11"); // here integer number '11' is image id from the table
rs = psmnt.executeQuery();
if(rs.next()) {
byte[] bytearray = new byte[1048576];
int size=0;
sImage = rs.getBinaryStream(1);
response.reset();
response.setContentType("image/jpeg");
while((size=sImage.read(bytearray))!= -1 ){
response.getOutputStream().write(bytearray,0,size);
}
}
}
catch(Exception ex){
out.println("error :"+ex);
}
finally {
// close all the connections.
rs.close();
psmnt.close();
connection.close();
}
%>


Save this code as a .jsp file named "retrieve_image.jsp" in the application directory in Tomcat-6.0.16 and run this jsp page with following url in address bar of the browser http://localhost:8080/user/retrieve_image.jsp

Download Source Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 19, 2008

Related Tutorials

Discuss: Retrieve image from mysql database through jsp   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:12
prasanth chottathol
September 27, 2011
If any error found

response.getOutputStream().flush(); while((size=sImage.read(bytearray))!= -1 ){ response.getOutputStream().write(bytearray,0,size); } add the response.getOutputStream().flush(); before the while loop.
Prasanth Chottathol
September 27, 2011
If any error use this code

response.getOutputStream().flush(); while((size=sImage.read(bytearray))!= -1 ){ response.getOutputStream().write(bytearray,0,size); } add the response.getOutputStream().flush(); before the while loop
Carlos
October 15, 2011
PROGRAMACIÃ?N POSTGRESQL JAVA SQL

Loading Images CONNECTED WITH JSP Servlet and PostgreSQL
Charles
October 15, 2011
bytea change with servlet jsp postgresql

Why deployed image using this technologies
Thomas
February 25, 2013
Resizing the image

Hey Roseindia, That was a great tutorial thank you. I am just wondering how to resize the image using jsp/css when it comes out the other end? Thanks
Mahesh Chunkhade
December 20, 2011
Thanks

This tutorial very useful . also very nice site for online learning.
Sujitha
January 9, 2013
with height and width

Its working. But how to specify height and width.
abdulrahman
April 25, 2012
website

hi which is to manage pictures, you will have to use HTML, Javascript and PHP languages. The data concerning the pictures will be stored in a MySQL data base.The way you will organise your directories containing HTML documents, PHP scripts, library files is important and it will be reflected in the final score.The interfaces that you will see in this document are given as example. You are free to organize your own interfaces provided then can offer the same features. For example, in the first interface, textual links are used to choose the feature. You may decide to give other king of tool to do this choice : button, radio button, selection list, pictures,... Navigating between these interfaces is important. You are free to decide how the user will be able to do this navigation.The topic of the work,The project is the management of a collection of pictures. At the beginning these pictures are supposed to be already stored on the Web server like this : They are all in the same folder called "photos". This folder contains subfolders in which the pictures are stored.photos--> pictA,pictB,pictC,pictD These pictures are the original pictures and there size can be too important to be able to work with them directly. The work is divided in three parts corresponding to three main features. Each of them can be reached from the home page. This home page must contains tools which allow the user to activate these activities.To do that, the application will send to the user an interface showing the list of the sub-folders. So, the user will be able to choose which folder will be treated. The treatment is to create thumbnails for all the pictures in the chosen folder. The thumbnail corresponding to a picture will have the name of the picture with the prefix "min_". Example : The picture named "P12089.jpg" will have the corresponding thumbnail "min_ P12089.jpg".Just by clicking on the name of a sub-folder the user can start the creation of the thumbnails corresponding to the pictures of this sub-folder.The script creating these thumbnails will return an interface showing all the thumbnails of the subfolder.Identify a picture means to associate this picture with a location (only one location by picture), one or several themes and one or several topics. When the user ask to identify the pictures he must be able to choose the directory he wants to work on (like when he creates the thumbnails). Then he will be able to see the thumbnails contained in this folder (like when he creates the thumbnails). To choose the picture to be identify he just have to click on the thumbnail. Then he receives an interface allowing him to give the informations concerning this picture. At the beginning, this interface can look like :There is no location, no theme and no subject but this interface must allow the user to enter new location, theme or subject. As soon as one of these informations is entered, it is inserted into the interface like this :This example of interface indicates that there are one location and one theme in the corresponding tables but still no subject. The user selected the location and the theme and added a comment for the picture.When the user validates these informations he receives an interface which looks like
plc training india
September 20, 2012
http://www.technocratautomation.co.in/

i like your post
DMCA.com