Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Pro JSP
|
Pro JSP Advanced JSP coding questions. Beginning questions will be redirected to the Beginning JSP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro JSP 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 January 16th, 2006, 06:24 AM
Registered User
 
Join Date: Dec 2005
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to arun_sunmic
Default Retrieving img file from app server using database

       Hai,i am V.S.Arun.There is an easy way for retrieving any files(image,text,audio,video,etc...)from any database.


    1.)Here for sample i used MS-Access database.
    2.)The driver used is Type 1 JDBC DRIVER(jdbc-odbc bridge driver).
    3.)The follwing code works fine when your application is small.
    4.)When the application grows larger and larger ur task of
       retrieving files gets complicated.


Here is a complete example.


              The concept is just to store the locations of the files(any) in the application server. Here there is no need to store the entire file in the database.

for example consider that i am having some image files in the following locations in the server(Tomcat server)

     CATALINA_HOME/examples/jsp/images

I can store it in a database table as follows:

      Table Name -----> img

This table has only one field named ----> location

                        Table(img)

  field 1 ---> location

                 /examples/jsp/images/img1.jpeg
                 /examples/jsp/images/img2.jpeg
                 /examples/jsp/images/img3.jpeg
                 /examples/jsp/images/img4.jpeg
                 /examples/jsp/images/img5.jpeg


         Here is the code to retrive the image files



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

<html>
<body bgcolor="9caed6">
<br><br>
<center>

Some of my pictures

</center>
<br><br>

<%

response.setContentType("text/html");

Connection con = null;
Statement st = null;
ResultSet rs = null;

String location = null;

try
{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:datasource name");
st = con.createStatement();
rs = st.executeQuery("select *from img");

while(rs.next())
{
location = rs.getString(2);

%>

<img src="<%=location%>" > </img>

<br><br>

<%

                                                                                                                            }
st.close();
con.close();

}

catch(Exception e)
{
out.println("Error while retrieving"+e);
}

finally
{
if(con!=null)
con = null;
}

%>

</body>
</html>





Similar Threads
Thread Thread Starter Forum Replies Last Post
Storing & retrieving swf file from sql server tarduk2004 ASP.NET 2.0 Basics 2 October 30th, 2007 02:31 AM
<img> from database... How?? arganaz ASP.NET 2.0 Basics 3 November 20th, 2006 06:58 AM
retrieving img from sql and showing it in imgctrl harshaghanta ASP.NET 2.0 Professional 7 August 16th, 2006 05:51 PM
Saving/Retrieving Image file to Database davekrunal46 VB How-To 6 January 11th, 2006 04:41 AM
Retrieving Roles using C# / ADSI in ASP.Net app Prashant.k.m ASP.NET 1.0 and 1.1 Professional 0 October 4th, 2005 04:28 AM





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