473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Not displaying image from database

Expand|Select|Wrap|Line Numbers
  1. <%@ page import="java.sql.*" %>
  2. <% Class.forName("com.mysql.jdbc.Driver"); %>
  3. <HTML>
  4.     <HEAD>
  5.         <TITLE>Fetching Data From a Database</TITLE>
  6.     </HEAD>
  7.     <BODY>
  8.         <H1>Fetching Data From a Database</H1>
  9.         <% 
  10.  
  11. response.setContentType("image/jpg");
  12.  
  13.  
  14.  
  15.  Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","srys");
  16.             Statement statement = connection.createStatement();
  17.             String id = request.getParameter("id");  
  18. ResultSet resultset = statement.executeQuery("select file_data from file where id = '" + id + "'") ; 
  19.             if(!resultset.next()) {
  20.                 out.println("Sorry, could not find that publisher. ");
  21.             } else {
  22.         %>
  23.         <TABLE BORDER="1">
  24.             <TR>
  25.  
  26.                <TH>picture</TH>
  27.            </TR>
  28.            <TR>
  29.  
  30.                <TD> <%= resultset.getBlob(1) %> </TD>
  31.            </TR>
  32.        </TABLE>
  33.        <BR>
  34.        <% 
  35.            } 
  36.        %>
  37.     </BODY>
  38. </HTML>
May 14 '14 #1
1 1338
chaarmann
785 Expert 512MB
Do not mix content of different types within a single response. Either send only the binary data for the image (contentType = image/jpg) or only send the HTML tags(contentType =text/html).

In your case you send HTML-text bytes (with embedded image data bytes), but tell the browser (by setting the content type) to interpret all the data as an image. Of course this does not work, because an image has a special structure and the browser sees that the data does not fit to the structure, so it displays an error or does nothing.

Send the embedding HTML page first without image data, but with an image tag instead that should contain an URL. When the browser renders the HTML page it fires a second request with this URL automatically and then you send over the image data only.
May 14 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Barnes | last post by:
I have an asp form that sends data to a redirect page with SQL using JScript. There is a function that I'm using called replace(). The form submits without errors and goes to the redirect page but...
14
by: Akbar | last post by:
Hey there, Big-time curiosity issue here... Here's the test code (it's not that long)... it's to display a large number of image links with captions, ideally pulled in from an external file...
1
by: Ziemowit Skowronski | last post by:
Hi, I have a problem with displaying unicode strings in ASP.NET (C#). I recieve data from MS SQL Server database and use them to construct navigation on the page. Because I'm from Poland, I have...
3
by: velu | last post by:
Asp.Net 2 I am trying to display Image from Northwind Database. I ran a queary "SELECT CategoryID, CategoryName, Description, Picture FROM Categories" but the Dataview displayes all the fields...
2
by: sj | last post by:
I am just learning to use Tkinter and am having problems displaying image files. I am able to display an image using tutorials (such as http://www.daniweb.com/code/snippet296.html) But when I try...
1
by: shivendravikramsingh | last post by:
hi friends,i have a problem i m using this code for displaying image.this code is working fine when we run this code in internet Explorer but when the same page with this code i can in mozilla or...
5
by: shreedhan | last post by:
Hey all, I have a strange type of problem. I have a simple code of html (say in test.html) <img src="/home/Shreedhan/edited_shreedhan2.jpg" /> If I put this code in a separate file and...
2
by: shivapadma | last post by:
i have inserted the image into database using the following code String driverName = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/"; String dbName =...
3
by: rhandoo | last post by:
Hi, I am facing the problem in displaying the special characters like ( ` ) GRAVE ACCENT in C# windows Application. I am using oracle as back end. In oracle database table the string contain...
3
by: Hataf | last post by:
Hi! I am having a problem in displaying images from database. This the code gallery.php <?php session_start(); if(isset($_SESSION)){} //die('ERROR:'.mysql_error());
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.