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