Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > JSP Basics
|
JSP Basics Beginning-level questions on JSP. More advanced coders should post to Pro JSP.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the JSP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 13th, 2008, 07:47 AM
Registered User
 
Join Date: Feb 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default dispalying images from the database mysql

Please help me !!
Its really urgent....
I have to display diffrent images from the database mysql having same imageid say imgid=1 in a jsp page.Please tell me the proper code for it.


<%@ page import="java.sql.*" %>
<%@ page import= "java.io.*" %>
<%
String connectionURL = "jdbc:mysql://localhost:3306/modelval?user=modval;password=modval";
Connection connection = null;
Statement statement = null;
%>

<%
int Id=request.Parameter("id");
Class.forName("com.mysql.jdbc.Driver").newInstance ();
connection = DriverManager.getConnection(connectionURL, "modval", "modval");
statement = connection.createStatement();
ResultSet rs = statement.executeQuery( "SELECT Photo FROM tab_photos where CountryID= " + Id + " ");

rs.next();
byte [] barray = rs.getBytes("Photo");
rs.close();
statement.close();
response.setContentType("image/gif");
response.getOutputStream().write(barray);
rs.close();
%>

this display only one image .But i have to retrieve three diffrent images in html td tags by applying a while loop.... how can it be possible... please get the code for me....

consultant
 
Old February 14th, 2008, 11:19 PM
jomet
Guest
 
Posts: n/a
Default

HI,

<table>
<tr>
response.setContentType("image/gif");
while(rs.next())
{
byte [] barray = rs.getBytes("Photo");
%>
<td><%response.getOutputStream().write(barray); %></td>
<%
}
rs.close();
statement.close();
%>
</tr>
</table>

i dont tested,
try something like this,

Cheers,

jomet.
---------------------------------------------
Once you start a working on something,
dont be afraid of failure and dont abandon it.
People who work sincerely are the happiest.
 
Old February 15th, 2008, 01:39 AM
Registered User
 
Join Date: Feb 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanx Jomet,It was very nice to talk to you...

The problem has been sought....Actuallly I had to display the images dynamically ...so I passed the parameter like CountryId.Something like this....MyPhoto.jsp?CountryId=<%=rs.getString("CountryId") %> where Country Id is the id which has different images in the database table name tab_photos.


the Code is something like this...

<%@ page import="java.sql.*" %>
<%@ page import= "java.io.*" %>
<%
String connectionURL = "jdbc:mysql://localhost:3306/modelval?user=modval;password=modval";
Connection connection = null;
Statement statement = null;
%>
<%
String Id=request.getParameter("CountryId");
Class.forName("com.mysql.jdbc.Driver").newInstance ();
connection = DriverManager.getConnection(connectionURL, "modval", "modval");
statement = connection.createStatement();
ResultSet rs = statement.executeQuery( "SELECT * FROM tab_photos where CountryId= '" + Id + "' ");
rs.next();
byte [] barray = rs.getBytes("Photo");
rs.close();
statement.close();
response.setContentType("image/jpeg");
response.getOutputStream().write(barray);
%>

i had writen tis code in MyPhoto.jsp and call this code from ---.jsp page through the given parameter MyPhoto.jsp?CountryId=<%=rs.getString("CountryId") %>


consultant
 
Old February 15th, 2008, 01:56 AM
Registered User
 
Join Date: Feb 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

HI!
 I am getting some problem...Can anyone please tell what's the code to convert tinyblobs to images in jsp...as tinyblobs is the dataype of image field of table tab_name in Mysql.

when I give the datatype as blob and write the code

rs.next();
byte [] barray = rs.getBytes("Photo");
rs.close();
statement.close();
response.setContentType("image/jpeg");
response.getOutputStream().write(barray);

The image is being dispalyed but when i change it to tinyblob then no image is there..Is this the right code for tinyblobs....
I need your help.Thanx in advance...

Amit Srivastava





Similar Threads
Thread Thread Starter Forum Replies Last Post
Load Images from and Save Images to a Database cyndie VB.NET 2 August 17th, 2008 06:42 AM
How to insert images to MySQL (BLOB) wkm1925 ASP.NET 2.0 Professional 0 November 19th, 2006 06:29 AM
Images and MYsql Capt D MySQL 4 April 5th, 2006 11:17 AM
HELP - Displaying Images from mysql timmaher Beginning PHP 5 January 26th, 2005 03:09 PM
displaying images from mySql DB in html cmcmillan.schillers Beginning PHP 1 October 5th, 2004 03:52 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.